Goods.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import { _decorator, color, Color, Component, EventTouch, Node, Prefab, Sprite, Tween, tween, UIOpacity, v3, Vec2, Vec3 } from 'cc';
  2. import { GameMgr } from '../manager/GameMgr';
  3. import { ResMgr } from '../manager/ResMgr';
  4. import { Bundle } from '../enum/Bundle';
  5. import { EffectMgr } from '../manager/EffectMgr';
  6. import { Mode } from '../enum/Mode';
  7. const { ccclass, property } = _decorator;
  8. @ccclass('Game/Goods')
  9. export class Goods extends Component {
  10. private uiOpacity: UIOpacity = null
  11. private sp: Sprite = null
  12. public get Sp(): Sprite {
  13. return this.sp
  14. }
  15. @property(Prefab)
  16. private parMergePre: Prefab = null
  17. private visible: boolean = true
  18. public get Visible(): boolean {
  19. return this.visible
  20. }
  21. public set Visible(v: boolean) {
  22. this.visible = v
  23. this.uiOpacity = this.getComponent(UIOpacity)
  24. this.uiOpacity.opacity = this.visible ? 255 : 0
  25. }
  26. private id: number = 0
  27. public get Id(): number {
  28. return this.id
  29. }
  30. public set Id(v: number) {
  31. this.id = v
  32. switch (GameMgr.mode) {
  33. case Mode.tp:
  34. this.sp.spriteFrame = ResMgr.getSpriteFrame(Bundle.Icon, `tp${v}`, 'tp/')
  35. break;
  36. case Mode.gs:
  37. this.sp.spriteFrame = ResMgr.getSpriteFrame(Bundle.Icon, `gs${v}`, 'gs/')
  38. break;
  39. case Mode.ls:
  40. this.sp.spriteFrame = ResMgr.getSpriteFrame(Bundle.Icon, `ls${v}`, 'ls/')
  41. break;
  42. }
  43. }
  44. private layer: number = 0
  45. public get Layer(): number {
  46. return this.layer
  47. }
  48. public set Layer(v: number) {
  49. this.layer = v
  50. const isFront: boolean = this.layer === 0
  51. this.sp.color = isFront ? Color.WHITE : color(100, 100, 100, 200)
  52. }
  53. private slot: number = 0
  54. public get Slot(): number {
  55. return this.slot
  56. }
  57. public set Slot(v: number) {
  58. this.slot = v
  59. }
  60. protected onLoad(): void {
  61. this.uiOpacity = this.getComponent(UIOpacity)
  62. this.sp = this.getComponentInChildren(Sprite)
  63. this.node.on(Node.EventType.TOUCH_START, this.onTouchStart, this)
  64. }
  65. protected onDestroy(): void {
  66. this.node.off(Node.EventType.TOUCH_START, this.onTouchStart, this)
  67. }
  68. private onTouchStart(e: EventTouch): void {
  69. if (this.layer !== 0) return
  70. if (GameMgr.isPicking) return
  71. const uiPos: Vec2 = e.getUILocation()
  72. GameMgr.pick(this, uiPos)
  73. this.Visible = false
  74. }
  75. public remove(): void {
  76. this.node.off(Node.EventType.TOUCH_START, this.onTouchStart, this)
  77. const tw = tween(this.sp.node)
  78. tw.to(0.15, { scale: v3(1.1, 0.9, 1) })
  79. tw.to(0.15, { scale: v3(0.9, 1.1, 1) })
  80. tw.call(() => {
  81. if (this.layer === 0) {
  82. const worldPos: Vec3 = this.node.getWorldPosition()
  83. worldPos.y -= 30
  84. EffectMgr.create(this.parMergePre, GameMgr.Stage, worldPos)
  85. }
  86. this.node.destroy()
  87. })
  88. tw.start()
  89. }
  90. public bounce(delay: number = 0): void {
  91. tween(this.sp.node).delay(delay).to(0.1, { scale: v3(0.9, 1.1, 1) }).to(0.1, { scale: v3(1.1, 0.9, 1) }).to(0.1, { scale: v3(1, 1, 1) }).start()
  92. }
  93. }