PageRenderTime 25ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/files/engine2d/1.0.1/engine2d.js

https://gitlab.com/Mirros/jsdelivr
JavaScript | 232 lines | 111 code | 2 blank | 119 comment | 25 complexity | 3c4d6f4a1d3179deba267216d9c869ac MD5 | raw file
  1. /**
  2. * Engine2D game engine v1.0.1
  3. * License: http://git.io/vlp11
  4. * @author jackdalton
  5. */
  6. /**
  7. * Engine2D namespace
  8. *
  9. * @namespace
  10. */
  11. var Engine2D = {
  12. TYPE: {
  13. RECT: 1,
  14. CIRCLE: 2
  15. },
  16. /**
  17. * 2D vector constructor.
  18. *
  19. * @constructor
  20. * @param {Number} x - X position
  21. * @param {Number} y - Y position
  22. * @this {Vector2}
  23. */
  24. Vector2: function(x, y) {
  25. var self = this;
  26. x = x || 0;
  27. y = y || 0;
  28. /**
  29. * Sets the vector position.
  30. *
  31. * @memberof Engine2D.Vector2
  32. * @param {Vector2} position - Desired vector position
  33. */
  34. self.setPos = function(position) {
  35. x = position.getX();
  36. y = position.getY();
  37. };
  38. /**
  39. * Gets the vector x position.
  40. *
  41. * @memberof Engine2D.Vector2
  42. * @returns {Number} x - Vector x position.
  43. */
  44. self.getX = function() {
  45. return x;
  46. };
  47. /**
  48. * Gets the vector y position.
  49. *
  50. * @memberof Engine2D.Vector2
  51. * @returns {Number} y - Vector y position.
  52. */
  53. self.getY = function() {
  54. return y;
  55. };
  56. /**
  57. * Gets the full vector position.
  58. *
  59. * @memberof Engine2D.Vector2
  60. * @returns {Object} position - An object containing properties `x` and `y`, representing vector positions.
  61. */
  62. self.getPos = function() {
  63. return {
  64. x: x,
  65. y: y
  66. };
  67. };
  68. /**
  69. * Calculates the distance to another vector position.
  70. *
  71. * @memberof Engine2D.Vector2
  72. * @param {Vector2} pos - Position to calculate distance to.
  73. * @returns {Number} distance - Distance to other vector.
  74. */
  75. self.distanceTo = function(pos) {
  76. return (Math.sqrt(Math.pow(pos.getX() - x, 2) + Math.pow(pos.getY() - y, 2)));
  77. };
  78. /**
  79. * Finds the midpoint between this and another vector position.
  80. *
  81. * @memberof Engine2D.Vector2
  82. * @param {Vector2} pos - Position to calculate midpoint for.
  83. * @returns {Vector2} midpoint - Midpoint between this and another vector position.
  84. */
  85. self.midpoint = function(pos) {
  86. return new Engine2D.Vector2((x + pos.getX()) / 2, (y + pos.getY()) / 2);
  87. };
  88. /**
  89. * Performs a vector movement.
  90. *
  91. * @memberof Engine2D.Vector2
  92. * @param {Number} plusX - X value to add to vector x position.
  93. * @param {Number} plusY - Y value to add to vector y position.
  94. */
  95. self.vectorMovement = function(plusX, plusY) {
  96. x += plusX;
  97. y += plusY;
  98. };
  99. },
  100. /**
  101. * Engine2D game scene constructor.
  102. *
  103. * @constructor
  104. */
  105. GameScene: function() {
  106. var self = this;
  107. self.objects = {};
  108. /**
  109. * Checks whether a game object ID is valid or not.
  110. *
  111. * @private
  112. * @param {string} objectId - ID to validate.
  113. * @returns {boolean} - Whether the generated object ID is valid or not.
  114. */
  115. var isValidID = function(objectId) {
  116. var pass;
  117. for (var i in self.objects) {
  118. if (self.objects[i].id == objectId) pass = true;
  119. }
  120. if (!!pass)
  121. return true;
  122. else
  123. return false;
  124. };
  125. /**
  126. * Adds game object to scene.
  127. *
  128. * @param {Object} gameObject - Valid Engine2D game object to add to scene.
  129. * @memberof Engine2D.GameScene
  130. * @returns {string} id - ID of game object added to scene.
  131. */
  132. self.addObject = function(gameObject) {
  133. var pass;
  134. for (var i in Engine2D.TYPE) {
  135. if (gameObject.type == Engine2D.TYPE[i]) pass = true;
  136. }
  137. if (!!pass) {
  138. self.objects[gameObject.id] = gameObject;
  139. return gameObject.id;
  140. } else
  141. throw new TypeError("\"" + gameObject.type + "\" is not a valid Engine2D game object type.");
  142. };
  143. /**
  144. * Disables a game object.
  145. *
  146. * @param {string} objectId - ID of desired object in the scene to disable.
  147. * @memberof Engine2D.GameScene
  148. */
  149. self.disableObject = function(objectId) {
  150. if (isValidID(objectId))
  151. self.objects[objectId].alive = false;
  152. else
  153. throw new ReferenceError("Object \"" + objectId + "\" either doesn't exist, or hasn't been added to the scene.");
  154. };
  155. /**
  156. * Enables a game object.
  157. *
  158. * @param {string} objectId - ID of desired object in the scene to enable.
  159. * @memberof Engine2D.GameScene
  160. */
  161. self.enableObject = function(objectId) {
  162. if (isValidID(objectId))
  163. self.objects[objectId].alive = true;
  164. else
  165. throw new ReferenceError("Object \"" + objectId + "\" either doesn't exist, or hasn't been added to the scene.");
  166. };
  167. /**
  168. * Permanently destroys a game object.
  169. *
  170. * @param {string} objectId - ID of desired object in the scene to destroy.
  171. * @memberof Engine2D.GameScene
  172. */
  173. self.destroyObject = function(objectId) {
  174. if (isValidID(objectId))
  175. delete self.objects[objectId];
  176. else
  177. throw new ReferenceError("Object \"" + objectId + "\" either doesn't exist, or hasn't been added to the scene.");
  178. };
  179. },
  180. /**
  181. * Engine2D rectangle constructor.
  182. *
  183. * @constructor
  184. * @this {Rect}
  185. * @param {Object} options - An object specifying various aspects of a rectangle.
  186. */
  187. Rect: function(options) {
  188. var self = this;
  189. options = options || {};
  190. self.id = options.id || Engine2D.randomID();
  191. self.type = Engine2D.TYPE.RECT;
  192. self.alive = options.alive || true;
  193. self.size = {}, self.position = {};
  194. self.size.width = options.width || 0;
  195. self.size.height = options.height || 0;
  196. self.position = options.position || new Engine2D.Vector2(0, 0);
  197. },
  198. /**
  199. * Engine2D circle constructor.
  200. *
  201. * @constructor
  202. * @this {Circle}
  203. * @param {Object} options - An object specifying various aspects of a circle.
  204. */
  205. Circle: function(options) {
  206. var self = this;
  207. options = options || {};
  208. self.id = options.id || Engine2D.randomID();
  209. self.type = Engine2D.TYPE.CIRCLE;
  210. self.alive = options.alive || true;
  211. self.size = {}, self.position = {};
  212. self.size.radius = options.radius || 0;
  213. self.position = options.position || new Engine2D.Vector2(0, 0);
  214. },
  215. /**
  216. * Used to generate random object IDs for Engine2D game objects.
  217. *
  218. * @returns {string} - Randomly generated object ID.
  219. */
  220. randomID: function() {
  221. var opts = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", out = "";
  222. var i = 0;
  223. while (i < 5) {
  224. i++;
  225. out += opts[Math.floor(Math.random() * opts.length)];
  226. }
  227. return out;
  228. }
  229. };
  230. typeof module !== "undefined" ? module.exports = Engine2D : window.Engine2D = Engine2D;