UpgradeGunUI.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import { _decorator, Node, Label} from 'cc';
  2. import { BaseExp } from '../core/base/BaseExp';
  3. import List from '../third/List';
  4. import { autoBind } from '../extend/AutoBind';
  5. import { userIns } from '../data/UserData';
  6. import MsgHints from '../utils/MsgHints';
  7. import { Constants } from '../data/Constants';
  8. import { stateMgr } from '../core/manager/StateManager';
  9. import { UpgradeItem } from '../items/item/UpgradeItem';
  10. import { uiMgr } from '../core/manager/UIManager';
  11. import { ArsenalUI } from './ArsenalUI';
  12. const { ccclass, property } = _decorator;
  13. @ccclass('UpgradeGunUI')
  14. export class UpgradeGunUI extends BaseExp {
  15. @autoBind({ type: List, tooltip: "枪的属性数据" })
  16. public gun_attr_scrollView: List;
  17. @autoBind({ type: Label, tooltip: "金币文本" })
  18. public gold_lable: Label;
  19. @autoBind({ type: Label, tooltip: "钻石文本" })
  20. public diamond_lable: Label;
  21. @autoBind({ type: Node, tooltip: "模型摄像机" })
  22. public model_camera: Node;
  23. //升级数据
  24. private gunAttrList:any[] = [];
  25. //枪数据
  26. private curGunData:any = null;
  27. start() {
  28. this.hasAnim = false;
  29. this.closeOnBlank = false;
  30. //注册动态变化值
  31. stateMgr.registerUI(Constants.gold, this.gold_lable);
  32. stateMgr.registerUI(Constants.diamond, this.diamond_lable);
  33. }
  34. public show(...args: any[]){
  35. this.model_camera.active = true;
  36. const gunData:any = args[0];
  37. //加载选中的枪
  38. this.loadUpgradeData(gunData.id);
  39. }
  40. public hide(){
  41. this.model_camera.active = false;
  42. const arsenalUI:ArsenalUI = uiMgr.getPageComponent(Constants.popUIs.arsenalUI);
  43. arsenalUI.model_camera.active = true
  44. }
  45. /**
  46. * 加载枪的数据
  47. */
  48. public loadUpgradeData(id:string){
  49. this.curGunData = userIns.data.guns.find(gun => gun.id === id);
  50. //模型摄像机下的枪
  51. this.model_camera.children.forEach(child => {
  52. child.active = `${this.curGunData.prb_name}_gun` === child.name;
  53. });
  54. //选择枪的属性列表
  55. this.gunAttrList = [];
  56. const values = userIns.gunAttrKeys.map(key => this.curGunData[key]);
  57. values.forEach((value,i) => {
  58. let attr_name: string = userIns.gunAttrKeys[i];
  59. // 生成消耗字段的key(使用驼峰命名转换)
  60. const attr = attr_name.replace(/^\w/, (c) => c.toUpperCase());
  61. const expendKey:string = `next${attr}Expenditure`;
  62. const uniqueIdKey:string = `${attr_name}UniqueId`;
  63. const attrLevelKey:string = `${attr_name}Level`;
  64. //里面包括属性总的值和最大等级值 totalValue maxLevel
  65. let mData = userIns.getGunMaxValue(this.curGunData,attr_name);
  66. this.gunAttrList.push({
  67. attr_name: attr_name,
  68. attr_value: value,
  69. attr_icon: this.curGunData[`${attr_name}_icon`],
  70. ...mData,
  71. attribute_lang: userIns.playerGunAttsTable.find(e=>e.attribute_name == attr_name)?.attribute_lang,
  72. nextExpenditure: this.curGunData[expendKey] || '',
  73. unique_id: this.curGunData[uniqueIdKey] || '',
  74. level: this.curGunData[attrLevelKey] || 1
  75. })
  76. });
  77. this.gun_attr_scrollView.numItems = this.gunAttrList.length;
  78. }
  79. /**
  80. * 选择枪的属性升级列表的数据
  81. * @param item item节点
  82. * @param idx 数据信息
  83. */
  84. public setGunAttsItemData(item: Node, idx: number) {
  85. let com:UpgradeItem = item.getComponent(UpgradeItem);
  86. com.init(this.gunAttrList[idx],this.clicked.bind(this));
  87. }
  88. /**
  89. * item上的按钮点击事件
  90. * @param data 数据
  91. * @param idx 点击按钮的下标 1、免费看广告 2、金币 3、钻石
  92. * @param clikTarget 点击对象
  93. */
  94. public clicked(data:any,idx:number,clikTarget: Node){
  95. if(data.isMaxLevel){
  96. MsgHints.show(`This gun ${data.attr_name} is at the maximum level!`);
  97. }else{
  98. const [id,num] = data.nextExpenditure.split("_");
  99. const sData:any = userIns.itemTable.find(e=>e.id == id);
  100. //是否升级
  101. let isSucced: boolean = false;
  102. if(idx === 1){//免费看广告
  103. }else if(idx === 3){//金币钻石
  104. if(sData.id === 1001){
  105. if(userIns.data.gold < num){
  106. MsgHints.show("Insufficient gold!");
  107. }else{
  108. userIns.data.gold -= num;
  109. isSucced = true;
  110. }
  111. }else if(sData.id === 1002){//钻石
  112. if(userIns.data.diamond < num){
  113. MsgHints.show("Insufficient diamonds!");
  114. }else{
  115. userIns.data.diamond -= num;
  116. isSucced = true;
  117. }
  118. }
  119. }
  120. if(isSucced){//升级
  121. userIns.upgradeGun(this.curGunData,data.attr_name);
  122. //刷新数据
  123. this.loadUpgradeData(this.curGunData.id);
  124. }
  125. }
  126. }
  127. }