player.js 4.2 KB

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