StoreUI.ts 4.4 KB

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