CoinItem.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { _decorator, Component, Input, input, Label, math, Node, Sprite, tween, v3 } from 'cc';
  2. import { Bundle } from '../enum/Bundle';
  3. import { EventType } from '../enum/EventType';
  4. import { AudioMgr } from '../manager/AudioMgr';
  5. import { EventMgr } from '../manager/EventMgr';
  6. import { ResMgr } from '../manager/ResMgr';
  7. import { LabelTick } from '../uiExtend/LabelTick';
  8. const { ccclass, property } = _decorator;
  9. @ccclass('UIItem/CoinItem')
  10. export class CoinItem extends Component {
  11. @property(LabelTick)
  12. private labelTick: LabelTick = null
  13. private coin: number = 0
  14. public get Coin() : number {
  15. return this.coin
  16. }
  17. protected onLoad(): void {
  18. EventMgr.emit(EventType.CoinItemShow, this)
  19. EventMgr.on(EventType.CoinItemShow, this.onShow, this)
  20. EventMgr.on(EventType.CoinItemHide, this.onHide, this)
  21. EventMgr.on(EventType.UpdateCoin, this.updateCoin, this)
  22. }
  23. protected onDisable(): void {
  24. this.unscheduleAllCallbacks()
  25. }
  26. protected onDestroy(): void {
  27. EventMgr.emit(EventType.CoinItemHide, this)
  28. this.unscheduleAllCallbacks()
  29. EventMgr.off(EventType.UpdateCoin, this.updateCoin, this)
  30. EventMgr.off(EventType.CoinItemShow, this.onShow, this)
  31. EventMgr.off(EventType.CoinItemHide, this.onHide, this)
  32. }
  33. private onShow(other: CoinItem): void {
  34. if (this === other) return
  35. this.node.active = false
  36. }
  37. private onHide(other: CoinItem): void {
  38. if (this === other) return
  39. this.node.active = true
  40. this.init(other.coin)
  41. }
  42. public init(coin: number): void {
  43. this.coin = coin
  44. this.labelTick.init(coin)
  45. }
  46. public updateCoin(coin: number, playAnim: boolean = true): void {
  47. if (!this.node.active) return
  48. this.coin = coin
  49. this.labelTick.tickLabel(coin)
  50. const rangeX: number = 50
  51. const rangeY: number = 100
  52. if (playAnim) {
  53. AudioMgr.playSfx('获得金币')
  54. this.schedule(() => {
  55. const flyCoin: Node = this.createFlyCoin()
  56. const posX: number = math.randomRangeInt(-rangeX, rangeX)
  57. const posY: number = math.randomRangeInt(-rangeY, -50)
  58. const tw = tween(flyCoin)
  59. tw.to(0.5, { position: v3(posX, posY) }, { easing: 'cubicOut' })
  60. tw.to(1, { position: this.node.position }, { easing: 'backIn' })
  61. tw.call(() => {
  62. AudioMgr.playSfx('获得金币1')
  63. flyCoin.destroy()
  64. })
  65. tw.start()
  66. }, 0.1, 5)
  67. }
  68. }
  69. public createFlyCoin(): Node {
  70. const flyCoin: Node = new Node()
  71. const sp: Sprite = flyCoin.addComponent(Sprite)
  72. sp.spriteFrame = ResMgr.getSpriteFrame(Bundle.UI, 'xingxing', 'common/')
  73. this.node.parent.addChild(flyCoin)
  74. return flyCoin
  75. }
  76. }