logic.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { GameSysLogic } from "./gameSysLogic";
  2. export class GameLogicMgr {
  3. private static _inst: GameLogicMgr;
  4. public static get inst(): GameLogicMgr {
  5. if (this._inst == null) {
  6. this._inst = new GameLogicMgr();
  7. this._inst.init();
  8. }
  9. return this._inst;
  10. }
  11. isGamePause: boolean = false;
  12. sysLogics: Map<number, GameSysLogic> = new Map();
  13. switch(){
  14. this.isGamePause = !this.isGamePause;
  15. //@ts-ignore
  16. dragonBones.timeScale = this.isGamePause ? 0 : 1;
  17. }
  18. init(){
  19. //@ts-ignore
  20. let aniMgr = cc.director.getAnimationManager();
  21. let actionMgr = cc.director.getActionManager();
  22. if(!CC_EDITOR){
  23. aniMgr._oldUpdate = aniMgr.update;
  24. aniMgr.update = function(dt){
  25. if(GameLogicMgr.inst.isGamePause) return;
  26. aniMgr._oldUpdate(dt);
  27. }
  28. // @ts-ignore
  29. actionMgr._oldUpdate = actionMgr.update;
  30. actionMgr.update = function(dt){
  31. if(GameLogicMgr.inst.isGamePause) return;
  32. // @ts-ignore
  33. actionMgr._oldUpdate(dt);
  34. }
  35. }
  36. cc.director.getScheduler().enableForTarget(this);
  37. cc.director.getScheduler().schedule(this.update, this, 0, cc.macro.REPEAT_FOREVER, 0, false);
  38. cc.director.getScheduler().schedule(this.updateSec, this, 1, cc.macro.REPEAT_FOREVER, 0, false);
  39. }
  40. update(dt){
  41. if (this.isGamePause) return;
  42. this.sysLogics.forEach((v)=>{
  43. // v['update'](dt);
  44. v.gameUpdate(dt);
  45. });
  46. let toDel = [];
  47. this.timer.forEach((v,k)=>{
  48. v.time -= dt;
  49. if(v.time <= 0) {
  50. v.call && v.call();
  51. toDel.push(k);
  52. }
  53. })
  54. toDel.forEach(v=>{
  55. this.timer.delete(v);
  56. })
  57. }
  58. updateSec(dt) {
  59. if (this.isGamePause) return;
  60. this.sysLogics.forEach((v)=>{
  61. // v['updateSec'](dt);
  62. v.gameUpdateSec(dt);
  63. });
  64. }
  65. end(){
  66. cc.director.getScheduler().unschedule(this.update,this);
  67. cc.director.getScheduler().unschedule(this.updateSec,this);
  68. }
  69. addSysLogic(logic: GameSysLogic){
  70. this.sysLogics.set(logic.logicId, logic);
  71. }
  72. removeSysLogic(logic: GameSysLogic){
  73. this.sysLogics.delete(logic.logicId);
  74. }
  75. timer:Map<number, {time: number, call: Function}> = new Map();
  76. idBase_timer = 0;
  77. setGameTimeout(call:Function, time: number){
  78. let id = this.idBase_timer++;
  79. this.timer.set(id, {time, call})
  80. return id;
  81. }
  82. clearGameTimeout(id){
  83. this.timer.delete(id);
  84. }
  85. };