TopCell.ts 2.6 KB

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