CocktailCup.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import { _decorator, Color, Component, Enum, Node, Sprite, tween, Vec3, UITransform, view } 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. @ccclass('CocktailCup')
  11. @executeInEditMode
  12. export class CocktailCup extends Component {
  13. @property(Sprite)
  14. sprite: Sprite = null!
  15. @property({ type: Enum(CupHeight), displayName: '杯高度' })
  16. cupHeight: CupHeight = CupHeight.Two
  17. @property({ type: Enum(WaterColors) })
  18. private _cupColor: WaterColors = WaterColors.Blue
  19. @property({ type: Enum(WaterColors) })
  20. get cupColor() {
  21. return this._cupColor
  22. }
  23. set cupColor(value) {
  24. this._cupColor = value
  25. this.changeColor()
  26. }
  27. @property(Node)
  28. waters: Node = null!; //水节点
  29. currentLayers: number = 0;
  30. onLoad() {
  31. // 初始化时隐藏所有水层
  32. this.waters.children.forEach(water => water.active = false);
  33. }
  34. changeColor() {
  35. if (!this.sprite) return
  36. this.sprite.color = new Color(WaterColorHex[this._cupColor])
  37. }
  38. // 添加水层
  39. async addLayer(color: WaterColors) {
  40. const nextIndex = this.waters.children.filter(n => n.active).length;
  41. const waterNode = this.waters.children[nextIndex];
  42. if (waterNode) {
  43. const water = waterNode.getComponent(Water)!;
  44. water.color = color;
  45. waterNode.active = true;
  46. // 添加后检查是否满杯
  47. await this.checkAndDestroy(); // 等待销毁流程完成
  48. }
  49. }
  50. //补满水
  51. public async fillUp() {
  52. const currentActive = this.waters.children.filter(n => n.active).length;
  53. const layersToAdd = this.cupHeight - currentActive;
  54. for (let i = 0; i < layersToAdd; i++) {
  55. const waterIndex = currentActive + i;
  56. const waterNode = this.waters.children[waterIndex];
  57. const water = waterNode.getComponent(Water)!;
  58. water.color = this.cupColor;
  59. waterNode.active = true;
  60. waterNode.scale = Vec3.ZERO;
  61. await new Promise<void>(resolve => {
  62. tween(waterNode)
  63. .to(0.5 / layersToAdd, { scale: Vec3.ONE }, { easing: 'sineOut' })
  64. .call(() => resolve())
  65. .start();
  66. });
  67. }
  68. // 补满后执行移动销毁动画
  69. const uiTransform = this.node.getComponent(UITransform)!;
  70. const screenWidth = view.getVisibleSize().width;
  71. const targetX = screenWidth / 2 + uiTransform.width * 1.5;
  72. await new Promise<void>(resolve => {
  73. tween(this.node)
  74. .to(0.5, { position: new Vec3(targetX, 0, 0) }, { easing: 'sineIn' })
  75. .call(() => {
  76. this.node.destroy();
  77. resolve();
  78. })
  79. .start();
  80. });
  81. }
  82. // 清空水层(消除时调用)
  83. reset() {
  84. this.waters.children.forEach(water => water.active = false);
  85. this.currentLayers = 0;
  86. }
  87. get isFull(): boolean {
  88. return this.waters.children.every(node => node.active);
  89. }
  90. private async checkAndDestroy() {
  91. if (this.isFull) {
  92. await new Promise<void>(resolve => {
  93. tween(this.node)
  94. .to(0.3, { scale: Vec3.ZERO })
  95. .call(async () => {
  96. const outArea = this.node.parent?.parent?.getComponent(OutArea);
  97. if (outArea) {
  98. this.node.removeFromParent(); // 先从父节点移除
  99. this.node.destroy();
  100. await outArea.arrangeCups(); // 等待排列完成
  101. }
  102. resolve();
  103. })
  104. .start();
  105. });
  106. }
  107. }
  108. public get currentColor(): WaterColors {
  109. return this.cupColor;
  110. }
  111. public get remainingCapacity(): number {
  112. return this.cupHeight - this.waters.children.filter(n => n.active).length;
  113. }
  114. }