import { _decorator,Camera,Node} from 'cc';
import { GMap } from './GMap';
import { Player } from './Player';
import { Utils } from '../utils/Utils';
import { Logger } from '../extend/Logger';
import { ResUtil } from '../utils/ResUtil';
import { userIns } from '../data/UserData';
import { BuildEnemys } from './BuildEnemys';
import { autoBind } from '../extend/AutoBind';
import { BaseExp } from '../core/base/BaseExp';
import { PoolManager } from '../core/manager/PoolManager';
import { uiMgr } from '../core/manager/UIManager';
import { Constants } from '../data/Constants';
import { Main } from './Main';
const { ccclass, property } = _decorator;

@ccclass('Game')
export class Game extends BaseExp {
    public static I: Game;
    @autoBind({type: Node,tooltip: "map节点的位置"})
    public map_pos: Node = null!;

    /*是否暂停游戏*/
    public isPause: boolean = false;
    /**是否游戏结束*/
    public _isGameOver: boolean = false;
    public set isGameOver(value: boolean) {
        if(value){this.gameEnd();}
        this._isGameOver = value;
    }
    public get isGameOver(){
        return this._isGameOver;
    }
    //当前地图场景
    public map: GMap = null!;
    //创建敌人的组建
    public buildEnemys: BuildEnemys = null!;
    //玩家组建
    public player: Player = null!;
    //主角摄像机
    public camera: Camera = null!;
    //Canvas2D节点
    public canvas: Node = null!;

    start() {
        Game.I = this;
        this.canvas = this.node.parent.getChildByName('Canvas')
        this.canvas.active = true;
        this.map_pos.active = true;
        //加载map
        this.loadMap();
    }

    /**
     * 下一关
     */
    public nextLevel(){
        userIns.data.level += 1;
        //加载新的map
        this.loadMap();
        //刷新新的任务
        uiMgr.getPageComponent(Constants.mainUIs.main).loadData();
        this.isGameOver = this.isPause = false;
        //加载敌人
        this.buildEnemys.loadLevelEnemys();
    }

    /**
     * 重玩
     */
    public restart(){
        //重新加载map
        this.loadMap();
        //创建玩家拥有的枪
        this.player.restart();
        //加载敌人 
        this.buildEnemys.loadLevelEnemys();
    }

    /**
     * 加载map
     */
    public loadMap(){
        const name: string = userIns.getCurLevelData().map;
        if(Utils.isNull(name))return
        Logger.log(`加载地图:${name}`);
        ResUtil.loadRes(`map/${name}`).then((map:Node)=>{
            //把旧的地图回收 PoolManager.putNode(this.map.node); 
            //这儿不回收了 懒得处理初始状态 全部重新加载一次
            if(this.map){
               this.map.node.removeFromParent();
               this.map.node.destroy();
               this.map = null!;
            }
            map.parent = this.map_pos.parent;
            map.worldPosition = this.map_pos.worldPosition;
            this.map = map.getComponent(GMap);
            //创建敌人的组建
            this.buildEnemys = this.map.getComponent(BuildEnemys);
            //玩家组建
            this.player = this.map.getComponentInChildren(Player);
            //主角摄像机
            const cameraNode: Node = Utils.findName(map, 'mainCamera');
            this.camera = cameraNode.getComponent(Camera);
        });
    }

    /**
     * 游戏结束回收
     */
    public gameEnd(){
        this.buildEnemys.recycle();
    }
}

