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

/**
 * 主角法术攻击
 */
const { ccclass, property } = cc._decorator;

@ccclass
export default class MMagic 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 y = this.sprite.moveV2.y * 300 + this.node.y
            let x = this.sprite.moveV2.x * 300 + this.node.x;
            tp = cc.v2(x, y)
        }

        let map = this.sprite.ff.mMap;
        let hitNode = cc.instantiate(bobject.mHitEffect)
        hitNode.setPosition(tp)
        hitNode.parent = map.mSprites;
        
        let endSpine: sp.Skeleton = hitNode.children[0].getComponent(sp.Skeleton)
        endSpine.setCompleteListener(() => {
            endSpine.setCompleteListener(null);
            hitNode.destroy()

        });
        endSpine.setAnimation(0, 'animation', false);

        this.roundHit(hitNode)
    }

        /**
     * 炸开后周围受到伤害
     */
    public roundHit(hitNode:cc.Node) {
        let ff = this.sprite.ff
        let mGroup = this.sprite.getEnemyGroup();
        let nodes = ff.mMap.getSprites();
        // cc.log('this._skillData : ',this._skillData)
        for (let i = 0; i < nodes.length; i++) {
            const node = nodes[i];
            let target = node.getComponent(FSprite);
            if (target && node.active && target.isActive && target.hp > 0 && target.node.group == mGroup) {
                let dis = cc.Vec2.distance(hitNode.getPosition(), node.getPosition());
                if (dis < 300) {
                    if (target.hp > 0) {
                        if (target != null && target.isActive) {
                            this.sprite.atkjs(target, this._skillData);
                        }
                    }
                }
            }
        }
    }

}
