123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- 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() {
- if (!this.canSkill) {
- return;
- }
- let target = this.checkTarget();
- if (target) {
- (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)
- }
- }
- }
|