Game
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

entities.js 3.2KB

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