PageRenderTime 39ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/js/core/AbstractGame.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 120 lines | 63 code | 16 blank | 41 comment | 2 complexity | 487d8607d418da8cca854a851d8c91b0 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. /**
  2. File:
  3. AbstractGame.js
  4. Created By:
  5. Mario Gonzalez
  6. Project:
  7. RealtimeMultiplayerNodeJS
  8. Abstract:
  9. This class is the base Game controller in RealtimeMultiplayerGame it provides things such as, keeping track of the current game clock, starting and stopping the game clock
  10. Basic Usage:
  11. [This class is not instantiated! - below is an example of using this class by extending it]
  12. (function(){
  13. MyGameClass = function() {
  14. return this;
  15. }
  16. RealtimeMultiplayerGame.extend(MyGameClass, RealtimeMultiplayerGame.AbstractGame, null);
  17. };
  18. */
  19. (function () {
  20. RealtimeMultiplayerGame.AbstractGame = function () {
  21. this.setupNetChannel();
  22. this.setupCmdMap();
  23. this.fieldController = new RealtimeMultiplayerGame.Controller.FieldController();
  24. return this;
  25. };
  26. RealtimeMultiplayerGame.AbstractGame.prototype = {
  27. // Properties
  28. gameClockReal: 0, // Actual time via "new Date().getTime();"
  29. gameClock: 0, // Seconds since start
  30. gameTick: 0, // Ticks since start
  31. isRunning: true,
  32. speedFactor: 1, // Used to create Framerate Independent Motion (FRIM) - 1.0 means running at exactly the correct speed, 0.5 means half-framerate. (otherwise faster machines which can update themselves more accurately will have an advantage)
  33. intervalGameTick: null, // Setinterval for gametick
  34. intervalFramerate: 60, // Try to call our tick function this often, intervalFramerate, is used to determin how often to call settimeout - we can set to lower numbers for slower computers
  35. intervalTargetDelta: NaN, // this.targetDelta, milliseconds between frames. Normally it is 16ms or 60FPS. The framerate the game is designed against - used to create framerate independent motion
  36. gameDuration: Number.MAX_VALUE, // Gameduration
  37. netChannel: null, // ServerNetChannel / ClientNetChannel determined by subclass
  38. fieldController: null, // FieldController
  39. cmdMap: {},
  40. /**
  41. * Setup the ClientNetChannel or ServerNetChannel
  42. */
  43. setupNetChannel: function () {
  44. },
  45. /**
  46. * setup the command mapping for the events recevied from netchannel
  47. */
  48. setupCmdMap: function () {
  49. },
  50. // Methods
  51. tick: function () {
  52. // Store previous time and update current
  53. var oldTime = this.gameClockReal;
  54. this.gameClockReal = new Date().getTime();
  55. // Our clock is zero based, so if for example it says 10,000 - that means the game started 10 seconds ago
  56. var delta = this.gameClockReal - oldTime;
  57. this.gameClock += delta;
  58. this.gameTick++;
  59. // Framerate Independent Motion -
  60. // 1.0 means running at exactly the correct speed, 0.5 means half-framerate. (otherwise faster machines which can update themselves more accurately will have an advantage)
  61. this.speedFactor = delta / ( this.intervalTargetDelta );
  62. if (this.speedFactor <= 0) this.speedFactor = 1;
  63. this.fieldController.tick(this.speedFactor, this.gameClockReal, this.gameTick);
  64. },
  65. /**
  66. * Start/Restart the game tick
  67. */
  68. startGameClock: function () {
  69. var that = this;
  70. this.gameClockReal = new Date().getTime();
  71. this.intervalTargetDelta = Math.floor(1000 / this.intervalFramerate);
  72. this.intervalGameTick = setInterval(function () {
  73. that.tick()
  74. }, this.intervalTargetDelta);
  75. },
  76. /**
  77. * Stop the game tick
  78. */
  79. stopGameClock: function () {
  80. clearInterval(RealtimeMultiplayerGame.AbstractGame.prototype.intervalGameTick);
  81. clearTimeout(RealtimeMultiplayerGame.AbstractGame.prototype.intervalGameTick);
  82. },
  83. setGameDuration: function () {
  84. },
  85. // Memory
  86. dealloc: function () {
  87. if (this.netChannel) this.netChannel.dealloc();
  88. this.netChannel = null;
  89. clearInterval(this.intervalGameTick);
  90. },
  91. log: function () {
  92. // OVERRIDE or USE CONSOLE.LOG
  93. },
  94. ///// Accessors
  95. getGameClock: function () {
  96. return this.gameClock;
  97. },
  98. getGameTick: function () {
  99. return this.gameTick;
  100. }
  101. }
  102. })();