Player.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. class Player {
  2. constructor(game, controller) {
  3. this.id = 0;
  4. this.position = { x: 0, y: 0 };
  5. this.targetPosition = { x: 0, y: 0 };
  6. this.velocity = { x: 0.0, y: 0.0 };
  7. this.controller = controller;
  8. this.acceleration = { x: 0, y: 0, speed: 0.08 };
  9. this.width = 20;
  10. this.height = 20;
  11. this.maxSpeed = 2;
  12. this.turnRate = Math.PI / 64;
  13. this.targetAngle = 0;
  14. this.angleRadians = 0;
  15. this.color = "crimson";
  16. this.playerName = "Player";
  17. this.spriteCanvas = null;
  18. this.spriteContext = null;
  19. this.isAccelerating = false;
  20. this.isDead = false;
  21. this.health = 5;
  22. this.maxHealth = 5;
  23. this.seen = [];
  24. }
  25. init(systemObject) {
  26. this.system = systemObject;
  27. this.controller.init(this);
  28. this.color = "crimson";
  29. this.controller.init(systemObject, this);
  30. this.respawn();
  31. }
  32. update(delta) {
  33. if (this.isDead) {
  34. return;
  35. }
  36. if (this.health <= 0) {
  37. this.die();
  38. return;
  39. }
  40. this.angleRadians = this.system.angleTo(this.position, this.targetPosition);
  41. if (this.targetPosition.x != this.position.x) {
  42. if (Math.abs(this.position.x - this.targetPosition.x) < this.maxSpeed) {
  43. this.position.x = this.targetPosition.x;
  44. this.targetAngle = this.angleRadians;
  45. } else {
  46. this.targetAngle = this.angleRadians;
  47. this.position.x += this.maxSpeed * Math.cos(this.angleRadians) * delta;
  48. }
  49. }
  50. if (this.targetPosition.y != this.position.y) {
  51. if (Math.abs(this.position.y - this.targetPosition.y) < this.maxSpeed) {
  52. this.position.y = this.targetPosition.y;
  53. this.targetAngle = this.angleRadians;
  54. } else {
  55. this.targetAngle = this.angleRadians;
  56. this.position.y += (this.maxSpeed) * Math.sin(this.angleRadians) * delta;
  57. }
  58. }
  59. }
  60. control(input) {
  61. this.controller.makeDecisions(this);
  62. }
  63. respawn() {
  64. this.position.x = 0;
  65. this.position.y = 0;
  66. }
  67. die() {
  68. this.health = 0;
  69. if (!this.isDead) {
  70. var player = this;
  71. setTimeout(function () { player.respawn(); }, 3000);
  72. this.isDead = true;
  73. }
  74. }
  75. walkUp() {
  76. this.targetPosition.y = this.position.y - this.maxSpeed;
  77. }
  78. walkDown() {
  79. this.targetPosition.y = this.position.y + this.maxSpeed;
  80. }
  81. walkLeft() {
  82. this.targetPosition.x = this.position.x - this.maxSpeed;
  83. }
  84. walkRight() {
  85. this.targetPosition.x = this.position.x + this.maxSpeed;
  86. }
  87. walkTowards(newPosition) {
  88. this.targetPosition.x = parseInt(newPosition.x);
  89. this.targetPosition.y = parseInt(newPosition.y);
  90. }
  91. jump() {
  92. console.log("jump");
  93. }
  94. preRender(context, angle) {
  95. if (angle == null) {
  96. angle = 0;
  97. }
  98. context.fillStyle = this.color;
  99. context.strokeStyle = this.color;
  100. context.lineWidth = 4;
  101. context.save();
  102. context.translate(this.width / 2, this.height / 2);
  103. context.rotate(angle);
  104. context.beginPath();
  105. context.moveTo(this.width / 2, 0);
  106. context.lineTo(-(this.width / 2), -(this.height / 3));
  107. context.lineTo(-(this.width / 2), this.height / 3);
  108. context.lineTo(this.width / 2, 0);
  109. context.fill();
  110. context.restore();
  111. }
  112. draw(context) {
  113. if (this.spriteContext == null) {
  114. this.spriteCanvas = document.createElement("canvas");
  115. this.spriteCanvas.width = this.width;
  116. this.spriteCanvas.height = this.height;
  117. this.spriteContext = this.spriteCanvas.getContext("2d");
  118. this.preRender(this.spriteContext);
  119. }
  120. if (this.isDead) {
  121. return;
  122. }
  123. context.save();
  124. context.translate(this.position.x, this.position.y);
  125. context.rotate(this.targetAngle);
  126. if (this.isAccelerating) {
  127. context.fillStyle = "#7777FF";
  128. context.beginPath();
  129. context.moveTo(-this.width - 5, 0);
  130. context.lineTo(-this.width / 2, 3);
  131. context.lineTo(-this.width / 2, -3);
  132. context.lineTo(-this.width - 5, 0);
  133. context.fill();
  134. }
  135. context.drawImage(this.spriteCanvas, 0, 0, this.spriteCanvas.width, this.spriteCanvas.height,
  136. -this.spriteCanvas.width / 2, -this.spriteCanvas.height / 2, this.width, this.height);
  137. context.strokeStyle = "#00FF00";
  138. if (this.isTakingDamage) {
  139. context.strokeStyle = "#FF0000";
  140. this.isTakingDamage = false;
  141. }
  142. context.restore();
  143. this.controller.debugDraw(context);
  144. }
  145. trigger(eventId) {
  146. if (this.seen.indexOf(eventId) == -1) {
  147. this.seen.push(eventId);
  148. return true;
  149. }
  150. return false;
  151. }
  152. }