/js/model/GameEntity.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs · JavaScript · 174 lines · 97 code · 19 blank · 58 comment · 11 complexity · 6894b853aef2818af4691cbc661dc2b0 MD5 · raw file

  1. /**
  2. File:
  3. GameEntity.js
  4. Created By:
  5. Mario Gonzalez
  6. Project:
  7. RealtimeMultiplayerNodeJS
  8. Abstract:
  9. This class is the base GameEntity class in RealtimeMultiplayerGame, it contains a position rotation, health
  10. Basic Usage:
  11. var badGuy = new RealtimeMultiplayerGame.GameEntity();
  12. badGuy.position.x += 1;
  13. */
  14. (function () {
  15. RealtimeMultiplayerGame.namespace("RealtimeMultiplayerGame.model");
  16. RealtimeMultiplayerGame.model.GameEntity = function (anEntityid, aClientid) {
  17. this.clientid = aClientid;
  18. this.entityid = anEntityid;
  19. this.traits = [];
  20. this.position = new RealtimeMultiplayerGame.model.Point(0, 0);
  21. return this;
  22. };
  23. RealtimeMultiplayerGame.model.GameEntity.prototype = {
  24. // Connection info
  25. clientid: -1, // Owner of this object
  26. entityid: -1, // UUID for this entity
  27. entityType: -1, // A special interger representing the entityType sent via along with other network info
  28. position: RealtimeMultiplayerGame.model.Point.prototype.ZERO, // Current position of this entity
  29. rotation: 0,
  30. traits: null, // An array of our traits, in reverse added order
  31. view: null,
  32. lastReceivedEntityDescription: null, // The last received entity description (set by renderAtTime)
  33. /**
  34. * Update the view's position
  35. */
  36. updateView: function () {
  37. // OVERRIDE
  38. },
  39. /**
  40. * Updates the position of this GameEntity based on it's movement properties (velocity, acceleration, damping)
  41. * @param {Number} speedFactor A number signifying how much faster or slower we are moving than the target framerate
  42. * @param {Number} gameClock Current game time in seconds (zero based)
  43. * @param {Number} gameTick Current game tick (incrimented each frame)
  44. */
  45. updatePosition: function (speedFactor, gameClock, gameTick) {
  46. // OVERRIDE
  47. },
  48. /**
  49. * Construct an entity description for this object, it is essentually a CSV so you have to know how to read it on the receiving end
  50. * @param wantsFullUpdate If true, certain things that are only sent when changed are always sent
  51. */
  52. constructEntityDescription: function (gameTick, wantsFullUpdate) {
  53. // Note: "~~" is just a way to round the value without the Math.round function call
  54. var returnString = this.entityid;
  55. returnString += "," + this.clientid;
  56. returnString += "," + this.entityType;
  57. returnString += "," + ~~this.position.x;
  58. returnString += "," + ~~this.position.y;
  59. return returnString;
  60. },
  61. ////// TRAIT SUPPORT
  62. /**
  63. * Adds and attaches a trait (already created), to this entity.
  64. * The trait is only attached if we do not already have one of the same type attached, or don't care (aTrait.canStack = true)
  65. * @param {RealtimeMultiplayerGame.controller.traits.BaseTrait} aTrait A BaseTrait instance
  66. * @return {Boolean} Whether the trait was added
  67. */
  68. addTrait: function (aTrait) {
  69. // Check if we already have this trait, if we do - make sure the trait allows stacking
  70. var existingVersionOfTrait = this.getTraitWithName(aTrait.displayName);
  71. if (existingVersionOfTrait && !existingVersionOfTrait.canStack) {
  72. return false;
  73. }
  74. // Remove existing version
  75. if (existingVersionOfTrait) {
  76. this.removeTraitWithName(aTrait.displayName);
  77. }
  78. this.traits.push(aTrait);
  79. aTrait.attach(this);
  80. return aTrait;
  81. },
  82. /**
  83. * Calls addTrait and executes it immediately
  84. * @param aTrait
  85. */
  86. addTraitAndExecute: function (aTrait) {
  87. var wasAdded = this.addTrait(aTrait);
  88. if (wasAdded) {
  89. aTrait.execute();
  90. return aTrait;
  91. }
  92. return null;
  93. },
  94. /**
  95. * Removes a trait with a matching .displayName property
  96. * @param aTraitName
  97. */
  98. removeTraitWithName: function (aTraitName) {
  99. var len = this.traits.length;
  100. var removedTraits = null;
  101. for (var i = 0; i < len; ++i) {
  102. if (this.traits[i].displayName === aTraitName) {
  103. removedTraits = this.traits.splice(i, 1);
  104. break;
  105. }
  106. }
  107. // Detach removed traits
  108. if (removedTraits) {
  109. i = removedTraits.length;
  110. while (i--) {
  111. removedTraits[i].detach();
  112. }
  113. }
  114. },
  115. /**
  116. * Removes all traits contained in this entity
  117. */
  118. removeAllTraits: function () {
  119. var i = this.traits.length;
  120. while (i--) {
  121. this.traits[i].detach();
  122. }
  123. this.traits = [];
  124. },
  125. ///// MEMORY
  126. dealloc: function () {
  127. this.position = null;
  128. this.removeAllTraits();
  129. this.traits = null;
  130. },
  131. ////// ACCESSORS
  132. setView: function (aView) {
  133. this.view = aView;
  134. },
  135. getView: function () {
  136. return this.view;
  137. },
  138. /**
  139. * Returns a trait with a matching .displayName property
  140. * @param aTraitName
  141. */
  142. getTraitWithName: function (aTraitName) {
  143. var len = this.traits.length;
  144. var trait = null;
  145. for (var i = 0; i < len; ++i) {
  146. if (this.traits[i].displayName === aTraitName) {
  147. trait = this.traits[i];
  148. break;
  149. }
  150. }
  151. return trait;
  152. }
  153. }
  154. })();