|
@@ -1,9 +1,12 @@
|
|
|
|
|
|
-import FSprite, { SpriteActionType, SpriteType } from "../FSprite";
|
|
|
+import CMath from "../../../../util/CMath";
|
|
|
+import { SpriteActionType } from "../FSprite";
|
|
|
import PSprite from "../PSprite";
|
|
|
+import SkillBase from "../skill/SkillBase";
|
|
|
+import SkillFixed from "../skill/SkillFixed";
|
|
|
import AIBase from "./AIBase";
|
|
|
|
|
|
-const {ccclass, property} = cc._decorator;
|
|
|
+const { ccclass, property } = cc._decorator;
|
|
|
|
|
|
/**
|
|
|
* 伙伴AI
|
|
@@ -15,100 +18,91 @@ const {ccclass, property} = cc._decorator;
|
|
|
@ccclass
|
|
|
export default class AIPet extends AIBase {
|
|
|
|
|
|
- onLoad(){
|
|
|
+ onLoad() {
|
|
|
super.onLoad();
|
|
|
- this.atk_CD = 3000;
|
|
|
- this.speed = 120;
|
|
|
- this.atk_count = 3;
|
|
|
+ this.speed = 100;
|
|
|
this.sprite.SPEED_WALK = this.speed;
|
|
|
+ this.addSkill()
|
|
|
+ }
|
|
|
+ public addSkill() {
|
|
|
+ let skillFixed: SkillFixed = this.node.addComponent(SkillFixed)
|
|
|
+ cc.resources.load('prefab/bullet/' + this.sprite.attrData.bullet, cc.Prefab, (err, prefab: cc.Prefab) => {
|
|
|
+ if (err) {
|
|
|
+ cc.error(err);
|
|
|
+ } else {
|
|
|
+ //加载结束
|
|
|
+ skillFixed.mBullet = prefab
|
|
|
+ this.skills = this.node.getComponents(SkillBase);
|
|
|
+ }
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
- update (dt) {
|
|
|
- if(this.sprite){
|
|
|
- if(this.sprite.gamePause){
|
|
|
+ update(dt) {
|
|
|
+ if (this.sprite && this.sprite.isActive) {
|
|
|
+ if (this.sprite.gamePause) {
|
|
|
return;
|
|
|
}
|
|
|
this.AI();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public AI(){
|
|
|
+ public AI() {
|
|
|
+ if (!this.canSkill) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
let target = this.checkTarget();
|
|
|
- if(target){
|
|
|
- let time = new Date().getTime();
|
|
|
+ if (target) {
|
|
|
(this.sprite as PSprite).stopFollow();
|
|
|
- if(this.sprite.isActive){
|
|
|
- if(this.skills.length > 0){
|
|
|
- let skill = this.checkSkill(target);
|
|
|
- if(skill){
|
|
|
+ if (this.sprite.isActive) {
|
|
|
+ let skill: SkillBase = this.checkSkill(target);
|
|
|
+ if (skill) {
|
|
|
+ if (this.speed <= 0) {
|
|
|
this.canSkill = false;
|
|
|
- skill.exe(target,()=>{
|
|
|
+ skill.exe(target, () => {
|
|
|
this.canSkill = true;
|
|
|
+ // cc.log('技能使用结束 :',skill)
|
|
|
});
|
|
|
- // cc.tween(this.node).sequence(
|
|
|
- // cc.delayTime(skill.continued/1000),
|
|
|
- // cc.callFunc(()=>{
|
|
|
-
|
|
|
- // })
|
|
|
- // ).start();
|
|
|
- }
|
|
|
- } else{
|
|
|
- if(time - this.atk_Time > this.atk_CD){
|
|
|
- this.atk_Time = time;
|
|
|
- this.fire(target);
|
|
|
+ } else {
|
|
|
+ let p1 = this.sprite.node.getPosition()
|
|
|
+ let p2 = target.node.getPosition()
|
|
|
+ let dis = CMath.getDistance(p1, p2);
|
|
|
+ if (dis < skill.range) {
|
|
|
+ this.canSkill = false;
|
|
|
+ skill.exe(target, () => {
|
|
|
+ this.canSkill = true;
|
|
|
+ // cc.log('技能使用结束 :',skill)
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ this.walk(skill.range);
|
|
|
+ // this.moveToTarget(target)
|
|
|
+ }
|
|
|
}
|
|
|
+ } else {
|
|
|
+ this.walk(300);
|
|
|
}
|
|
|
}
|
|
|
- }else{
|
|
|
+ } else {
|
|
|
(this.sprite as PSprite).startFollow();
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- public fire(target:FSprite){
|
|
|
- //判断是否在攻击范围内
|
|
|
- let mts = this.sprite.mButtleDis;
|
|
|
- let p1 = target.node.getPosition();
|
|
|
- let p2 = this.sprite.node.getPosition();
|
|
|
- let dis = cc.Vec2.distance(p1,p2);
|
|
|
-
|
|
|
- if(dis > mts){
|
|
|
- this.atk_Time = 0;
|
|
|
- let tmp = {
|
|
|
- x:0,
|
|
|
- y:0
|
|
|
- }
|
|
|
- let px1 = p1.x - p2.x;
|
|
|
- if(Math.abs(px1) < 50){
|
|
|
- tmp.x = 0;
|
|
|
- }else if(px1 > 0){
|
|
|
- tmp.x = 1;
|
|
|
- }else{
|
|
|
- tmp.x = -1;
|
|
|
- }
|
|
|
- let py1 = p1.y - p2.y;
|
|
|
- if(Math.abs(py1) < 50){
|
|
|
- tmp.y = 0;
|
|
|
- }else if(py1 > 0){
|
|
|
- tmp.y = 1;
|
|
|
- }else{
|
|
|
- tmp.y = -1;
|
|
|
- }
|
|
|
- this.sprite.setDir(tmp);
|
|
|
- }else{
|
|
|
- this.sprite.setDir({x:0,y:0});
|
|
|
- this.sprite.status = SpriteType.NONE;
|
|
|
- this.sprite.setShooting(true);
|
|
|
- let count = 0;
|
|
|
- this.sprite.setFireCallback(()=>{
|
|
|
- count ++;
|
|
|
- if(count >= this.atk_count){
|
|
|
- this.sprite.setShooting(false);
|
|
|
- this.sprite.setFireCallback(null);
|
|
|
- cc.tween(this).delay(0.7).call(()=>{
|
|
|
- this.walk(this.sprite.mButtleDis);
|
|
|
- }).start();
|
|
|
- }
|
|
|
- });
|
|
|
+ /**
|
|
|
+ * 远程怪物的闲逛
|
|
|
+ */
|
|
|
+ public walk(distance) {
|
|
|
+ if (this.speed > 0) {
|
|
|
+ this.canSkill = false;
|
|
|
+ this.sprite.setDir(this.getRandState(distance));
|
|
|
+ this.sprite.playAction(SpriteActionType.move, true)
|
|
|
+ cc.tween(this).delay(1).call(() => {
|
|
|
+ this.sprite.setDir({ x: 0, y: 0 });
|
|
|
+ this.sprite.playAction(SpriteActionType.stand, true)
|
|
|
+ }).delay(1).call(() => {
|
|
|
+ this.canSkill = true;
|
|
|
+ }).start()
|
|
|
+ } else {
|
|
|
+ this.canSkill = true;
|
|
|
+ this.sprite.setDir({ x: 0, y: 0 });
|
|
|
+ this.sprite.playAction(SpriteActionType.stand, true)
|
|
|
}
|
|
|
}
|
|
|
}
|