GameMgr.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { _decorator, Component, Constructor, Size, TiledMap, TiledObjectGroup, Vec3 } from 'cc';
  2. import { ModulerBase } from './ModulerBase';
  3. const { ccclass, property } = _decorator;
  4. @ccclass('GameMgr')
  5. export class GameMgr extends Component {
  6. //单例
  7. private static _instance: GameMgr = null;
  8. private _modluers:ModulerBase[] = [];
  9. //单例接口
  10. static get Instance(){return GameMgr._instance}
  11. protected onLoad(): void {
  12. //单例赋值 this
  13. GameMgr._instance = this;
  14. //获取子节点当中所有被管理的模块
  15. this._modluers = this.getComponentsInChildren(ModulerBase);
  16. }
  17. //开始游戏
  18. startGame(){
  19. this._modluers.forEach((moduler:ModulerBase)=>{
  20. moduler.init();
  21. })
  22. }
  23. //获取被管理的某个模块(派生类型)
  24. //模板
  25. getModuler<T extends ModulerBase>(type: Constructor<T>): T{
  26. for(const moduler of this._modluers){
  27. if(moduler instanceof type){
  28. return moduler as T;
  29. }
  30. }
  31. return null;
  32. }
  33. //移除某个管理模块
  34. removeModuler<T extends ModulerBase>(type: Constructor<T>){
  35. for(let i = 0; i < this._modluers.length; i++){
  36. if(this._modluers[i] instanceof type){
  37. this._modluers[i].removeSelf();
  38. return;
  39. }
  40. }
  41. }
  42. start() {
  43. this.startGame();
  44. }
  45. update(deltaTime: number) {
  46. }
  47. }