CocktailCup.ts 2.1 KB

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