
import { EditorSet } from "../common/editorSet";

const {ccclass, property, executeInEditMode, playOnFocus} = cc._decorator;

ccclass
class AniData{
    loop: boolean;
}


@ccclass
@executeInEditMode
@playOnFocus
export default class AniList extends cc.Component {
    @property(cc.Prefab) monster: cc.Prefab = null;
    @property([cc.AnimationClip]) aniClip = [];

    monsterNode: cc.Node;

    protected onLoad(): void {

        let monsterNode = new cc.Node('monsterGroup');
        this.monsterNode = monsterNode;
        EditorSet.DontShow(monsterNode);
        this.node.addChild(monsterNode);

        if(CC_EDITOR){
            let node = new cc.Node('Collide');
            this.node.addChild(node);
            node.zIndex = cc.macro.MAX_ZINDEX;
            EditorSet.DontShow(node);
            let debugDrawer = node.addComponent(cc.Graphics);
            debugDrawer.lineWidth = 5;
            debugDrawer.strokeColor = new cc.Color(255, 0, 0);
            debugDrawer.fillColor = new cc.Color(255, 0, 0);

            debugDrawer.circle(0, 0, 20);
            debugDrawer.stroke();
        }
        else{
            this.play();
        }
        
    }

    // protected onFocusInEditor(): void {
    //     this.play();
    // }

    // protected onLostFocusInEditor(): void {
    //     this.end();
    // }

    end(){
        this.monsterNode.destroyAllChildren();
    }

    play(): void {
        this.monsterNode.destroyAllChildren();

        if(!this.monster) {
            cc.log('设置怪物资源');
            return;
        }

        let monsterNode = cc.instantiate(this.monster);
        let cnt = 0;
        let callAni = ()=>{
            let aniNode = new cc.Node('AniItem');
            let parent = monsterNode.parent ? monsterNode.parent : this.monsterNode;
            monsterNode.removeFromParent(false);
            aniNode.addChild(monsterNode);
            parent.addChild(aniNode);

            let ani = aniNode.addComponent(cc.Animation);
            ani.addClip(this.aniClip[cnt], 'path')
            ani.play('path');
            ani.once(cc.Animation.EventType.FINISHED, ()=>{
                cnt++;
                if(cnt < this.aniClip.length){
                    aniNode.angle = 0;
                    callAni();
                }
            });
        }

        callAni();

    }

}