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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Player
  3. */
  4. function Player(game) {
  5. makeEnt(this, game, 100);
  6. this.moves = true;
  7. this.invincible = false;
  8. this.invincibleTimeout = null;
  9. this.started = false;
  10. this.rotation = 0;
  11. this.box = new Box();
  12. this.shape.push(this.box);
  13. this.erectLevel = 0;
  14. this.rise();
  15. this.pos.set({ x: 0, y: game.canvas.height / 2 });
  16. }
  17. Player.prototype.setBox = function() {
  18. var w = 30 + ((this.erectLevel - 1) * 15);
  19. var h = 15 + ((this.erectLevel - 1) * 6);
  20. this.box.width = w;
  21. this.box.height = h;
  22. this.box.pos.x = -(w / 2);
  23. this.box.pos.y = -(h / 2);
  24. }
  25. Player.prototype.rise = function() {
  26. this.erectLevel += 1;
  27. this.setInvincible(200);
  28. this.setBox();
  29. }
  30. Player.prototype.lower = function() {
  31. if (this.invincible)
  32. return;
  33. this.erectLevel -= 1;
  34. if (this.erectLevel === 0)
  35. this.lose();
  36. else
  37. this.setInvincible(500);
  38. this.setBox();
  39. }
  40. Player.prototype.setInvincible = function(time) {
  41. clearTimeout(this.invincibleTimeout);
  42. this.invincible = true;
  43. this.invincibleTimeout = setTimeout(function() {
  44. this.invincible = false;
  45. }.bind(this), time);
  46. }
  47. Player.prototype.update = function() {
  48. // Jump
  49. if (this.game.presses.jump) {
  50. this.started = true;
  51. this.vel.y = -1.3;
  52. }
  53. // Gravity and movement
  54. if (this.started) {
  55. this.force.y = 0.4;
  56. this.force.x = 0.13 + ((this.erectLevel - 1) * 0.02);
  57. }
  58. // Lose if we hit the edge
  59. if (
  60. this.pos.y < 0 ||
  61. this.pos.y > this.game.canvas.height - this.shape.height()) {
  62. this.lose();
  63. return;
  64. }
  65. // Collide
  66. for (var i in this.game.entities) {
  67. var ent = this.game.entities[i];
  68. if (ent === this)
  69. continue;
  70. if (!this.shape.collidesWith(ent.shape))
  71. continue;
  72. if (ent instanceof Obstacle) {
  73. this.lower();
  74. return;
  75. } else if (ent instanceof PowerUp) {
  76. this.rise();
  77. ent.dead = true;
  78. }
  79. }
  80. }
  81. Player.prototype.move = function() {
  82. // Move camera
  83. this.game.camera.x = this.pos.x - (this.game.canvas.width / 7);
  84. // Rotate
  85. this.rotation = this.vel.angle();
  86. }
  87. Player.prototype.lose = function() {
  88. alert("You died!");
  89. this.game.stop();
  90. }
  91. Player.prototype.draw = function(ctx) {
  92. if (this.invincible)
  93. ctx.fillStyle = "#d5d5bf";
  94. else
  95. ctx.fillStyle = "#f5f5dc";
  96. ctx.rotate(this.rotation);
  97. this.shape.draw(ctx);
  98. }
  99. /*
  100. * Obstacle
  101. */
  102. function Obstacle(game, x, y) {
  103. makeEnt(this, game, 100);
  104. this.pos.set({ x: x, y: y });
  105. var w = 70;
  106. var h = game.canvas.height;
  107. var gap = 200;
  108. this.shape.push(new Box(w, h, { x: 0, y: -(h / 2) - (gap / 2) }));
  109. this.shape.push(new Box(w, h, { x: 0, y: (h / 2) + (gap / 2) }));
  110. }
  111. Obstacle.prototype.draw = function(ctx) {
  112. this.shape.draw(ctx);
  113. }
  114. Obstacle.prototype.update = function() {
  115. if (this.game.camera.x > this.pos.x + this.shape.width())
  116. this.dead = true;
  117. }
  118. /*
  119. * PowerUp
  120. */
  121. function PowerUp(game, x, y, type) {
  122. makeEnt(this, game, 100);
  123. this.pos.set({ x: x, y: y });
  124. this.type = type;
  125. this.shape.push(new Box(30, 30));
  126. }
  127. PowerUp.prototype.draw = function(ctx) {
  128. ctx.fillStyle = "#33ee33";
  129. ctx.strokeStyle = "#227722";
  130. ctx.beginPath();
  131. ctx.arc(15, 15, 15, 0, 2 * Math.PI);
  132. ctx.stroke();
  133. ctx.fill();
  134. }
  135. PowerUp.prototype.update = function() {
  136. if (this.game.camera.x > this.pos.x + this.shape.width())
  137. this.dead = true;
  138. }