Water.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { _decorator, BoxCollider2D, Button, CircleCollider2D, Collider2D, Color, Component, Enum, find, Node, NodeEventType, sp, Sprite } from 'cc';
  2. import { WaterColorHex, WaterColors } from '../TakeGobletGlobalInstance';
  3. import { OriginCupState } from './OriginCup';
  4. const { ccclass, property, executeInEditMode } = _decorator;
  5. @ccclass('Water')
  6. @executeInEditMode
  7. export class Water extends Component {
  8. private _color: WaterColors = WaterColors.Blue;
  9. skeleton: sp.Skeleton = null!;
  10. @property({ type: Sprite })
  11. sprite: Sprite = null!;
  12. @property({ type: Enum(WaterColors) })
  13. get color() {
  14. return this._color;
  15. }
  16. set color(value: WaterColors) {
  17. this._color = value;
  18. this.updateColor();
  19. }
  20. private updateColor() {
  21. let color = new Color(WaterColorHex[this.color]);
  22. const target = this.skeleton ?? this.sprite;
  23. if (target) {
  24. target.color = color;
  25. }
  26. }
  27. start() {
  28. this.skeleton = this.node.getComponent(sp.Skeleton)!;
  29. this.sprite = this.node.getChildByName('sprite')?.getComponent(Sprite)!;
  30. this.updateColor();
  31. }
  32. initColor(color: WaterColors) {
  33. this._color = color;
  34. this.updateColor();
  35. }
  36. //播放动画根据状态
  37. playAnimation(state: OriginCupState, index?: number) {
  38. switch (state) {
  39. case OriginCupState.Up:
  40. this.playUpAnimation();
  41. break;
  42. case OriginCupState.PourWater:
  43. this.playPourWaterAnimation(index);
  44. break;
  45. default:
  46. break;
  47. }
  48. }
  49. playUpAnimation() {
  50. if (!this.skeleton) return;
  51. this.skeleton.setAnimation(0, 'pour_idle', false);
  52. }
  53. playPourWaterAnimation(index: number) {
  54. if (!this.skeleton) return;
  55. console.log(`动画名:pour_0${index} -- index:${index}`);
  56. this.skeleton.setAnimation(0, `pour_0${index}`, false);
  57. }
  58. }