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 5.4KB

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