import { v2 } from "cc";
import { JTween } from "../../../../../../extensions/ngame/assets/ngame/sync/frame/game/tween/JNFrameTween";
import { app } from "../../../../App";
import GSpine from "../../../base/common/GSpine";
import GSkillCDBase from "../../GSkillCDBase";
import { v3 } from "cc";
import GBaseMode from "../../../GBaseMode";
import GDetection from "../../../base/common/GDetection";
import { rect } from "cc";
import GRoleDefault from "../../../base/role/GRoleDefault";
import { TB } from "../../../../config/data/schema";
import GRoleAttack from "../../../base/common/GRoleAttack";
import { sp } from "cc";

/**
 * 疯狂松鼠技能
 * 参数:[技能冷却]
 */
export default class GSkillCrazySquirrel extends GSkillCDBase{

    role:GRoleDefault;
    info: TB.TbGRoleSkill;

    bind(role:GRoleDefault,info: TB.TbGRoleSkill):GSkillCrazySquirrel {
        //技能冷却
        this.cdTatal = parseInt(info.skillArgs[0]);
        this.role = role;
        this.info = info;
        return this;

    }

    onRelease(): boolean {
        
        //获取敌人位置
        let enemy = this.role.fsm.enemy;
        let scene:GBaseMode<{},{}> = this.role.mode;
        if(!enemy) return false;

        let aw = parseInt(this.info.skillArgs[1]);
        let ah = parseInt(this.info.skillArgs[2]);
        
        //出现松鼠丢炸弹
        GSpine.onPlayAnotherSpine(this.role,app.battleRes.getData(140003),"skill",{
            end:() => {
                //从天而降
                let pos = enemy.v2World.add(v2(0,1000));
                let end = enemy.v2World.clone();

                //生成火球
                let spine = GSpine.onCreateSpine(app.battleRes.getData(140004));
                this.role.mode.addGNode(spine.node,pos);
                spine.setAnimation(0,"animation",true);
                spine.node.angle = 90;
                JTween(pos)
                    .to(end,600)
                    .onUpdate(pos => {
                        spine.node.worldPosition = v3(pos.x,pos.y,0);
                    })
                    .onComplete(() => {
                        let world = spine.node.worldPosition;
                        spine.node.destroy();
                        GSpine.onPlaySceneSpine(scene,v2(world.x,world.y),app.battleRes.getData<sp.SkeletonData>(140005),"animation");
                        GDetection.testAABBCenterRole(rect(world.x,world.y,aw,ah)).forEach(enemy =>{
                            GRoleAttack.onNormalAttack(this.role,enemy);
                        });
                    })
                    .start();
            }
        });
        return true;

    }

}

