StoreUI.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 { Utils } from '../utils/Utils';
  7. import MsgHints from '../utils/MsgHints';
  8. import { StoreItem } from '../items/item/StoreItem';
  9. import { Constants } from '../data/Constants';
  10. import { stateMgr } from '../core/manager/StateManager';
  11. import i18n from '../core/i18n/runtime-scripts/LanguageData';
  12. import PlatformSystem from '../platform/PlatformSystem';
  13. import { ITEM_TYPE, ResUtil } from '../utils/ResUtil';
  14. import { audioMgr } from '../core/manager/AudioManager';
  15. const { ccclass, property } = _decorator;
  16. @ccclass('StoreUI')
  17. export class StoreUI extends BaseExp {
  18. @autoBind({ type: List, tooltip: "任务数据" })
  19. public coin_scrollView: List;
  20. @autoBind({ type: List, tooltip: "任务数据" })
  21. public diamond_scrollView: List;
  22. @autoBind({ type: Label, tooltip: "金币文本" })
  23. public gold_lable: Label;
  24. @autoBind({ type: Label, tooltip: "钻石文本" })
  25. public diamond_lable: Label;
  26. @autoBind({ type: Label, tooltip: "金币购买的list标题" })
  27. public coin_title_lable: Label;
  28. @autoBind({ type: Label, tooltip: "钻石购买的list标题" })
  29. public diamond_title_lable: Label;
  30. //金币数据
  31. private coinList:Array<any> = [];
  32. //钻石数据
  33. private diamondList:Array<any> = [];
  34. start() {
  35. this.hasAnim = false;
  36. this.closeOnBlank = false;
  37. //注册动态变化值
  38. stateMgr.registerUI(Constants.gold, this.gold_lable);
  39. stateMgr.registerUI(Constants.diamond, this.diamond_lable);
  40. }
  41. public show(...args: any[]){
  42. this.loadStoreData();
  43. }
  44. /**
  45. * 加载商店数据
  46. */
  47. public loadStoreData(){
  48. //重置所有广告次数
  49. const datas = Utils.clone(userIns.shopTable).map(e => ({ ...e, alreadyVideo: 0 }));
  50. this.coinList = datas.filter(e=>e.type === 1);
  51. if(this.coinList.length > 0){
  52. let coin = this.coinList[0];
  53. this.coin_title_lable.string = i18n.isZh ? coin.name : coin.name_lang;
  54. }
  55. this.coin_scrollView.numItems = this.coinList.length;
  56. this.diamondList = datas.filter(e=>e.type === 2);
  57. this.diamond_scrollView.numItems = this.diamondList.length;
  58. if(this.diamondList.length > 0){
  59. let diamond = this.diamondList[0];
  60. this.diamond_title_lable.string = i18n.isZh ? diamond.name : diamond.name_lang;
  61. }
  62. }
  63. /**
  64. * 设置金币购买的数据
  65. * @param item item节点
  66. * @param idx 数据信息
  67. */
  68. public setCoinItemData(item: Node, idx: number) {
  69. let com:StoreItem = item.getComponent(StoreItem);
  70. com.init(this.coinList[idx],(data:any,n: Node)=>{
  71. this.clicked(data,n)
  72. });
  73. }
  74. /**
  75. * 设置钻石购买的数据
  76. * @param item item节点
  77. * @param idx 数据信息
  78. */
  79. public setDiamondItemData(item: Node, idx: number) {
  80. let com:StoreItem = item.getComponent(StoreItem);
  81. com.init(this.diamondList[idx],this.clicked.bind(this));
  82. }
  83. /**
  84. * 看广告
  85. * @param data 看广告数据
  86. * @param clikTarget 道具节点
  87. */
  88. public clicked(data:any,clikTarget: Node){
  89. audioMgr.playOneShot(Constants.audios.click);
  90. const num: number = Number(data.price_2);
  91. PlatformSystem.platform.showRewardVideo((f) => {
  92. if(f) {
  93. data.alreadyVideo += 1;
  94. if(data.alreadyVideo >= num){
  95. data.alreadyVideo = 0;
  96. //达到看视频的次数 获得奖励
  97. if(data.type == 1){
  98. audioMgr.playOneShot(Constants.audios.buy);
  99. userIns.data.gold += data.quantity;
  100. ResUtil.flyAnim(ITEM_TYPE.Coin, clikTarget, this.gold_lable.node, 5, 50,(b) => {});
  101. }else if(data.type == 2){
  102. audioMgr.playOneShot(Constants.audios.buy);
  103. userIns.data.diamond += data.quantity;
  104. ResUtil.flyAnim(ITEM_TYPE.Diamond, clikTarget, this.diamond_lable.node, 5, 50,(b) => {});
  105. }
  106. }else{
  107. MsgHints.show(`Watch ${num - data.alreadyVideo} more times to purchase successfully!`);
  108. }
  109. if(data.type === 1){
  110. this.coin_scrollView.updateAll();
  111. }else if(data.type === 2){
  112. this.diamond_scrollView.updateAll();
  113. }
  114. }
  115. });
  116. }
  117. }