|
|
@@ -11,61 +11,74 @@ export default class Animals extends cc.Component {
|
|
|
ani: cc.Animation = null;
|
|
|
|
|
|
clipArr: Array<cc.AnimationClip> = new Array<cc.AnimationClip>();
|
|
|
- private _state: AnimalState = AnimalState.Wait;
|
|
|
+ private _state: AnimalState = -1;
|
|
|
|
|
|
- curTargetPoint = null;
|
|
|
- speed = 1;
|
|
|
+ configID = 0;
|
|
|
+ curTargetPoint: cc.Vec2 = null;
|
|
|
+ angle = null;
|
|
|
+ speed = 0.5;
|
|
|
walkTotleFarme = 0;
|
|
|
isWalk = false;
|
|
|
+ pointIndex = -1;
|
|
|
|
|
|
start() {
|
|
|
- this.clipArr = this.ani.getClips();
|
|
|
this.curTargetPoint = this.node.getPosition();
|
|
|
this.getNextPoint();
|
|
|
}
|
|
|
|
|
|
- setState(state) {
|
|
|
+ setState(state, configID) {
|
|
|
+ this.configID = configID;
|
|
|
if (this._state == state) {
|
|
|
return;
|
|
|
}
|
|
|
this._state = state;
|
|
|
+ let clipName = ''
|
|
|
switch (this._state) {
|
|
|
case AnimalState.Hanger:
|
|
|
- this.ani.defaultClip = this.ani[0];
|
|
|
+ clipName = this.configID + '_' + 0;
|
|
|
break;
|
|
|
case AnimalState.Eat:
|
|
|
- this.ani.defaultClip = this.ani[2];
|
|
|
+ clipName = this.configID + '_' + 2;
|
|
|
break;
|
|
|
case AnimalState.Wait:
|
|
|
- this.ani.defaultClip = this.ani[0];
|
|
|
+ clipName = this.configID + '_' + 1;
|
|
|
break;
|
|
|
}
|
|
|
- this.ani.play()
|
|
|
+ this.ani.play(clipName);
|
|
|
}
|
|
|
|
|
|
getNextPoint() {
|
|
|
let ran = Math.random() * 2 + 3;
|
|
|
this.scheduleOnce(() => {
|
|
|
- this.curTargetPoint = gData.pastureData.getCanMovePoint();
|
|
|
- let distance = cc.Vec2.distance(this.node.position, this.curTargetPoint);
|
|
|
+ this.pointIndex = gData.pastureData.getCanMoveIndex(this.pointIndex);
|
|
|
+ this.curTargetPoint = gData.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;
|
|
|
- this.ani.defaultClip = this.ani[1];
|
|
|
- this.ani.play()
|
|
|
+ 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 angle = Math.atan2(this.curTargetPoint.y - this.node.position.y, this.curTargetPoint.x - this.node.position.x);
|
|
|
- let x = Math.cos(angle) * this.speed;
|
|
|
- let y = Math.sin(angle) * this.speed;
|
|
|
- this.node.setPosition(cc.v2(x, y));
|
|
|
+ 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.setState(this._state, this.configID);
|
|
|
+ this.getNextPoint();
|
|
|
}
|
|
|
}
|
|
|
}
|