import CMath from "../../../../util/CMath";
import BObject from "../../bullet/BObject";
import FSprite, { SpriteActionType, SpriteType } from "../FSprite";
import SkillBase from "./SkillBase";

/**
 * 点射
 */
const {ccclass, property} = cc._decorator;

@ccclass
export default class SkillFixed extends SkillBase {
   /**
     * 每次攻击几颗子弹
     */
    @property({
        displayName: '每次攻击子弹数量'
    })
    public count = 1;

    @property({
        displayName: '每颗子弹间隔时间'
    })
    public btime = 1;

    @property({
        type:cc.Prefab,
        displayName: '子弹'
    })
    mBullet:cc.Prefab = null;
    /**
     * 释放技能
     */
    public exe(target:FSprite,callback:()=>void){
        this.time = new Date().getTime();
        // this.sprite.setDir({ x: 0, y: 0 });


        //-----播放开始动画
        let bNode = cc.instantiate(this.mBullet)
        
        let bobject = bNode.getComponent(BObject)
        bobject.distance = this.range
        let sf = bobject.mStartEffect

        let x = this.sprite.mAtkSite.worldX * this.sprite.spine.node.scaleX
        let y = this.sprite.mAtkSite.worldY

        let pos = cc.v2(x, y);
        let sfNode = cc.instantiate(sf)
        sfNode.angle = this.sprite.wAngle
        sfNode.setPosition(pos);
        sfNode.parent = this.sprite.node
        let startSpine: sp.Skeleton = sfNode.getComponent(sp.Skeleton)
        startSpine.setCompleteListener(() => {
            startSpine.setCompleteListener(null);
            sfNode.destroy()
            callback()
        });
        if(startSpine.findAnimation('skill')){
            startSpine.setAnimation(0, 'skill', false);
        }else{
            startSpine.setAnimation(0, 'atk', false);
        }
        //----开始动画播放结束

        this.sprite.playAction(SpriteActionType.atk, false, () => {
            // this.AI.walk(this.range);
            this.sprite.playAction(SpriteActionType.stand,true)
        });
        let tp = target.node.getPosition();
        for (let i = 0; i < this.count; i++) {
            this.fire(i, tp);
        }
    }

    public fire(index, tp: cc.Vec2) {
        this.fireBullet(index, tp);
    }

    private fireBullet(index, p2) {
        for (let i = 0; i < this.count; i++) {
            cc.tween(this).sequence(
                cc.delayTime(i * this.btime),
                cc.callFunc(() => {
                    this.fireBulletByTime(index, p2);
                })
            ).start();
        }
    }

    private fireBulletByTime(index, p2) {
        let node = cc.instantiate(this.mBullet);
        node.group = 'bullet'
        let sprite = this.sprite
        let x = sprite.node.x + sprite.mAtkSite.worldX * this.sprite.spine.node.scaleX
        let y = sprite.node.y + sprite.mAtkSite.worldY
        let pos = cc.v2(x, y);
        node.setPosition(pos);

        let bObject = node.getComponent(BObject);
        bObject.setSprite(this.sprite);
        bObject.distance = this.range
        bObject.speed = this.speed;
        node.parent = this.sprite.map.mSprites;
        let p1 = node.getPosition();

        let angle = CMath.getAngle(p1, p2);

        // let cur = index - Math.floor(this.count / 2);

        // angle += cur * this.rotate;
        bObject._skillData = this._skillData
        bObject.fireAngle(angle);
    }

    
}
