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

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