12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- import { _decorator, Component, Constructor } from 'cc';
- import { ModulerBase } from './ModulerBase';
- 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(){
- 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();
- }
- }
|