| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- import { _decorator, Component, Node, Animation, v3, } from 'cc';
- import { TakeAWalk } from '../../public/TakeAWalk';
- const { ccclass, property } = _decorator;
- @ccclass('Animal')
- export class Animal extends Component {
- @property({ type: Animation, tooltip: "动画组件" }) animation: Animation;
- @property({ type: TakeAWalk, tooltip: "散步组件" }) walk: TakeAWalk;
- public state: AnimalState = AnimalState.Hungry;
- private curState: AnimalState = AnimalState.Hungry;
- start() {
- this.scheduleOnce(() => {
- this.animation.play(this.animation.clips[this.curState].name);
- }, this.random(0.5, 2));
- }
- private random(min: number, max: number): number {
- return min + Math.random() * (max - min);
- }
- update() {
- if (this.walk.curTargetPoint && this.walk.curTargetPoint.x > this.node.position.x) {
- this.node.setScale(v3(-0.3, 0.3, 1));
- } else {
- this.node.setScale(v3(0.3, 0.3, 1));
- }
- if (this.curState != this.state) {
- this.curState = this.state;
- this.animation.play(this.animation.clips[this.curState].name);
- if (this.curState != AnimalState.Wait) {
- this.walk.stop();
- } else {
- this.walk.walk();
- }
- }
- }
- }
- export enum AnimalState {
- Hungry,
- Wait,
- Out,
- None
- }
|