| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 | import { _decorator, Component, Constructor } from 'cc';import { ModulerBase } from './ModulerBase';import { GameInfo } from '../../GameInfo';const { ccclass, property } = _decorator;@ccclass('GameMgr')export class GameMgr extends Component {    //单例    private static _instance: GameMgr = null;    private _modluers:ModulerBase[] = [];    //单例接口    static get Instance(){return GameMgr._instance}    protected onLoad(): void {        //单例赋值 this        GameMgr._instance = this;        //获取子节点当中所有被管理的模块        this._modluers = this.getComponentsInChildren(ModulerBase);    }    //开始游戏    startGame(){        GameInfo.Instance.setIsGameOver(false);        this._modluers.forEach((moduler:ModulerBase)=>{            moduler.init();          })    }    //获取被管理的某个模块(派生类型)    //模板    getModuler<T extends ModulerBase>(type: Constructor<T>): T{        for(const moduler of this._modluers){            if(moduler instanceof type){                return moduler as T;            }        }        return null;    }    //移除某个管理模块    removeModuler<T extends ModulerBase>(type: Constructor<T>){        for(let i = 0; i < this._modluers.length; i++){            if(this._modluers[i] instanceof type){                this._modluers[i].removeSelf();                return;            }        }    }    start() {        this.startGame();    }}
 |