CocktailCup.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. const { ccclass, property, executeInEditMode } = _decorator;
  7. /** 调酒杯组件脚本*/
  8. @ccclass('CocktailCup')
  9. @executeInEditMode
  10. export class CocktailCup extends Component {
  11. @property(Sprite)
  12. sprite: Sprite = null!
  13. @property({ type: Enum(CupHeight), displayName: '杯高度' })
  14. cupHeight: CupHeight = CupHeight.Two
  15. @property({ type: Enum(WaterColors) })
  16. private _cupColor: WaterColors = WaterColors.Blue
  17. @property({ type: Enum(WaterColors) })
  18. get cupColor() {
  19. return this._cupColor
  20. }
  21. set cupColor(value) {
  22. this._cupColor = value
  23. this.changeColor()
  24. }
  25. @property(Node)
  26. waters: Node = null!; //水节点
  27. currentLayers: number = 0;
  28. onLoad() {
  29. // 初始化时隐藏所有水层
  30. this.waters.children.forEach(water => water.active = false);
  31. }
  32. changeColor() {
  33. if (!this.sprite) return
  34. this.sprite.color = new Color(WaterColorHex[this._cupColor])
  35. }
  36. // 添加水层
  37. addLayer(color: WaterColors) {
  38. const nextIndex = this.waters.children.filter(n => n.active).length;
  39. const waterNode = this.waters.children[nextIndex];
  40. if (waterNode) {
  41. const water = waterNode.getComponent(Water)!;
  42. water.color = color;
  43. waterNode.active = true;
  44. // 添加后检查是否满杯
  45. this.checkAndDestroy();
  46. }
  47. }
  48. // 清空水层(消除时调用)
  49. reset() {
  50. this.waters.children.forEach(water => water.active = false);
  51. this.currentLayers = 0;
  52. }
  53. get isFull(): boolean {
  54. return this.waters.children.every(node => node.active);
  55. }
  56. checkAndDestroy() {
  57. if (this.isFull) {
  58. tween(this.node)
  59. .to(0.3, { scale: Vec3.ZERO })
  60. .call(() => {
  61. this.node.destroy();
  62. EventDispatcher.instance.emit(GameEvent.EVENT_COCKTAIL_CUP_DESTROYED, this.node);
  63. })
  64. .start();
  65. }
  66. }
  67. public get currentColor(): WaterColors {
  68. return this.cupColor;
  69. }
  70. public get remainingCapacity(): number {
  71. return this.cupHeight - this.waters.children.filter(n => n.active).length;
  72. }
  73. }