CocktailCup.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { _decorator, Color, Component, Enum, Node, Sprite, tween, Vec3 } 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. reset() {
  51. this.waters.children.forEach(water => water.active = false);
  52. this.currentLayers = 0;
  53. }
  54. get isFull(): boolean {
  55. return this.waters.children.every(node => node.active);
  56. }
  57. private async checkAndDestroy() {
  58. if (this.isFull) {
  59. await new Promise<void>(resolve => {
  60. tween(this.node)
  61. .to(0.3, { scale: Vec3.ZERO })
  62. .call(async () => {
  63. // 先触发排列再销毁
  64. const outArea = this.node.parent?.parent?.getComponent(OutArea);
  65. if (outArea) {
  66. this.node.removeFromParent(); // 先从父节点移除
  67. await outArea.arrangeCups(); // 等待排列完成
  68. }
  69. this.node.destroy(); // 最后销毁节点
  70. resolve();
  71. })
  72. .start();
  73. });
  74. }
  75. }
  76. public get currentColor(): WaterColors {
  77. return this.cupColor;
  78. }
  79. public get remainingCapacity(): number {
  80. return this.cupHeight - this.waters.children.filter(n => n.active).length;
  81. }
  82. }