CurrencyUI.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /** @format */
  2. import {UI} from '../../enums/UI'
  3. import {BaseUI} from '../BaseUI'
  4. import {node, observer} from '../../mobx/observer'
  5. import {Data, Mgr} from '../../GameControl'
  6. import {EVENT, GOODS, MOD} from '../../enums/Enum'
  7. import {ccUtils} from '../../utils/ccUtils'
  8. import {GoodsConfig} from '../../config/GoodsConfig'
  9. const {ccclass, property} = cc._decorator
  10. @ccclass
  11. @observer
  12. export class CurrencyUI extends BaseUI {
  13. @node('goods/item')
  14. item: cc.Node
  15. goodsIDs: number[]
  16. items: cc.Node[]
  17. onShow(args, fromUI: number) {
  18. let isShow = false
  19. Mgr.goods.goodsEndPosMap.clear()
  20. this.goodsIDs = args
  21. let nodes, node
  22. nodes = this.items = ccUtils.instantChildren(this.item, this.goodsIDs?.length)
  23. this.item.parent.getComponent(cc.Layout).updateLayout()
  24. for (let i = 0; i < nodes.length; i++) {
  25. node = nodes[i]
  26. let goodID = this.goodsIDs[i]
  27. this.items[i]['id'] = goodID
  28. let cfg = GoodsConfig[goodID]
  29. this[`_${cfg.icon}`] = Mgr.goods.getGoodsNum(goodID)
  30. ccUtils.setLabel(Math.toKMBNum(Mgr.goods.getGoodsNum(goodID)), node, 'lb')
  31. Mgr.goods.goodsEndPosMap.set(goodID, ccUtils.convertToWorldSpaceCanvasAR(cc.find('lb', node)))
  32. //移除正在飞行的道具回调,避免重复增加
  33. let cbArr = Mgr.goods.flyGoodsCBMap.get(goodID)
  34. if (cbArr) cbArr.length = 0
  35. this.loadTexImg(`Public/goods/${cfg.icon}`, node, 'icon')
  36. }
  37. Mgr.event.add(EVENT.goodsChangeSync, this, this.showCurrency)
  38. }
  39. onHide(): any {
  40. Mgr.goods.goodsEndPosMap.clear()
  41. Mgr.event.removeAll(this)
  42. }
  43. //网络事件=======================================
  44. //触发事件=======================================
  45. showCurrency(goodID: number, changeNum: number) {
  46. let item = this.items[this.goodsIDs.indexOf(goodID)]
  47. if (!item) return
  48. let key: string = GoodsConfig[goodID]?.icon
  49. if (!key) {
  50. console.error('CurrencyUI 道具表没有该道具')
  51. }
  52. this[`_${key}`] += changeNum
  53. ccUtils.setLabel(Math.toKMBNum(this[`_${key}`]), item, 'lb')
  54. }
  55. // 点击事件=======================================
  56. onAddClick(event) {
  57. let type = event.target.parent['id']
  58. switch (type) {
  59. case GOODS.fatigue:
  60. Mgr.global.tryJumpMod(MOD.fatigueBuy)
  61. break
  62. case GOODS.coin:
  63. Mgr.global.tryJumpMod(MOD.moneyBuy)
  64. break
  65. case GOODS.diamond:
  66. Mgr.global.tryJumpMod(MOD.resourceStore)
  67. break
  68. case GOODS.roleSoulStone:
  69. case GOODS.equipRuneStones:
  70. case GOODS.normalDrawScroll:
  71. case GOODS.rarelDrawScroll:
  72. Mgr.global.tryJumpMod(MOD.dailyGift)
  73. break
  74. case GOODS.launchLotteryCoin:
  75. Mgr.event.trigger(EVENT.openLotteryTask)
  76. break
  77. default:
  78. Mgr.global.tryJumpMod(MOD.dailyGift)
  79. break
  80. }
  81. }
  82. }