CocktailCup.ts 4.3 KB

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