CocktailCup.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { _decorator, Color, Component, Enum, Node, Sprite } from 'cc';
  2. import { CupHeight, WaterColorHex, WaterColors } from '../TakeGobletGlobalInstance';
  3. const { ccclass, property, executeInEditMode } = _decorator;
  4. /** 调酒杯组件脚本*/
  5. @ccclass('CocktailCup')
  6. @executeInEditMode
  7. export class CocktailCup extends Component {
  8. @property(Sprite)
  9. sprite: Sprite = null!
  10. @property({ type: Enum(CupHeight), displayName: '杯高度' })
  11. cupHeight: CupHeight = CupHeight.Two
  12. @property({ type: Enum(WaterColors) })
  13. private _cupColor: WaterColors = WaterColors.Blue
  14. @property({ type: Enum(WaterColors) })
  15. get cupColor() {
  16. return this._cupColor
  17. }
  18. set cupColor(value) {
  19. this._cupColor = value
  20. this.changeColor()
  21. }
  22. @property(Node)
  23. waters: Node = null!; //水节点
  24. private currentLayers: number = 0;
  25. onLoad() {
  26. // 初始化时隐藏所有水层
  27. this.waters.children.forEach(water => water.active = false);
  28. }
  29. changeColor() {
  30. if (!this.sprite) return
  31. this.sprite.color = new Color(WaterColorHex[this._cupColor])
  32. }
  33. // 添加水层
  34. addLayer() {
  35. if (this.currentLayers >= this.cupHeight) return;
  36. const waterNode = this.waters.children[this.currentLayers];
  37. if (waterNode) {
  38. waterNode.active = true;
  39. this.currentLayers++;
  40. }
  41. }
  42. // 清空水层(消除时调用)
  43. reset() {
  44. this.waters.children.forEach(water => water.active = false);
  45. this.currentLayers = 0;
  46. }
  47. isFull(): boolean {
  48. return this.currentLayers >= this.cupHeight;
  49. }
  50. }