CocktailCup.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. TakeGobletAudioMgr.playOneShot(TakeGobletAudioMgr.getMusicIdName(5), 1.0);
  58. this.playAnimationByState(CocktailCupState.Full);
  59. await GameUtil.delay(0.5);
  60. // 补满后执行移动销毁动画
  61. const uiTransform = this.node.getComponent(UITransform)!;
  62. const screenWidth = view.getVisibleSize().width;
  63. const targetX = screenWidth + uiTransform.width * 1.5;
  64. await new Promise<void>(resolve => {
  65. tween(this.node)
  66. .to(0.5, { position: new Vec3(targetX, 0, 0) }, { easing: 'sineIn' })
  67. .call(async () => {
  68. const outArea = this.node.parent?.parent?.getComponent(OutArea);
  69. if (outArea) {
  70. this.node.removeFromParent(); // 先从父节点移除
  71. this.node.destroy();
  72. await outArea.arrangeCups(); // 等待排列完成
  73. }
  74. resolve();
  75. })
  76. .start();
  77. });
  78. }
  79. //播放动画
  80. playAnimationByState(state: CocktailCupState) {
  81. const waters = this.waters.children;
  82. const siblingNodes = this.node.children.filter(child => child.name !== 'waters');
  83. const allNodes = [...waters, ...siblingNodes];
  84. switch (state) {
  85. case CocktailCupState.GetWater:
  86. allNodes.forEach(child => child.getComponent(sp.Skeleton)!.setAnimation(0, `pour_0${this.currentLayers}`, false));
  87. break;
  88. case CocktailCupState.Move:
  89. allNodes.forEach(child => child.getComponent(sp.Skeleton)!.setAnimation(0, 'move', false));
  90. break;
  91. case CocktailCupState.Full:
  92. allNodes.forEach(child => child.getComponent(sp.Skeleton)!.setAnimation(0, 'full', false));
  93. break;
  94. default:
  95. allNodes.forEach(child => child.getComponent(sp.Skeleton)!.setAnimation(0, 'idle', false));
  96. break;
  97. }
  98. }
  99. // 清空水层(消除时调用)
  100. reset() {
  101. this.currentLayers = 0;
  102. }
  103. get isFull(): boolean {
  104. return this.currentLayers >= this.cupHeight;
  105. }
  106. private async checkAndDestroy() {
  107. if (this.isFull) {
  108. await this.fillUp();
  109. }
  110. }
  111. public get currentColor(): WaterColors {
  112. return this.cupColor;
  113. }
  114. public get remainingCapacity(): number {
  115. return this.cupHeight - this.currentLayers;
  116. }
  117. }