import { SpriteFrame } from "cc";
import { TD, app } from "../../../App";
import GRoleBase from "../role/GRoleBase";
import { GAttackBase, GAttackBullet } from "./GAttack";
import GButtleDefault from "../bullet/GButtleDefault";
import { v3 } from "cc";
import { bezier } from "cc";
import { v2 } from "cc";
import { Vec2 } from "cc";
import GEffectUtil from "../common/GEffectUtil";
import GDetection from "../common/GDetection";
import { rect } from "cc";
import GRoleDefault from "../role/GRoleDefault";
import { TB } from "../../../config/data/schema";
import GRoleAttack from "../common/GRoleAttack";
import { sp } from "cc";

/**
 * 抛物线 爆炸普攻
 * 攻击子弹,爆炸特效,龙骨-初始位置,子弹大小,爆炸宽度,爆炸高度
 */
export default class GAttackParabolicBangRemote implements GAttackBase{

    attack(role: GRoleDefault, info: TB.TbGRoleAttack): void {

        let enemy = role.fsm.enemy;
        if(!enemy) return;

        let image:SpriteFrame = app.battleRes.getData<SpriteFrame>(info.attackArgs[0]);
        let bang = {
            ske: app.battleRes.getData<sp.SkeletonData>(info.attackArgs[1]),
        };
        let bone = role.spine.findBone(info.attackArgs[2]);
        let scale:number = parseFloat(info.attackArgs[3]);
        let aw = parseFloat(info.attackArgs[4]);
        let ah = parseFloat(info.attackArgs[5]);

        if(!image || !bone || !scale || !bang|| !aw|| !ah) {
            console.warn("GAttackParabolicBangRemote ERROR",image,bone,scale,bang);
            return;
        }
        
        let bullet = GAttackBullet.create(GButtleDefault,{
            image:image,
            scale:scale,
        });
        role.mode.addGObject(bullet);
        let world2 = role.node.worldPosition.clone().add(v3(role.getMirrorValue(bone.worldX),bone.worldY,0))

        //设置子弹位置
        bullet.node.setWorldPosition(v3(world2.x,world2.y,world2.z))
        
        //子弹移动
        let start = bullet.v2World;
        let end = enemy.v2World;
        let center = v2(((start.x + end.x) / 2),((start.y + end.y) / 2) + (Math.abs((start.x - end.x)) / 2));

        bullet.JTween({})
            .to({},Vec2.distance(start,end)*3)
            .onUpdate((data,elapsed) => {
                bullet.node.setWorldPosition(v3(
                    bezier(start.x,center.x,center.x,end.x,elapsed),
                    bezier(start.y,center.y,center.y,end.y,elapsed),
                    0
                ))
            })
            .onComplete(() => {
                //销毁
                bullet.node.destroy();
                
                if(!role.get()) return;
                //生成爆炸特效
                let effect = GEffectUtil.createOne(bang.ske);
                role.mode.addGNode(effect.node,bullet.node.worldPosition);
                //获取默认动画
                effect.setAnimation(0,"animation",false);

                GDetection.testAABBCenterRole(rect(effect.node.worldPosition.x,effect.node.worldPosition.y,aw,ah)).forEach(enemy =>{
                    GRoleAttack.onNormalAttack(role,enemy);    //普通攻击
                });
                
            })
            .start();
            

    }

}


