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