GEngine.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import ConfigManager from "./configs/ConfigManager";
  2. import { GUIManager } from "./gui/GUIManager";
  3. import IInitiator from "./initiators/IInitiator";
  4. import InitiatorEvent from "./initiators/InitiatorEvent";
  5. import LoadingView from "./loadingView/LoadingView";
  6. import { SoundManager } from "./sounds/SoundManager";
  7. const {ccclass, property} = cc._decorator;
  8. @ccclass
  9. export default class GEngine extends cc.Component
  10. {
  11. @property(cc.Node)
  12. layerRoot:cc.Node=null;
  13. private __initiators:IInitiator[];
  14. private __completeCallBack:Function;
  15. private __autoTick:boolean;
  16. private __inited:boolean;
  17. start():void{
  18. if(GEngine.single!=null){
  19. throw new Error("引擎单例已存在");
  20. }
  21. GEngine.instance=this;
  22. this.__inited=false;
  23. }
  24. /**
  25. * @param initiators 初始化流程
  26. * @param completeCallBack 完成回调
  27. * @param autoTick true 内部调用心跳 false不调用
  28. */
  29. public Startup(initiators:IInitiator[],completeCallBack:Function,autoTick:boolean=true):void{
  30. this.__initiators=initiators;
  31. this.__completeCallBack=completeCallBack;
  32. this.__autoTick=autoTick;
  33. //节点静态化
  34. cc.game.addPersistRootNode(this.node);
  35. this.addComponent(SoundManager);
  36. this.__initiatorTotal=this.__initiators.length;
  37. this.TryNextInitiator();
  38. }
  39. update(dt:number):void{
  40. if(this.__inited&&this.__autoTick){
  41. this.Tick(dt);
  42. }
  43. }
  44. /**
  45. * 心跳
  46. * @param dt
  47. */
  48. Tick(dt:number):void{
  49. GUIManager.single.Tick(dt);
  50. }
  51. private __initiatorTotal:number=0;
  52. private __currentInitiator:IInitiator;
  53. private TryNextInitiator():void{
  54. if(this.__currentInitiator!=null){
  55. this.__currentInitiator.RemoveAllEvent();
  56. this.__currentInitiator.Destroy();
  57. this.__currentInitiator=null;
  58. }
  59. if(this.__initiators.length>0){
  60. this.__currentInitiator=this.__initiators.shift();
  61. LoadingView.single.UpdateLabel(this.__currentInitiator.GetName());
  62. this.__currentInitiator.AddEvent(InitiatorEvent.EVENT_PROGRESS,this,this.InitiatorProgressEventHandler);
  63. this.__currentInitiator.AddEvent(InitiatorEvent.EVENT_ERROR,this,this.InitiatorErrorEventHandler);
  64. this.__currentInitiator.AddEvent(InitiatorEvent.EVENT_COMPLETE,this,this.InitiatorCompleteEventHandler);
  65. this.__currentInitiator.Start();
  66. }else{
  67. this.onComplete();
  68. }
  69. }
  70. private InitiatorProgressEventHandler(progress:number):void{
  71. let value:number=(this.__initiatorTotal-this.__initiators.length+progress)/this.__initiatorTotal;
  72. LoadingView.single.UpdateProgress(value);
  73. }
  74. private InitiatorErrorEventHandler(msg:string):void{
  75. throw new Error(msg);
  76. }
  77. private InitiatorCompleteEventHandler():void{
  78. cc.log(this.__currentInitiator.GetName()+"完成");
  79. this.TryNextInitiator();
  80. }
  81. private onComplete():void{
  82. if(this.__completeCallBack!=null){
  83. this.__completeCallBack();
  84. this.__completeCallBack=null;
  85. }
  86. this.__inited=true;
  87. }
  88. private static instance:GEngine;
  89. public static get single():GEngine{
  90. return this.instance;
  91. }
  92. }