GameMgr.ts 1.5 KB

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