Game
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

game.js 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. var keymap = {
  2. 32: "jump",
  3. touch: "jump"
  4. };
  5. /*
  6. * Collision box
  7. */
  8. function Box(width, height, pos) {
  9. pos = pos || {};
  10. this.width = width;
  11. this.height = height;
  12. this.pos = new Vec2(pos.x, pos.y);
  13. }
  14. Box.prototype.collidesWith = function(box, ent1pos, ent2pos) {
  15. var x1 = ent1pos.x + this.pos.x;
  16. var x2 = ent2pos.x + box.pos.x;
  17. var y1 = ent1pos.y + this.pos.y;
  18. var y2 = ent2pos.y + box.pos.y;
  19. var w1 = this.width;
  20. var w2 = box.width;
  21. var h1 = this.height;
  22. var h2 = box.height;
  23. return ((x1 + w1 >= x2 && x1 <= x2 + w2) &&
  24. (y1 + h1 >= y2 && y1 <= y2 + h2));
  25. }
  26. Box.prototype.trace = function(ctx) {
  27. ctx.beginPath();
  28. ctx.moveTo(this.pos.x, this.pos.y);
  29. ctx.lineTo(this.pos.x, this.pos.y + this.height);
  30. ctx.lineTo(this.pos.x + this.width, this.pos.y + this.height);
  31. ctx.lineTo(this.pos.x + this.width, this.pos.y);
  32. ctx.closePath();
  33. }
  34. /*
  35. * Collision shape
  36. */
  37. function Shape(ent) {
  38. this.ent = ent;
  39. this.boxes = [];
  40. this._height = -1;
  41. this._width = -1;
  42. }
  43. Shape.prototype.push = function(box) {
  44. this._height = -1;
  45. this._width = -1;
  46. this.boxes.push(box);
  47. }
  48. Shape.prototype.pop = function() {
  49. this._height = -1;
  50. this._width = -1;
  51. return this.boxes.pop();
  52. }
  53. Shape.prototype.draw = function(ctx) {
  54. for (var i = 0; i < this.boxes.length; ++i) {
  55. var box = this.boxes[i];
  56. box.trace(ctx);
  57. ctx.stroke();
  58. ctx.fill();
  59. }
  60. }
  61. Shape.prototype.collidesWith = function(shape) {
  62. for (var i = 0; i < this.boxes.length; ++i) {
  63. var box = this.boxes[i];
  64. for (var j = 0; j < shape.boxes.length; ++j) {
  65. var otherBox = shape.boxes[j];
  66. if (box.collidesWith(otherBox, this.ent.pos, shape.ent.pos))
  67. return true;
  68. }
  69. }
  70. return false;
  71. }
  72. Shape.prototype.width = function() {
  73. if (this._width !== -1)
  74. return this._width;
  75. var minX = 0;
  76. var maxX = 0;
  77. this.boxes.forEach(function(box) {
  78. if (box.pos.x < minX)
  79. minX = box.pos.x;
  80. if (box.pos.x + box.width > maxX)
  81. maxX = box.pos.x + box.width;
  82. });
  83. this._width = Math.abs(maxX - minX);
  84. return this._width;
  85. }
  86. Shape.prototype.height = function() {
  87. if (this._height !== -1)
  88. return this._height;
  89. var minY = 0;
  90. var maxY = 0;
  91. this.boxes.forEach(function(box) {
  92. if (box.pos.y < minY)
  93. minY = box.pos.y;
  94. if (box.pos.y + box.height > maxY)
  95. maxY = box.pos.y + box.height;
  96. });
  97. this._height = Math.abs(maxY - minY);
  98. return this._height;
  99. }
  100. // Make entity from object
  101. function makeEnt(obj, game, mass) {
  102. obj.pos = new Vec2();
  103. obj.vel = new Vec2();
  104. obj.force = new Vec2();
  105. obj.game = game;
  106. obj.shape = new Shape(obj);
  107. obj.moves = false;
  108. obj.dead = false;
  109. obj.inputListener = false;
  110. obj.collude = true;
  111. obj.layer = 0;
  112. obj.mass = mass || 0;
  113. obj.forceScalar = 1 / obj.mass;
  114. }
  115. /*
  116. * Game
  117. */
  118. function Game(canvas) {
  119. this.canvas = canvas;
  120. this.ctx = canvas.getContext("2d");
  121. this.raf = null;
  122. this.prevTime = null;
  123. this.stopped = true;
  124. this.camera = new Vec2();
  125. this.worldgen = null;
  126. this.entities = [];
  127. this.layers = [];
  128. this.inputListeners = [];
  129. this.keys = {};
  130. this.onkey = function onkey(evt) {
  131. var down = (evt.type === "keydown" || evt.type === "touchstart");
  132. var code = evt.keyCode || "touch";
  133. var name = keymap[code];
  134. if (name) {
  135. this.keys[name] = down;
  136. for (var i = 0; i < this.inputListeners.length; ++i) {
  137. var ent = this.inputListeners[i];
  138. if (ent === null) {
  139. continue;
  140. } else if (ent.dead) {
  141. delete this.inputListeners[i];
  142. continue;
  143. }
  144. this.inputListeners[i].onInput(name, down);
  145. }
  146. }
  147. }.bind(this);
  148. }
  149. Game.prototype.start = function(worldgen) {
  150. window.addEventListener("keydown", this.onkey);
  151. window.addEventListener("keyup", this.onkey);
  152. window.addEventListener("touchstart", this.onkey);
  153. window.addEventListener("touchend", this.onkey);
  154. this.prevTime = new Date().getTime();
  155. this.stopped = false;
  156. this.worldgen = worldgen;
  157. this.update();
  158. }
  159. Game.prototype.spawn = function(ent) {
  160. this.entities.push(ent);
  161. if (!this.layers[ent.layer])
  162. this.layers[ent.layer] = [];
  163. this.layers[ent.layer].push(ent);
  164. if (ent.inputListener)
  165. this.inputListeners.push(ent);
  166. }
  167. Game.prototype.update = function() {
  168. var time = new Date().getTime();
  169. var dt = time - this.prevTime;
  170. if (this.stopped)
  171. return;
  172. // Go through and update
  173. var xRatio = 1 / (1 + (dt * 0.005));
  174. for (var i = 0; i < this.entities.length; ++i) {
  175. var ent = this.entities[i];
  176. // Remove dead entities, replace them with the last entity
  177. if (ent.dead) {
  178. if (i + 1 === this.entities.length) {
  179. this.entities.pop();
  180. } else {
  181. this.entities[i] = this.entities.pop();
  182. ent = this.entities[i];
  183. }
  184. }
  185. if (ent.update)
  186. ent.update();
  187. if (ent.moves) {
  188. ent.force.scale(ent.forceScalar * dt);
  189. ent.vel.add(ent.force);
  190. ent.force.set({ x: 0, y: 0 });
  191. ent.vel.scale(xRatio);
  192. ent.pos.add(ent.vel.clone().scale(dt));
  193. }
  194. if (ent.move)
  195. ent.move();
  196. }
  197. // Exit if stopped
  198. if (this.stopped)
  199. return;
  200. // Tick worldgen
  201. this.worldgen.update();
  202. // Go through and draw
  203. this.canvas.width = this.canvas.width;
  204. for (var i = 0; i < this.layers.length; ++i) {
  205. var layer = this.layers[i];
  206. for (var j = 0; j < layer.length; ++j) {
  207. var ent = layer[j];
  208. // Remove dead entities, replace them with the last entity
  209. if (ent.dead) {
  210. if (i + 1 === this.entities.length) {
  211. layer.pop();
  212. } else {
  213. layer[j] = layer.pop();
  214. ent = layer[j];
  215. }
  216. }
  217. if (ent.collude && ent.pos.x + ent.shape.width() < this.camera.x)
  218. continue;
  219. if (ent.collude && ent.pos.x > this.camera.x + this.canvas.width)
  220. continue;
  221. this.ctx.save();
  222. this.ctx.translate(
  223. ent.pos.x - this.camera.x,
  224. ent.pos.y - this.camera.y);
  225. ent.draw(this.ctx);
  226. this.ctx.restore();
  227. }
  228. }
  229. // Clear presses
  230. for (var i in this.presses) {
  231. this.presses[i] = false;
  232. }
  233. this.prevTime = time;
  234. if (!this.stopped)
  235. this.raf = reqAnimFrame(this.update.bind(this));
  236. }
  237. Game.prototype.stop = function(score) {
  238. this.stopped = true;
  239. cancelAnimFrame(this.raf);
  240. window.removeEventListener("keyup", this.onkey);
  241. window.removeEventListener("keydown", this.onkey);
  242. if (this.onstop)
  243. this.onstop(Math.floor(score));
  244. }