GameBase.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // GameBase.js
  2. // GameBase Class
  3. // FZ, Copyright (c) 2010 Zlongames
  4. (function()
  5. {
  6. //by mliao
  7. FZ.stateStatus = {};
  8. FZ.stateStatus.INIT = 0;
  9. FZ.stateStatus.NORMAL = 5;
  10. FZ.stateStatus.HIDENOTIFY = 15;
  11. FZ.stateStatus.HIDELOGIC = 20;
  12. FZ.stateStatus.DONOTHING = 30;
  13. FZ.GameStateBase=FZ.newClass(
  14. {
  15. StateName:"GameStateBase",
  16. //by mliao
  17. m_status : FZ.stateStatus.NORMAL,
  18. init:function()
  19. {
  20. // register the state into the GameBase
  21. FZ.GameBase.registerState(this.StateName, this);
  22. },
  23. //by mliao
  24. pause : function() {
  25. this.m_status = FZ.stateStatus.HIDENOTIFY;
  26. },
  27. resume : function() {
  28. this.m_status = FZ.stateStatus.NORMAL;
  29. },
  30. preProcess: function(){},
  31. postProcess: function(){},
  32. AIUpdate:function(deltaTime){},
  33. visualUpdate:function(){}
  34. });
  35. FZ.GameBase = {
  36. Time:(new Date()).getTime(),
  37. FrameNum:0,
  38. // fps
  39. Fps:0,
  40. Fps_lastTime :0,
  41. Fps_lastFrame:0,
  42. // update
  43. DefaultFPS:30,
  44. CurrentState:null,
  45. __StateLib:{},
  46. //add by mliao
  47. __stateStack : [],
  48. Width:640,
  49. Height:480,
  50. MainCanvas:null,
  51. MainContext:null,
  52. Name:"",
  53. SaveObject:{},
  54. init: function(theDiv)
  55. {
  56. this.MainCanvas = document.createElement("canvas");
  57. this.MainCanvas.width = this.Width;
  58. this.MainCanvas.height = this.Height;
  59. this.MainContext = this.MainCanvas.getContext("2d");
  60. this.MainCanvas.addEventListener("mousemove", FZ.Tools.bindWithEvent(FZ.ControlManager, FZ.ControlManager.MouseMove), false);
  61. this.MainCanvas.addEventListener("mousedown", FZ.Tools.bindWithEvent(FZ.ControlManager, FZ.ControlManager.MouseDown),false);
  62. this.MainCanvas.addEventListener("mouseup", FZ.Tools.bindWithEvent(FZ.ControlManager, FZ.ControlManager.MouseUp),false);
  63. this.theInterval = setInterval(FZ.Tools.bind(this, this.update), 1000/this.DefaultFPS);
  64. theDiv.appendChild(this.MainCanvas);
  65. },
  66. update:function()
  67. {
  68. //by mliao
  69. var index = 0;
  70. var tmpState = null;
  71. this.FrameNum++;
  72. var now=(new Date()).getTime();
  73. var deltaTime =( now-this.Time)/1000.0;
  74. this.Time=now;
  75. // update the fps
  76. if (this.time - this.Fps_lastTime>1000)
  77. {
  78. this.Fps =Math.round( 10*(this.FrameNum - this.Fps_lastFrame)/((this.Time - this.Fps_lastTime)/1000) )/10;
  79. this.Fps_lastTime = this.Time;
  80. this.Fps_lastFrame = this.FrameNum;
  81. }
  82. for(index = 0; index < this.__stateStack.length; index++) {
  83. tmpState = this.__stateStack[index];
  84. if((tmpState)&& (tmpState !== this.CurrentState)) {
  85. if(FZ.stateStatus.NORMAL === tmpState.m_status) {
  86. tmpState.AIUpdate(deltaTime);
  87. tmpState.visualUpdate();
  88. }
  89. else if(FZ.stateStatus.HIDENOTIFY === tmpState.m_status) {
  90. tmpState.visualUpdate();
  91. }
  92. else if(FZ.stateStatus.HIDELOGIC === tmpState.m_status) {
  93. tmpState.AIUpdate(deltaTime);
  94. }
  95. else if(FZ.stateStatus.DONOTHING === tmpState.m_status) {
  96. }
  97. }
  98. }
  99. if(null !== this.CurrentState) {
  100. this.CurrentState.AIUpdate(deltaTime);
  101. this.CurrentState.visualUpdate();
  102. }
  103. // AudioUpdate
  104. // this.AudioManager.update(deltaTime);
  105. },
  106. saveGame:function()
  107. {
  108. FZ._assert(this.Name != "", "Please set the name of the game first");
  109. //localStorage["FZ.Game."+this.Name] = FZ.JSON.stringify(this.SaveObject);
  110. },
  111. loadGame:function()
  112. {
  113. this.SaveObject = {};
  114. return false;
  115. FZ._assert(this.Name != "", "Please set the name of the game first");
  116. if (this.Name == "")
  117. return false;
  118. if (!localStorage.hasOwnProperty("FZ.Game."+this.Name) || 1 == 1)
  119. {
  120. this.SaveObject = {};
  121. return false;
  122. }
  123. this.SaveObject = FZ.JSON.parse(localStorage["FZ.Game."+this.Name]);
  124. return true;
  125. },
  126. registerState:function(StateName, StateObject)
  127. {
  128. this.__StateLib[StateName] = StateObject;
  129. },
  130. getState : function(stateName) {
  131. return this.__StateLib[stateName];
  132. },
  133. switchToState:function(newState)
  134. {
  135. var preStateName = this.CurrentState.StateName;
  136. var _args = [preStateName];
  137. Array.prototype.push.apply(_args, arguments);
  138. this.CurrentState.postProcess();
  139. this.CurrentState = this.__StateLib[newState];
  140. // this.CurrentState.preProcess(preStateName);
  141. this.CurrentState.preProcess.apply(this.CurrentState, _args);
  142. FZ.PrintLog("switchToState");
  143. },
  144. //by mliao
  145. resetAllState : function() {
  146. var delNum = 0;
  147. // this.CurrentState = null;
  148. delNum = this.__stateStack.length;
  149. this.__stateStack.splice(0, delNum);
  150. },
  151. pushState : function(stateName) {
  152. this.__stateStack.push(this.__StateLib[stateName]);
  153. },
  154. //
  155. // popState : function(sender) {
  156. //// this.CurrentState = null;
  157. // this.CurrentState = this.__stateStack.pop();
  158. // if((undefined !== this.CurrentState) && (null !== this.CurrentState)) {
  159. // if(this.CurrentState.resume){
  160. // this.CurrentState.resume(sender);
  161. // }
  162. // }
  163. // }
  164. popState : function() {
  165. // this.CurrentState = null;
  166. this.CurrentState = this.__stateStack.pop();
  167. if((undefined !== this.CurrentState) && (null !== this.CurrentState)) {
  168. if(this.CurrentState.resume){
  169. this.CurrentState.resume.apply(this.CurrentState, arguments);
  170. }
  171. }
  172. }
  173. };
  174. FZ.GameBase.CurrentState = new FZ.GameStateBase();
  175. })();