CocktailCup.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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.5);
  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. allNodes.forEach(child => child.getComponent(sp.Skeleton)!.setAnimation(0, `pour_0${this.currentLayers}`, false));
  88. break;
  89. case CocktailCupState.Move:
  90. allNodes.forEach(child => child.getComponent(sp.Skeleton)!.setAnimation(0, 'move', false));
  91. break;
  92. case CocktailCupState.Full:
  93. allNodes.forEach(child => child.getComponent(sp.Skeleton)!.setAnimation(0, 'full', false));
  94. break;
  95. default:
  96. allNodes.forEach(child => child.getComponent(sp.Skeleton)!.setAnimation(0, 'idle', false));
  97. break;
  98. }
  99. }
  100. protected onDestroy(): void {
  101. EventDispatcher.instance.emit(GameEvent.EVENT_REFRESH_REMAIN_CUP);
  102. }
  103. // 清空水层(消除时调用)
  104. reset() {
  105. this.currentLayers = 0;
  106. }
  107. get isFull(): boolean {
  108. return this.currentLayers >= this.cupHeight;
  109. }
  110. private async checkAndDestroy() {
  111. if (this.isFull) {
  112. await this.fillUp();
  113. }
  114. }
  115. public get currentColor(): WaterColors {
  116. return this.cupColor;
  117. }
  118. public get remainingCapacity(): number {
  119. return this.cupHeight - this.currentLayers;
  120. }
  121. }