PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/js/controller/FieldController.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 187 lines | 113 code | 31 blank | 43 comment | 14 complexity | a3c53f7029c434d9e64d8894e2583fe6 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. (function () {
  2. RealtimeMultiplayerGame.namespace("RealtimeMultiplayerGame.Controller");
  3. RealtimeMultiplayerGame.Controller.FieldController = function () {
  4. this.entities = new SortedLookupTable();
  5. this.players = new SortedLookupTable();
  6. };
  7. RealtimeMultiplayerGame.Controller.FieldController.prototype = {
  8. entities: null, // A SortedLookupTable for all entities
  9. players: null, // A SortedLookupTable for players only, stored using client.getClientid()
  10. /**
  11. * Update all entities
  12. * @param {Number} speedFactor A number signifying how much faster or slower we are moving than the target framerate
  13. * @param {Number} gameClock Current game time in seconds (zero based)
  14. * @param {Number} gameTick Current game tick (incrimented each frame)
  15. */
  16. tick: function (speedFactor, gameClock, gameTick) {
  17. // DO SOME STUFF
  18. },
  19. /**
  20. * Internal function. Adds an entity to our collection, and adds it to the view if we have one
  21. * @param anEntity An entity to add, should already be created and contain a unique entityid
  22. */
  23. addEntity: function (anEntity) {
  24. this.entities.setObjectForKey(anEntity, anEntity.entityid);
  25. // If we have a view, then add the player to it
  26. if (this.view) {
  27. this.view.addEntity(anEntity.getView());
  28. }
  29. },
  30. /**
  31. * Updates the entity based on new information (called by AbstractClientGame::renderAtTime)
  32. * @param {int} entityid entityid we want to update
  33. * @param {RealtimeMultiplayerGame.model.Point} newPosition position
  34. * @param {Number} newRotation rotation
  35. * @param {Object} newEntityDescription The full contents of the the snapshots newEntityDescription
  36. */
  37. updateEntity: function (entityid, newPosition, newRotation, newEntityDescription) {
  38. var entity = this.entities.objectForKey(entityid);
  39. if (entity != null) {
  40. entity.position.x = newPosition.x;
  41. entity.position.y = newPosition.y;
  42. entity.rotation = newRotation;
  43. entity.lastReceivedEntityDescription = newEntityDescription;
  44. } else {
  45. console.log("(FieldController)::updateEntity - Error: Cannot find entity with entityid", entityid);
  46. }
  47. },
  48. ///// Memory
  49. addPlayer: function (aPlayerEntity) {
  50. this.addEntity(aPlayerEntity);
  51. this.players.setObjectForKey(aPlayerEntity, aPlayerEntity.clientid);
  52. },
  53. /**
  54. * Remove a player.
  55. * Does player stuff, then calls removeEntity.
  56. * @param clientid ConnectionID of the player who jumped out of the game
  57. */
  58. removePlayer: function (clientid) {
  59. var player = this.players.objectForKey(clientid);
  60. if (!player) {
  61. console.log("(FieldController), No 'Character' with clientid " + clientid + " ignoring...");
  62. return;
  63. }
  64. this.removeEntity(player.entityid);
  65. this.players.remove(player.clientid);
  66. },
  67. /**
  68. * Removes an entity by it's ID
  69. * @param entityid
  70. */
  71. removeEntity: function (entityid) {
  72. var entity = this.entities.objectForKey(entityid);
  73. if (this.view)
  74. this.view.removeEntity(entity.view);
  75. entity.dealloc();
  76. this.entities.remove(entityid);
  77. },
  78. /**
  79. * Checks an array of "active entities", against the existing ones.
  80. * It's used to remove entities that expired in between two updates
  81. * @param activeEntities
  82. */
  83. removeExpiredEntities: function (activeEntities) {
  84. var entityKeysArray = this.entities._keys;
  85. var i = entityKeysArray.length;
  86. var key;
  87. var totalRemoved = 0;
  88. while (i--) {
  89. key = entityKeysArray[i];
  90. // This entity is still active. Move along.
  91. if (activeEntities[key])
  92. continue;
  93. // This entity is not active, check if it belongs to the server
  94. var entity = this.entities.objectForKey(key);
  95. var isPlayer = this.players.objectForKey(entity.clientid) != null;
  96. // Remove special way if player (which calls removeEntity on itself as well), or just remove it as an entity
  97. if (isPlayer) {
  98. this.removePlayer(entity.clientid);
  99. } else {
  100. this.removeEntity(entity.entityid);
  101. }
  102. totalRemoved++;
  103. }
  104. },
  105. dealloc: function () {
  106. this.players.forEach(function (key, entity) {
  107. this.removePlayer(entity.clientid);
  108. }, this);
  109. this.players.dealloc();
  110. this.players = null;
  111. this.entities.forEach(function (key, entity) {
  112. this.removeEntity(entity.entityid);
  113. }, this);
  114. this.entities.dealloc();
  115. this.entities = null;
  116. this.view = null;
  117. },
  118. ///// Accessors
  119. // Will be called on client side
  120. setView: function (aView) {
  121. var theInterface = RealtimeMultiplayerGame.Controller.FieldControllerViewProtocol;
  122. for (var member in theInterface) {
  123. if ((typeof aView[member] != typeof theInterface[member])) {
  124. console.log("object failed to implement interface member " + member);
  125. return false;
  126. }
  127. }
  128. // Checks passed
  129. this.view = aView;
  130. },
  131. getView: function () {
  132. return this.view
  133. },
  134. getEntities: function () {
  135. return this.entities
  136. },
  137. getPlayers: function () {
  138. return this.players;
  139. },
  140. getEntityWithid: function (anEntityid) {
  141. return this.entities.objectForKey(anEntityid);
  142. },
  143. getPlayerWithid: function (aClientid) {
  144. return this.players.objectForKey(aClientid);
  145. }
  146. };
  147. /**
  148. * Required methods for the FieldControllerView delegate
  149. */
  150. RealtimeMultiplayerGame.Controller.FieldControllerViewProtocol = {
  151. addEntity: function (anEntityView) {
  152. },
  153. dealloc: function () {
  154. }
  155. }
  156. })();