CocktailCup.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import { _decorator, Color, Component, Enum, Node, Sprite, tween, Vec3, UITransform, view, sp, Game } from 'cc';
  2. import { CupHeight, WaterColorHex, WaterColors } from '../TakeGobletGlobalInstance';
  3. import { Water } from './Water';
  4. import { EventDispatcher } from 'db://assets/core_tgx/easy_ui_framework/EventDispatcher';
  5. import { GameEvent } from '../Enum/GameEvent';
  6. import { OutArea } from './OutArea';
  7. import { GameUtil } from '../GameUtil';
  8. import { TakeGobletAudioMgr } from '../Manager/TakeGobletAudioMgr';
  9. const { ccclass, property, executeInEditMode } = _decorator;
  10. //调酒杯状态 默认 接水
  11. export enum CocktailCupState {
  12. Default,
  13. GetWater,
  14. Move,
  15. Full
  16. }
  17. /** 调酒杯组件脚本*/
  18. @ccclass('CocktailCup')
  19. @executeInEditMode
  20. export class CocktailCup extends Component {
  21. @property(Node)
  22. waters: Node = null! //水流节点
  23. @property(sp.Skeleton)
  24. colorB: sp.Skeleton = null! //水流颜色骨骼
  25. @property({ type: Enum(CupHeight), displayName: '杯高度' })
  26. cupHeight: CupHeight = CupHeight.Two
  27. @property({ type: Enum(WaterColors) })
  28. private _cupColor: WaterColors = WaterColors.Blue
  29. @property({ type: Enum(WaterColors) })
  30. get cupColor() {
  31. return this._cupColor
  32. }
  33. set cupColor(value) {
  34. this._cupColor = value
  35. this.changeColor()
  36. }
  37. currentLayers: number = 0;
  38. onLoad() {
  39. this.playAnimationByState(CocktailCupState.Default);
  40. }
  41. changeColor() {
  42. const color = new Color(WaterColorHex[this.cupColor]);
  43. this.waters.children.forEach((water, index) => {
  44. water.getComponent(sp.Skeleton)!.color = color;
  45. })
  46. this.colorB.color = color;
  47. }
  48. /** 添加图层 接水动画*/
  49. async addLayer() {
  50. this.currentLayers++;
  51. this.playAnimationByState(CocktailCupState.GetWater);
  52. await GameUtil.delay(0.4);
  53. await this.checkAndDestroy();
  54. }
  55. //补满水
  56. public async fillUp() {
  57. if (!this.node) return;
  58. TakeGobletAudioMgr.playOneShot(TakeGobletAudioMgr.getMusicIdName(5), 1.0);
  59. this.playAnimationByState(CocktailCupState.Full);
  60. await GameUtil.delay(0.5);
  61. // 补满后执行移动销毁动画
  62. const uiTransform = this.node.getComponent(UITransform)!;
  63. const screenWidth = view.getVisibleSize().width;
  64. const targetX = screenWidth + uiTransform.width * 1.5;
  65. await new Promise<void>(resolve => {
  66. tween(this.node)
  67. .to(0.5, { position: new Vec3(targetX, 0, 0) }, { easing: 'sineIn' })
  68. .call(async () => {
  69. const outArea = this.node.parent?.parent?.getComponent(OutArea);
  70. if (outArea) {
  71. this.node.removeFromParent(); // 先从父节点移除
  72. this.node.destroy();
  73. await outArea.arrangeCups(); // 等待排列完成
  74. }
  75. resolve();
  76. })
  77. .start();
  78. });
  79. }
  80. //播放动画
  81. playAnimationByState(state: CocktailCupState) {
  82. const waters = this.waters.children;
  83. const siblingNodes = this.node.children.filter(child => child.name !== 'waters');
  84. const allNodes = [...waters, ...siblingNodes];
  85. switch (state) {
  86. case CocktailCupState.GetWater:
  87. this.setSpineTimeScale(1.5);
  88. allNodes.forEach(child => child.getComponent(sp.Skeleton)!.setAnimation(0, `pour_0${this.currentLayers}`, false));
  89. break;
  90. case CocktailCupState.Move:
  91. allNodes.forEach(child => child.getComponent(sp.Skeleton)!.setAnimation(0, 'move', false));
  92. break;
  93. case CocktailCupState.Full:
  94. allNodes.forEach(child => child.getComponent(sp.Skeleton)!.setAnimation(0, 'full', false));
  95. break;
  96. default:
  97. allNodes.forEach(child => child.getComponent(sp.Skeleton)!.setAnimation(0, 'idle', false));
  98. break;
  99. }
  100. }
  101. //设置spine 时间缩放率
  102. setSpineTimeScale(timeScale: number = 1) {
  103. if (!this.node) return;
  104. const spineComponents = this.node.getComponentsInChildren(sp.Skeleton);
  105. spineComponents.forEach(spine => {
  106. spine.timeScale = timeScale;
  107. });
  108. }
  109. protected onDestroy(): void {
  110. EventDispatcher.instance.emit(GameEvent.EVENT_REFRESH_REMAIN_CUP);
  111. }
  112. // 清空水层(消除时调用)
  113. reset() {
  114. this.currentLayers = 0;
  115. }
  116. get isFull(): boolean {
  117. return this.currentLayers >= this.cupHeight;
  118. }
  119. private async checkAndDestroy() {
  120. if (this.isFull) {
  121. await this.fillUp();
  122. }
  123. }
  124. public get currentColor(): WaterColors {
  125. return this.cupColor;
  126. }
  127. public get remainingCapacity(): number {
  128. return this.cupHeight - this.currentLayers;
  129. }
  130. }