Animal.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import { _decorator, Component, Node, Animation, v3, } from 'cc';
  2. import { TakeAWalk } from '../../public/TakeAWalk';
  3. const { ccclass, property } = _decorator;
  4. @ccclass('Animal')
  5. export class Animal extends Component {
  6. @property({ type: Animation, tooltip: "动画组件" }) animation: Animation;
  7. @property({ type: TakeAWalk, tooltip: "散步组件" }) walk: TakeAWalk;
  8. public state: AnimalState = AnimalState.Hungry;
  9. private curState: AnimalState = AnimalState.Hungry;
  10. start() {
  11. this.scheduleOnce(() => {
  12. this.animation.play(this.animation.clips[this.curState].name);
  13. }, this.random(0.5, 2));
  14. }
  15. private random(min: number, max: number): number {
  16. return min + Math.random() * (max - min);
  17. }
  18. update() {
  19. if (this.walk.curTargetPoint && this.walk.curTargetPoint.x > this.node.position.x) {
  20. this.node.setScale(v3(-0.3, 0.3, 1));
  21. } else {
  22. this.node.setScale(v3(0.3, 0.3, 1));
  23. }
  24. if (this.curState != this.state) {
  25. this.curState = this.state;
  26. this.animation.play(this.animation.clips[this.curState].name);
  27. if (this.curState != AnimalState.Wait) {
  28. this.walk.stop();
  29. } else {
  30. this.walk.walk();
  31. }
  32. }
  33. }
  34. }
  35. export enum AnimalState {
  36. Hungry,
  37. Wait,
  38. Out,
  39. None
  40. }