TempCup.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { _decorator, Component, Enum, Node } from 'cc';
  2. import { CupHeight, WaterColors } from '../TakeGobletGlobalInstance';
  3. import { Water } from './Water';
  4. const { ccclass, property } = _decorator;
  5. @ccclass('TempCup')
  6. export class TempCup extends Component {
  7. @property({ type: Enum(CupHeight), displayName: '杯高度' })
  8. cupHeight: CupHeight = CupHeight.One
  9. @property(Node)
  10. waters: Node = null!; //水节点
  11. private _currentColor: WaterColors = WaterColors.Blue;
  12. private _isFull: boolean = false;
  13. get isFull(): boolean {
  14. return this._isFull;
  15. }
  16. get currentColor(): WaterColors {
  17. return this._currentColor;
  18. }
  19. start() {
  20. }
  21. update(deltaTime: number) {
  22. }
  23. fill(color: WaterColors) {
  24. if (this._isFull) return;
  25. const waterNode = this.waters.children[0];
  26. if (waterNode) {
  27. const water = waterNode.getComponent(Water)!;
  28. water.color = color;
  29. waterNode.active = true;
  30. this._currentColor = color;
  31. this._isFull = true;
  32. }
  33. }
  34. reset() {
  35. this.waters.children[0].active = false;
  36. this._isFull = false;
  37. console.log('暂存杯已重置');
  38. }
  39. public getColors(): WaterColors[] {
  40. return this.waters.children
  41. .filter(node => node.active)
  42. .map(node => node.getComponent(Water)!.color);
  43. }
  44. public getActiveWaters(): Water[] {
  45. return this.waters.children
  46. .filter(node => node.active)
  47. .map(node => node.getComponent(Water)!);
  48. }
  49. public isEmpty(): boolean {
  50. return this.waters.children.every(node => !node.active);
  51. }
  52. }