ArsenalUI.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import { _decorator, Node,Label, EventTouch} from 'cc';
  2. import List from '../third/List';
  3. import { Utils } from '../utils/Utils';
  4. import { userIns } from '../data/UserData';
  5. import { GunItem } from '../items/item/GunItem';
  6. import { autoBind } from '../extend/AutoBind';
  7. import { BaseExp } from '../core/base/BaseExp';
  8. import { GunAttrItem } from '../items/item/GunAttrItem';
  9. import { GunTypeItem } from '../items/item/GunTypeItem';
  10. import { uiMgr } from '../core/manager/UIManager';
  11. import { Constants } from '../data/Constants';
  12. import MsgHints from '../utils/MsgHints';
  13. import { Game } from '../game/Game';
  14. const { ccclass, property } = _decorator;
  15. @ccclass('ArsenalUI')
  16. export class ArsenalUI extends BaseExp {
  17. @autoBind({ type: List, tooltip: "枪的属性列表" })
  18. public gun_atts_scrollView: List;
  19. @autoBind({ type: List, tooltip: "枪的类型列表" })
  20. public gun_type_scrollView: List;
  21. @autoBind({ type: List, tooltip: "枪的列表" })
  22. public gun_scrollView: List;
  23. @autoBind({ type: Label, tooltip: "枪的名字" })
  24. public gun_name_label: Label;
  25. @autoBind({ type: Label, tooltip: "枪的类型的名字" })
  26. public type_name_label: Label;
  27. @autoBind({ type: Node, tooltip: "模型摄像机" })
  28. public model_camera: Node;
  29. //枪的属性数据
  30. private gunAttrList:Array<any> = [];
  31. //枪的类型数据
  32. private gunTypeList:Array<any> = [];
  33. //每个类型下的枪的数据
  34. private gunList:Array<any> = [];
  35. //玩家选择的类型 默认用户使用的枪的所在的类型
  36. public curTypeData:any = null;
  37. //用户当前选择的枪 默认用户当前使用的枪
  38. private curGunData:any = null;
  39. start() {
  40. this.hasAnim = false;
  41. this.closeOnBlank = false;
  42. }
  43. public show(...args: any[]){
  44. this.model_camera.active = true;
  45. this.curGunData = userIns.getCurUseGun();
  46. this.loadArsenalData();
  47. }
  48. public hide(){
  49. this.model_camera.active = false;
  50. }
  51. /**
  52. * 强制刷新列表数据
  53. */
  54. public forceRefresh(){
  55. this.curGunData = userIns.getCurUseGun();
  56. this.loadArsenalData(this.curTypeData,true)
  57. }
  58. /**
  59. * 加载武器库数据 选中的类型数据
  60. * @param typeData 选择的类型 默认用户选中的第一把枪的类型
  61. */
  62. public loadArsenalData(typeData:any = null,force: boolean = false){
  63. //列表2、枪的类型列表、查询玩家拥有的枪的类型 按type分组 从每个分组中取出第一个元素
  64. const groupedByType = userIns.playerGunsTable.reduce((acc: Record<number, any[]>, gun) => {
  65. if (!acc[gun.type]) {
  66. acc[gun.type] = [];
  67. }
  68. acc[gun.type].push(gun);
  69. return acc;
  70. }, {});
  71. if(this.gunTypeList.length <= 0 || force){
  72. this.gunTypeList = Object.values(groupedByType).map(group => {
  73. const typeData = group[0];
  74. //判断该类型是否解锁(用户是否拥有该类型的枪)
  75. const unlocked = userIns.data.guns.some(gun => gun.type === typeData.type);
  76. //判断是否选中类型(与当前使用枪械类型一致)
  77. const selected = typeData.type === this.curGunData?.type;
  78. return {
  79. ...typeData,
  80. unlocked: unlocked,
  81. selected: selected
  82. };
  83. });
  84. }
  85. this.gun_type_scrollView.numItems = this.gunTypeList.length;
  86. this.curTypeData = typeData ?? this.gunTypeList.find(gun => gun.type === this.curGunData.type);
  87. //枪的名字
  88. this.gun_name_label.string = this.curGunData.name_lang;
  89. //枪的类型的名字
  90. this.type_name_label.string = this.curTypeData.type_name;
  91. //列表3、类型的下的枪的类表、查询该类型下玩家拥有的枪
  92. const hasGunList = Utils.clone(userIns.data.guns)
  93. .filter(gun => gun.type === this.curTypeData.type)
  94. .map(gun => ({
  95. ...gun,
  96. unlocked: true,
  97. selected: gun.id == this.curGunData.id
  98. })); //添加解锁状态;
  99. //添加未解锁的枪
  100. const unlockGuns = Utils.clone(userIns.playerGunsTable)
  101. .filter(gun =>
  102. gun.type === this.curTypeData.type &&
  103. !hasGunList.some(g => g.id === gun.id)
  104. )
  105. .map(gun => ({
  106. ...gun,
  107. unlocked: false,
  108. selected: false
  109. }));
  110. //合并已解锁和未解锁的枪械列表
  111. this.gunList = [...hasGunList, ...unlockGuns];
  112. this.gun_scrollView.numItems = this.gunList.length;
  113. //模型摄像机下的枪
  114. this.model_camera.children.forEach(child => {
  115. child.active = `${this.curGunData.prb_name}_gun` === child.name;
  116. });
  117. //列表1、选择枪的属性列表
  118. this.gunAttrList = [];
  119. const values = userIns.gunAttrKeys.map(key => this.curGunData[key]);
  120. values.forEach((value,i) => {
  121. let attr_name: string = userIns.gunAttrKeys[i];
  122. //里面包括属性总的值和最大等级值 totalValue maxLevel
  123. let mData = userIns.getGunMaxValue(this.curGunData,attr_name);
  124. this.gunAttrList.push({
  125. attr_name: attr_name,
  126. attr_value: value,
  127. attr_icon: this.curGunData[`${attr_name}_icon`],
  128. ...mData,
  129. attribute_lang: userIns.playerGunAttsTable.find(e=>e.attribute_name == attr_name)?.attribute_lang
  130. })
  131. });
  132. this.gun_atts_scrollView.numItems = this.gunAttrList.length;
  133. }
  134. /**
  135. * 选择枪的属性列表的数据
  136. * @param item item节点
  137. * @param idx 数据信息
  138. */
  139. public setGunAttsItemData(item: Node, idx: number) {
  140. let com:GunAttrItem = item.getComponent(GunAttrItem);
  141. com.init(this.gunAttrList[idx]);
  142. }
  143. /**
  144. * 选择枪列表的数据
  145. * @param item item节点
  146. * @param idx 数据信息
  147. */
  148. public setGunListItemData(item: Node, idx: number) {
  149. let com:GunItem = item.getComponent(GunItem);
  150. com.init(this.gunList[idx],(gunData:any)=>{
  151. if(gunData.id == this.curGunData.id)return;
  152. this.curGunData = gunData;
  153. this.loadArsenalData(this.curTypeData);
  154. //给玩家把枪也切换了
  155. userIns.changeGun(gunData.id);
  156. Game.I?.player?.loadGunName(gunData.prb_name,true);
  157. });
  158. }
  159. /**
  160. * 选择枪的类型的数据
  161. * @param item item节点
  162. * @param idx 数据信息
  163. */
  164. public setGunTypeItemData(item: Node, idx: number) {
  165. let com:GunTypeItem = item.getComponent(GunTypeItem);
  166. com.init(this.gunTypeList[idx],(typeData:any)=>{
  167. this.gunTypeList.forEach(e=>e.selected = e.id == typeData.id);
  168. this.loadArsenalData(typeData);
  169. });
  170. }
  171. /**
  172. * 按钮点击事件
  173. * @param event 事件
  174. * @param param 参数
  175. */
  176. override onBtnClicked(event:EventTouch, param:any) {
  177. super.onBtnClicked(event,param);
  178. let btnName = event.target.name;
  179. if(btnName === 'upgrade_btn'){//升级枪的按钮
  180. if(!this.curGunData){
  181. MsgHints.show('Gun data not found');
  182. return;
  183. }
  184. this.model_camera.active = false;
  185. uiMgr.show(Constants.popUIs.upgradeGunUI,[this.curGunData]);
  186. }
  187. }
  188. }