UpgradeGunUI.ts 5.2 KB

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