ShopLayer.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. import { find, Node, _decorator } from 'cc';
  2. import { BaseLayer } from '../../common/BaseLayer';
  3. import { cocosUtil } from '../../utils/cocosUtil';
  4. import { utilTools } from '../../utils/utilTools';
  5. import { constants } from '../data/constants';
  6. import { localText } from '../data/localText';
  7. import { msgac } from '../data/msgac';
  8. import { ItemLayer } from '../item/ItemLayer';
  9. import { audioManager } from '../manager/audioManager';
  10. import { designManager } from '../manager/designManager';
  11. import { eventManager } from '../manager/eventManager';
  12. import { iconManager } from '../manager/iconManager';
  13. import { levelManager } from '../manager/levelManager';
  14. import { sdkManager } from '../manager/sdkManager';
  15. import { playerModel } from '../model/playerModel';
  16. const { ccclass, property } = _decorator;
  17. @ccclass('ShopLayer')
  18. export class ShopLayer extends BaseLayer {
  19. coinNode: Node;
  20. titleNode1: Node;
  21. titleNode2: Node;
  22. shopLayer: Node;
  23. btnCoinAdd: Node;
  24. type: number;
  25. id: number;
  26. onLoad() {
  27. super.onLoad();
  28. this.coinNode = this.getNodeByPath("coinLayer/num");
  29. this.titleNode1 = this.getNodeByPath("info/title1");
  30. this.titleNode2 = this.getNodeByPath("info/title2");
  31. this.shopLayer = this.getNodeByPath("info/shopLayer");
  32. this.shopLayer.addComponent(ItemLayer);
  33. this.btnCoinAdd = this.getNodeByPath("btns/btnCoinAdd");
  34. this.type = this.obj.type;
  35. this.id = this.obj.id;
  36. this.initUI();
  37. sdkManager.instance.hideCustomAdCommon();
  38. }
  39. onEnable() {
  40. this.addEventArr([
  41. msgac.coinChange,
  42. ]);
  43. }
  44. onDisable() {
  45. this.removeEventArr([
  46. msgac.coinChange,
  47. ]);
  48. }
  49. initUI() {
  50. this.initLocaltext();
  51. this.initEffect();
  52. this.refreshCoinLayer();
  53. if (this.type == constants.shopTypes.secret) {
  54. this.titleNode2.active = true;
  55. this.titleNode1.active = false;
  56. } else {
  57. this.titleNode1.active = true;
  58. this.titleNode2.active = false;
  59. }
  60. let arr = this.getShopArr();
  61. this.shopLayer.getComponent(ItemLayer).initUI(this, arr, this.refreshShopItem.bind(this));
  62. }
  63. refreshCoinLayer() {
  64. this.setString(this.coinNode, playerModel.instance.getCoin());
  65. }
  66. refreshShopItem(itemUI, item, index) {
  67. let equipItem = find("equipItem", itemUI);
  68. let skillItem = find("skillItem", itemUI);
  69. equipItem.active = false;
  70. skillItem.active = false;
  71. let hasBuyNode = null;
  72. if (this.type == constants.shopTypes.equip || this.type == constants.shopTypes.secret) {
  73. equipItem.active = true;
  74. hasBuyNode = find("hasBuy", equipItem);
  75. this.setString(find("atk/val", equipItem), "+" + item.atk);
  76. let equipRow = designManager.instance.getRowById(constants.tableName.equip, item.equipId);
  77. iconManager.instance.setSprite(find("icon", equipItem), equipRow.icon);
  78. } else if (this.type == constants.shopTypes.skill) {
  79. skillItem.active = true;
  80. hasBuyNode = find("hasBuy", skillItem);
  81. let skillRow = designManager.instance.getRowById(constants.tableName.skill, item.skillId);
  82. iconManager.instance.setSprite(find("icon", skillItem), skillRow.icon);
  83. this.setString(find("info/info", skillItem), skillRow.info);
  84. }
  85. let btnBuy = find("btnBuy", itemUI);
  86. this.setString(find("price", btnBuy), -item.price);
  87. btnBuy["item"] = item;
  88. if (item.hasBuy) {
  89. btnBuy.active = false;
  90. hasBuyNode.active = true;
  91. } else {
  92. btnBuy.active = true;
  93. hasBuyNode.active = false;
  94. }
  95. }
  96. initEffect() {
  97. cocosUtil.tweenScaleBreath(this.btnCoinAdd);
  98. }
  99. initLocaltext() {
  100. }
  101. getShopArr() {
  102. let arr = [];
  103. if (this.type == constants.shopTypes.equip) {
  104. // 装备商店
  105. let row = designManager.instance.getRowById(constants.tableName.shop_equip, this.id);
  106. for (let i in row.idArr) {
  107. let id = row.idArr[i];
  108. let info: any = {};
  109. info.equipId = id;
  110. info.price = row.priceArr[i];
  111. info.atk = row.atkArr[i];
  112. arr.push(info);
  113. }
  114. } else if (this.type == constants.shopTypes.secret) {
  115. // 神秘商店
  116. let tb = designManager.instance.getTable(constants.tableName.shop_secret);
  117. arr = utilTools.getRowsByWeightAndNum(tb, 3);
  118. } else if (this.type == constants.shopTypes.skill) {
  119. // 技能商店
  120. let tb = designManager.instance.getTable(constants.tableName.shop_skill);
  121. let tmpArr = [];
  122. for (let i in tb) {
  123. let row = tb[i];
  124. if (levelManager.instance.level % 2 == 0) {
  125. if (row.skillId == 1 || row.skillId == 3) {
  126. tmpArr.push(row);
  127. }
  128. continue;
  129. }
  130. if (row.skillId == 2 || row.skillId == 4) {
  131. tmpArr.push(row);
  132. }
  133. }
  134. tb = tmpArr;
  135. arr = utilTools.getRowsByWeightAndNum(tb, 3);
  136. }
  137. return arr;
  138. };
  139. coinChangeRet(data: any) {
  140. this.refreshCoinLayer();
  141. }
  142. process_event(event: any) {
  143. let ac = event.ac;
  144. let data = event.data;
  145. switch (ac) {
  146. case msgac.coinChange:
  147. this.coinChangeRet(data);
  148. break;
  149. default:
  150. break;
  151. }
  152. }
  153. onButtonClick(node: Node, name: string) {
  154. switch (name) {
  155. case "btnBuy":
  156. this.onClickBtnBuy(node);
  157. break;
  158. case "btnCoinAdd":
  159. this.onClickBtnCoinAdd(node);
  160. break;
  161. case "btnNext":
  162. this.onClickBtnNext(node);
  163. break;
  164. default:
  165. break;
  166. }
  167. }
  168. onClickBtnBuy(node: Node) {
  169. let item = node["item"];
  170. if (playerModel.instance.getCoin() < item.price) {
  171. this.createNotice(localText.textObj.lackCoin);
  172. return;
  173. }
  174. item.hasBuy = true;
  175. playerModel.instance.subCoin(item.price);
  176. this.shopLayer.getComponent(ItemLayer).refreshUI();
  177. if (this.type == constants.shopTypes.equip || this.type == constants.shopTypes.secret) {
  178. levelManager.instance.addAtk(item.atk);
  179. levelManager.instance.addEquip(item.equipId);
  180. audioManager.instance.playEffect(constants.audioNames.equip);
  181. node["hasPlayAudio"] = true;
  182. } else if (this.type == constants.shopTypes.skill) {
  183. playerModel.instance.addSkillNumById(item.skillId);
  184. eventManager.instance.send(msgac.skillLayerRefresh);
  185. }
  186. }
  187. onClickBtnNext(node: Node) {
  188. this.closeLayer();
  189. sdkManager.instance.showCustomAdCommon();
  190. }
  191. onClickBtnCoinAdd(node: Node) {
  192. sdkManager.instance.sendEvent("观看激励视频-领取金币500");
  193. sdkManager.instance.openAd((st: number) => {
  194. if (st != 1) {
  195. return;
  196. }
  197. this.btnCoinAdd.active = false;
  198. playerModel.instance.addCoin(levelManager.instance.shopAdCoin);
  199. this.openCoinGetEffectLayer(node);
  200. sdkManager.instance.sendEvent("观看完激励视频-领取金币500");
  201. });
  202. }
  203. }