Game
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

game.js 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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.mass = mass || 0;
  111. obj.forceScalar = 1 / obj.mass;
  112. }
  113. /*
  114. * Game
  115. */
  116. function Game(canvas) {
  117. this.canvas = canvas;
  118. this.ctx = canvas.getContext("2d");
  119. this.raf = null;
  120. this.prevTime = null;
  121. this.stopped = true;
  122. this.camera = new Vec2();
  123. this.worldgen = null;
  124. this.entities = [];
  125. this.inputListeners = [];
  126. this.keys = {};
  127. this.onkey = function onkey(evt) {
  128. var down = (evt.type === "keydown" || evt.type === "touchstart");
  129. var code = evt.keyCode || "touch";
  130. var name = keymap[code];
  131. if (name) {
  132. this.keys[name] = down;
  133. for (var i = 0; i < this.inputListeners.length; ++i) {
  134. var ent = this.inputListeners[i];
  135. if (ent === null) {
  136. continue;
  137. } else if (ent.dead) {
  138. delete this.inputListeners[i];
  139. continue;
  140. }
  141. this.inputListeners[i].onInput(name, down);
  142. }
  143. }
  144. }.bind(this);
  145. }
  146. Game.prototype.start = function(worldgen) {
  147. window.addEventListener("keydown", this.onkey);
  148. window.addEventListener("keyup", this.onkey);
  149. window.addEventListener("touchstart", this.onkey);
  150. window.addEventListener("touchend", this.onkey);
  151. this.prevTime = new Date().getTime();
  152. this.stopped = false;
  153. this.worldgen = worldgen;
  154. this.update();
  155. }
  156. Game.prototype.spawn = function(ent) {
  157. this.entities.push(ent);
  158. if (ent.inputListener)
  159. this.inputListeners.push(ent);
  160. }
  161. Game.prototype.update = function() {
  162. var time = new Date().getTime();
  163. var dt = time - this.prevTime;
  164. if (this.stopped)
  165. return;
  166. // Go through and update
  167. var xRatio = 1 / (1 + (dt * 0.005));
  168. for (var i = 0; i < this.entities.length; ++i) {
  169. var ent = this.entities[i];
  170. // Remove dead entities, replace them with the last entity
  171. if (ent.dead) {
  172. if (i + 1 === this.entities.length) {
  173. this.entities.pop();
  174. } else {
  175. this.entities[i] = this.entities.pop();
  176. var ent = this.entities[i];
  177. }
  178. }
  179. if (ent.update)
  180. ent.update();
  181. if (ent.moves) {
  182. ent.force.scale(ent.forceScalar * dt);
  183. ent.vel.add(ent.force);
  184. ent.force.set({ x: 0, y: 0 });
  185. ent.vel.scale(xRatio);
  186. ent.pos.add(ent.vel.clone().scale(dt));
  187. }
  188. if (ent.move)
  189. ent.move();
  190. }
  191. // Exit if stopped
  192. if (this.stopped)
  193. return;
  194. // Tick worldgen
  195. this.worldgen.update();
  196. // Go through and draw
  197. this.canvas.width = this.canvas.width;
  198. for (var i = 0; i < this.entities.length; ++i) {
  199. var ent = this.entities[i];
  200. if (ent.dead)
  201. continue;
  202. if (ent.pos.x + ent.shape.width() < this.camera.x)
  203. continue;
  204. if (ent.pos.x > this.camera.x + this.canvas.width)
  205. continue;
  206. this.ctx.save();
  207. this.ctx.translate(
  208. ent.pos.x - this.camera.x,
  209. ent.pos.y - this.camera.y);
  210. ent.draw(this.ctx);
  211. this.ctx.restore();
  212. }
  213. // Clear presses
  214. for (var i in this.presses) {
  215. this.presses[i] = false;
  216. }
  217. this.prevTime = time;
  218. if (!this.stopped)
  219. this.raf = reqAnimFrame(this.update.bind(this));
  220. }
  221. Game.prototype.stop = function() {
  222. this.stopped = true;
  223. cancelAnimFrame(this.raf);
  224. window.removeEventListener("keyup", this.onkey);
  225. window.removeEventListener("keydown", this.onkey);
  226. if (this.onstop)
  227. this.onstop();
  228. }