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; /** * 伙伴AI * * AI特性: * 1.有攻击对象的时候,主动攻击 * 2.没有攻击对象的时候,跟随 */ @ccclass export default class AIPet extends AIBase { onLoad() { super.onLoad(); this.speed = 300; this.sprite.SPEED_WALK = this.speed; this.addSkill() } public addSkill() { let skillFixed: SkillFixed = this.node.addComponent(SkillFixed) skillFixed.count = 1; skillFixed.time = 0; skillFixed.btime = 1; skillFixed.CD = 800; 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 && this.sprite.isActive) { if (this.sprite.gamePause) { return; } this.AI(); } } public AI() { let target = this.checkTarget(); if (target) { if (!this.canSkill) { return; } (this.sprite as PSprite).stopFollow(); if (this.sprite.isActive) { let skill: SkillBase = this.checkSkill(target); if (skill) { if (this.speed <= 0) { this.canSkill = false; skill.exe(target, () => { this.canSkill = true; // cc.log('技能使用结束 :',skill) }); } 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 { (this.sprite as PSprite).startFollow(); } } /** * 远程怪物的闲逛 */ 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) } } }