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.

entities.js 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Background
  3. */
  4. function Background(game) {
  5. makeEnt(this, game, 100);
  6. this.img = assets.imgs.background;
  7. this.img1x = 0;
  8. this.img1y = -300;
  9. this.img2x = 0;
  10. this.img2y = -100;
  11. this.collude = false;
  12. this.moves = true;
  13. this.started = false;
  14. this.pos.set({ x: -400, y: 0 });
  15. }
  16. Background.prototype.update = function() {
  17. if (!this.img.ready)
  18. return;
  19. if (this.img.ready && this.img2x === 0)
  20. this.img2x = this.img1x + this.img.width * 2;
  21. if (this.game.started)
  22. this.force.add({ x: 0.09, y: 0 });
  23. if (this.img1x + this.img.width * 2 + this.pos.x < this.game.camera.x) {
  24. this.img1x = this.img2x;
  25. this.img2x += this.img.width * 2;
  26. var tmpy = this.img1y;
  27. this.img1y = this.img2y;
  28. this.img2y = tmpy;
  29. }
  30. }
  31. Background.prototype.draw = function(ctx){
  32. ctx.save();
  33. ctx.translate(this.img1x, this.img1y);
  34. ctx.scale(2, 2);
  35. this.img.draw(ctx);
  36. ctx.restore();
  37. ctx.translate(this.img2x, this.img2y);
  38. ctx.scale(2, 2);
  39. this.img.draw(ctx);
  40. }
  41. /*
  42. * Player
  43. */
  44. function Player(game) {
  45. makeEnt(this, game, 100);
  46. this.img = assets.imgs.player;
  47. game.started = false;
  48. this.moves = true;
  49. this.inputListener = true;
  50. this.layer = 1;
  51. this.invincible = false;
  52. this.invincibleTimeout = null;
  53. this.started = false;
  54. this.rotation = 0;
  55. this.bigBox = new Box(60, 40, { x: -30, y: -20 });
  56. this.box = new Box();
  57. this.shape.push(this.box);
  58. this.erectLevel = 0;
  59. this.rise();
  60. this.pos.set({ x: 0, y: game.canvas.height / 2 });
  61. }
  62. Player.prototype.setBox = function() {
  63. var w = 25 + ((this.erectLevel - 1) * 10);
  64. var h = 15 + ((this.erectLevel - 1) * 6);
  65. this.box.width = w;
  66. this.box.height = h;
  67. this.box.pos.x = -(w / 2);
  68. this.box.pos.y = -(h / 2);
  69. }
  70. Player.prototype.rise = function() {
  71. this.erectLevel += 1;
  72. this.setInvincible(200);
  73. this.setBox();
  74. }
  75. Player.prototype.lower = function() {
  76. if (this.invincible)
  77. return;
  78. this.erectLevel -= 1;
  79. if (this.erectLevel === 0)
  80. this.lose();
  81. else
  82. this.setInvincible(500);
  83. this.setBox();
  84. }
  85. Player.prototype.setInvincible = function(time) {
  86. clearTimeout(this.invincibleTimeout);
  87. this.invincible = true;
  88. this.invincibleTimeout = setTimeout(function() {
  89. this.invincible = false;
  90. }.bind(this), time);
  91. }
  92. Player.prototype.onInput = function(name, down) {
  93. // Jump
  94. if (name === "jump" && down) {
  95. this.game.started = true;
  96. this.vel.y = -1.3;
  97. }
  98. }
  99. Player.prototype.update = function() {
  100. // Gravity and movement
  101. if (this.game.started) {
  102. this.force.y = 0.4;
  103. this.force.x = 0.13 + ((this.erectLevel - 1) * 0.02);
  104. }
  105. // Lose if we hit the edge
  106. if (
  107. this.pos.y < 0 ||
  108. this.pos.y > this.game.canvas.height - this.shape.height()) {
  109. this.lose();
  110. return;
  111. }
  112. // Collide
  113. for (var i in this.game.entities) {
  114. var ent = this.game.entities[i];
  115. if (ent === this)
  116. continue;
  117. if (!this.shape.collidesWith(ent.shape))
  118. continue;
  119. if (ent instanceof Obstacle) {
  120. this.lower();
  121. return;
  122. } else if (ent instanceof PowerUp) {
  123. this.rise();
  124. ent.dead = true;
  125. }
  126. }
  127. }
  128. Player.prototype.move = function() {
  129. // Move camera
  130. this.game.camera.x = this.pos.x - (this.game.canvas.width / 7);
  131. // Rotate
  132. this.rotation = this.vel.angle();
  133. }
  134. Player.prototype.lose = function() {
  135. this.game.stop(this.pos.x / 10);
  136. }
  137. Player.prototype.draw = function(ctx) {
  138. ctx.rotate(this.rotation);
  139. var scale = 0.4 + this.erectLevel / 8;
  140. ctx.scale(scale, scale);
  141. ctx.translate(70, -50);
  142. ctx.rotate(Math.PI / 2);
  143. this.img.draw(ctx);
  144. }
  145. /*
  146. * Obstacle
  147. */
  148. function Obstacle(game, x, y) {
  149. makeEnt(this, game, 100);
  150. this.img = assets.imgs.wall;
  151. this.collude = false;
  152. var w = 70;
  153. var h = game.canvas.height;
  154. var gap = 200;
  155. this.shape.push(new Box(w, h, { x: 0, y: -(h / 2) - (gap / 2) }));
  156. this.shape.push(new Box(w, h, { x: 0, y: (h / 2) + (gap / 2) }));
  157. this.pos.set({ x: x, y: y });
  158. }
  159. Obstacle.prototype.draw = function(ctx) {
  160. ctx.translate(-100, this.game.canvas.height / 2 - 340);
  161. ctx.scale(0.9, 1.1);
  162. this.img.draw(ctx);
  163. }
  164. Obstacle.prototype.update = function() {
  165. if (this.game.camera.x > this.pos.x + 800)
  166. this.dead = true;
  167. }
  168. /*
  169. * PowerUp
  170. */
  171. function PowerUp(game, x, y, type) {
  172. makeEnt(this, game, 100);
  173. this.layer = 1;
  174. this.type = type;
  175. this.shape.push(new Box(30, 30));
  176. this.pos.set({ x: x, y: y });
  177. }
  178. PowerUp.prototype.draw = function(ctx) {
  179. ctx.fillStyle = "#33ee33";
  180. ctx.strokeStyle = "#227722";
  181. ctx.beginPath();
  182. ctx.arc(15, 15, 15, 0, 2 * Math.PI);
  183. ctx.stroke();
  184. ctx.fill();
  185. }
  186. PowerUp.prototype.update = function() {
  187. if (this.game.camera.x > this.pos.x + this.shape.width())
  188. this.dead = true;
  189. }