| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- /** 动物挂载脚本 */
- import { AnimalState } from "../../game/data/GameData";
- const { ccclass, property } = cc._decorator;
- @ccclass
- export default class Animals extends cc.Component {
- @property(cc.Animation)
- ani: cc.Animation = null;
- clipArr: Array<cc.AnimationClip> = new Array<cc.AnimationClip>();
- private _state: AnimalState = -1;
- configID = 0;
- curTargetPoint: cc.Vec2 = null;
- angle = null;
- speed = 0.5;
- walkTotleFarme = 0;
- isWalk = false;
- pointIndex = -1;
- pastureData = null;
- start() {
- this.curTargetPoint = this.node.getPosition();
- this.getNextPoint();
- }
- setState(state, configID) {
- this.configID = configID;
- if (!this.pastureData) {
- this.pastureData = gData.pastureSystem.getPastureData(configID);
- }
- if (this._state == state) {
- return;
- }
- this._state = state;
- let clipName = ''
- switch (this._state) {
- case AnimalState.Hanger:
- clipName = this.configID + '_' + 0;
- break;
- case AnimalState.Eat:
- clipName = this.configID + '_' + 1;
- break;
- case AnimalState.Wait:
- clipName = this.configID + '_' + 2;
- break;
- }
- this.ani.play(clipName);
- }
- getNextPoint() {
- let ran = Math.random() * 2 + 3;
- this.scheduleOnce(() => {
- this.pointIndex = this.pastureData.getCanMoveIndex(this.pointIndex);
- this.curTargetPoint = this.pastureData.getCanMoveIndexPoint(this.pointIndex);
- let distance = this.node.getPosition().sub(this.curTargetPoint).mag();
- this.angle = Math.atan2(this.curTargetPoint.y - this.node.position.y, this.curTargetPoint.x - this.node.position.x);
- this.walkTotleFarme = distance / this.speed;
- let clipName = this.configID + '_' + 1;
- this.node.angle = this.angle;
- if (this.curTargetPoint.x > this.node.position.x) {
- this.node.scaleX = -0.3;
- }
- else {
- this.node.scaleX = 0.3;
- }
- this.ani.play(clipName);
- this.isWalk = true;
- }, ran)
- }
- update(dt) {
- if (this.isWalk) {
- let x = Math.cos(this.angle) * this.speed;
- let y = Math.sin(this.angle) * this.speed;
- this.node.setPosition(this.node.getPosition().add(cc.v2(x, y)));
- this.walkTotleFarme--;
- if (this.walkTotleFarme <= 0) {
- this.isWalk = false;
- this.node.setPosition(this.curTargetPoint);
- this.setState(this._state, this.configID);
- this.getNextPoint();
- }
- }
- }
- }
|