ItemView.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { _decorator, Component, Enum, Label, Node, Sprite } from 'cc';
  2. import { ItemKey, ItemService } from './ItemService';
  3. import { rootMgr } from '../../scene/RootMgr';
  4. const { ccclass, property } = _decorator;
  5. @ccclass('ItemView')
  6. export class ItemView extends Component {
  7. @property({ type: Enum(ItemKey) })
  8. readonly type: ItemKey
  9. @property(Label)
  10. label: Label
  11. @property(Node)
  12. labelIcon: Node
  13. @property(Sprite)
  14. adIcon: Sprite
  15. protected onEnable(): void {
  16. this.flshView(this.type)
  17. let item = rootMgr.dataControl?.getCompent(ItemService)
  18. item?.evt.on('useItem',this.flshView,this)
  19. }
  20. protected onDisable(): void {
  21. let item = rootMgr.dataControl?.getCompent(ItemService)
  22. item?.evt.off('useItem',this.flshView,this)
  23. }
  24. flshView(type: ItemKey) {
  25. if (type == this.type) {
  26. let number = rootMgr.dataControl.getCompent(ItemService).get(this.type)
  27. if (this.label) {
  28. this.label.string = number + ''
  29. }
  30. if (this.labelIcon) {
  31. this.labelIcon.active = number > 0
  32. }
  33. if (this.adIcon) {
  34. this.adIcon.node.active = number <= 0
  35. }
  36. }
  37. }
  38. }