ArsenalUI.ts 7.1 KB

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