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

/**
 * 龙卷风类型的技能
 */
const {ccclass, property} = cc._decorator;

@ccclass
export default class MFWind extends SkillBase {
    /**
     * 法术效果
     */
    public mBullet: cc.Prefab

    onLoad() {
        this.sprite = this.node.getComponent(FSprite);
    }
    /**
     * 释放技能
     */
    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)
        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()
            if(callback){
                callback()
            }
        });
        startSpine.setAnimation(0, 'atk', false);
        //----开始动画播放结束

        // this.sprite.playAction(SpriteActionType.atk, false, () => {
        //     if(this.sprite.isWalk){
        //         this.sprite.playAction(SpriteActionType.run,true)
        //     }else{
        //         this.sprite.playAction(SpriteActionType.stand,true)
        //     }
        // });
        let tp = undefined
        if (target) {
            tp = target.node.getPosition();
        } else {
            let angle = this.sprite.wAngle;

            let hd = angle * Math.PI / 180;
            let y = Math.sin(hd) * 300 + this.node.y
            let x = Math.cos(hd) * 300 + this.node.x;
            tp = cc.v2(x, y)
        }

        //向目标吹龙卷风
        if (target) {
            tp = target.node.getPosition();
        } else {
            
            let y = this.sprite.moveV2.y * 300 + this.node.y
            y +=  this.sprite.mAtkSite.worldY
            let x = this.sprite.moveV2.x * 300 + this.node.x;
            tp = cc.v2(x, y)
        }
        this.fireBulletByTime(tp)
    }
    /**
     * 
     * @param p2 向目标发射
     */
    private fireBulletByTime(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(LBObject);
        bObject.setSprite(this.sprite);
        bObject.speed = this.speed;
        node.parent = this.sprite.map.mSprites;
        let p1 = node.getPosition();


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

        bObject._skillData = this._skillData
        bObject.fireAngle2(angle);
    }
}
