import { _decorator, Color, Component, Enum, Node, Sprite } from 'cc'; import { CupHeight, WaterColorHex, WaterColors } from '../TakeGobletGlobalInstance'; const { ccclass, property, executeInEditMode } = _decorator; /** 调酒杯组件脚本*/ @ccclass('CocktailCup') @executeInEditMode export class CocktailCup extends Component { @property(Sprite) sprite: Sprite = null! @property({ type: Enum(CupHeight), displayName: '杯高度' }) cupHeight: CupHeight = CupHeight.Two @property({ type: Enum(WaterColors) }) private _cupColor: WaterColors = WaterColors.Blue @property({ type: Enum(WaterColors) }) get cupColor() { return this._cupColor } set cupColor(value) { this._cupColor = value this.changeColor() } @property(Node) waters: Node = null!; //水节点 private currentLayers: number = 0; onLoad() { // 初始化时隐藏所有水层 this.waters.children.forEach(water => water.active = false); } changeColor() { if (!this.sprite) return this.sprite.color = new Color(WaterColorHex[this._cupColor]) } // 添加水层 addLayer() { if (this.currentLayers >= this.cupHeight) return; const waterNode = this.waters.children[this.currentLayers]; if (waterNode) { waterNode.active = true; this.currentLayers++; } } // 清空水层(消除时调用) reset() { this.waters.children.forEach(water => water.active = false); this.currentLayers = 0; } isFull(): boolean { return this.currentLayers >= this.cupHeight; } }