ArsenalUI.ts 7.3 KB

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