123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- import { _decorator, Color, Component, Enum, Node, Sprite } from 'cc';
- import { CupHeight, WaterColorHex, WaterColors } from '../TakeGobletGlobalInstance';
- import { Water } from './Water';
- 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!; //水节点
- 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(color?: WaterColors) {
- if (this.currentLayers >= this.cupHeight) return;
- // 从下往上激活水层(假设waters子节点0是最底层)
- const waterNode = this.waters.children[this.currentLayers];
- if (waterNode) {
- if (color) {
- const water = waterNode.getComponent(Water);
- if (water) water.color = color;
- }
- waterNode.active = true;
- this.currentLayers++;
- }
- }
- // 清空水层(消除时调用)
- reset() {
- this.waters.children.forEach(water => water.active = false);
- this.currentLayers = 0;
- }
- isFull(): boolean {
- return this.currentLayers >= this.cupHeight;
- }
- public get currentColor(): WaterColors {
- return this.cupColor;
- }
- public get remainingCapacity(): number {
- return this.cupHeight - this.currentLayers;
- }
- }
|