/** 
 * @description 获取道具特效
 * @author kaka 
 * */
const { ccclass } = cc._decorator;

@ccclass
export default class FlySystem extends cc.Component {
    onLoad() {
        mk.fly = this;
    }

    private is_new_cb = true;
    /**
     * @param type 类型 道具id  10001，10002
     * @param num 特效数量
     * @param pos 起始位置
     * @param endpos 飞行目标的世界坐标 或目标对象路径
     * @param cb 动画结束回调
    */
    public async PlayCoinAnim(type: number = 0, num: number, pos: cc.Vec2, endpos: cc.Vec2 | string, item_cb: Function = null) {
        for (let i = 0; i < num; i++) {
            let copyCoin = await mk.pool.getPrefab('game/prefab/coin');
            let url = 'game/texture/coin/' + type;
            let sf = await mk.loader.load(url, cc.SpriteFrame);
            copyCoin.getComponent(cc.Sprite).spriteFrame = sf;
            copyCoin.setParent(this.node);
            copyCoin.scale = mk.math.random(0.56, 0.84, false);
            copyCoin.angle = mk.math.random(-45, 45, false);
            let dir = cc.v2(mk.math.random(-1, 1, false), mk.math.random(-1, 1, false));
            dir = dir.normalize();
            let deltaInit = cc.v2(mk.math.random(10, 160, false) * dir.x, mk.math.random(10, 160, false) * dir.y);
            let initPos = pos.add(deltaInit);
            copyCoin.setPosition(pos);

            this.FlyIn(type, copyCoin, initPos, endpos, item_cb);
        }
        this.is_new_cb = true;
    }

    private FlyIn(type: number, target: cc.Node, end: cc.Vec2, endpos: cc.Vec2 | string = null, item_cb: Function = null) {
        cc.tween(target)
            .to(mk.math.random(0.3, 0.6, false), { position: cc.v3(end) }, cc.easeSineInOut())
            .call(() => {
                this.FlyOut(type, target, endpos, item_cb);
            })
            .start();
    }

    private FlyOut(type: number, target: cc.Node, end: cc.Vec2 | string, item_cb: Function = null) {
        let dur = mk.math.random(0.3, 1, false);
        cc.tween(target)
            .to(dur, { scale: 0.8 })
            .start();

        let end_pos: cc.Vec2 = null;
        let end_node: cc.Node = null;
        if (typeof end === 'string') {
            end_node = cc.find(end as string);
            let world_pos = end_node.parent.convertToWorldSpaceAR(end_node.getPosition());
            end_pos = this.node.convertToNodeSpaceAR(world_pos);
        }
        else {
            end_pos = this.node.convertToNodeSpaceAR(end);
        }

        cc.tween(target)
            .to(dur, { position: cc.v3(end_pos) }, cc.easeSineOut())
            .call(() => {
                console.log('end_pos  ', target.position)
                mk.pool.return('game/prefab/coin', target);
                if (end_node) {
                    cc.tween(end_node).to(0.1, { scale: 1.2 }).to(0.05, { scale: 1 }).start();
                }

                if (item_cb != null) {
                    this.is_new_cb && item_cb();
                    this.is_new_cb = false;
                }
                this.iconScale(type);
            })
            .start();
    }

    /**
     * icon缩放动画
     * @param type icon类型
     * @returns 
     */
    private iconScale(type: number) {
        let node;
        if (type === 1) {
            node = gData.gameData.gameStyle.icon_hb;
        } else if (type === 3) {
            node = gData.gameData.gameStyle.icon_zb;
        }
        if (!node) return;
        let scale1 = cc.tween().to(0.05, { scale: 0.9 });
        let scale2 = cc.tween().to(0.05, { scale: 1 });
        let seque = cc.tween().sequence(scale1, scale2);
        cc.tween(node).then(seque).repeat(2).start();
    }
}

