
import FSprite, { SpriteActionType, SpriteType } from "../FSprite";
import PSprite from "../PSprite";
import AIBase from "./AIBase";

const {ccclass, property} = cc._decorator;

/**
 * 伙伴AI
 * 
 * AI特性:
 * 1.有攻击对象的时候，主动攻击
 * 2.没有攻击对象的时候，跟随
 */
@ccclass
export default class AIPet extends AIBase {

    onLoad(){
        super.onLoad();
        this.atk_CD = 3000;
        this.speed = 120;
        this.atk_count = 3;
        this.sprite.SPEED_WALK = this.speed;
    }

    update (dt) {
        if(this.sprite){
            if(this.sprite.gamePause){
                return;
            }
            this.AI();
        }
    }

    public AI(){
        let target = this.checkTarget();
        if(target){
            let time = new Date().getTime();
            (this.sprite as PSprite).stopFollow();
            if(this.sprite.isActive){
                if(this.skills.length > 0){
                    let skill = this.checkSkill(target);
                    if(skill){
                        this.canSkill = false;
                        skill.exe(target,()=>{
                            this.canSkill = true;
                        });
                        // 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{
            (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();
                }
            });
        }
    }
}
