Sheep.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. class Sheep {
  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 = 30;
  10. this.height = 30;
  11. this.maxSpeed = 2;
  12. this.turnRate = Math.PI / 64;
  13. this.targetAngle = 0;
  14. this.angleRadians = 0;
  15. this.color = "white";
  16. this.name = "Sheep";
  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 = "white";
  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. if(this.isRunningAway) {
  60. if(this.targetPosition.x == this.position.x &&
  61. this.targetPosition.y == this.position.y) {
  62. setTimeout(() => {
  63. this.isRunningAway = false;
  64. }, 300);
  65. }
  66. }
  67. }
  68. control(player) {
  69. this.controller.makeDecisions(player);
  70. }
  71. respawn() {
  72. this.position.x = Math.floor(200 * Math.random() - 100);
  73. this.position.y = Math.floor(200 * Math.random() - 100);
  74. this.targetPosition.x = this.position.x;
  75. this.targetPosition.y = this.position.y;
  76. this.targetAngle = Math.floor(360 * Math.random());
  77. }
  78. die() {
  79. this.health = 0;
  80. if (!this.isDead) {
  81. var sheep = this;
  82. setTimeout(function () { sheep.respawn(); }, 3000);
  83. this.isDead = true;
  84. }
  85. }
  86. walkUp() {
  87. this.targetPosition.y = this.position.y - this.maxSpeed;
  88. }
  89. walkDown() {
  90. this.targetPosition.y = this.position.y + this.maxSpeed;
  91. }
  92. walkLeft() {
  93. this.targetPosition.x = this.position.x - this.maxSpeed;
  94. }
  95. walkRight() {
  96. this.targetPosition.x = this.position.x + this.maxSpeed;
  97. }
  98. walkTowards(newPosition) {
  99. this.targetPosition.x = parseInt(newPosition.x);
  100. this.targetPosition.y = parseInt(newPosition.y);
  101. }
  102. preRender(context, angle) {
  103. if (angle == null) {
  104. angle = 0;
  105. }
  106. context.fillStyle = this.color;
  107. context.strokeStyle = this.color;
  108. context.lineWidth = 4;
  109. context.save();
  110. context.translate(this.width / 2, this.height / 2);
  111. context.rotate(angle);
  112. // context.beginPath();
  113. // context.moveTo(this.width / 2, 0);
  114. // context.lineTo(-(this.width / 2), -(this.height / 3));
  115. // context.lineTo(-(this.width / 2), this.height / 3);
  116. // context.lineTo(this.width / 2, 0);
  117. // context.fill();
  118. context.beginPath();
  119. context.arc(0, 0, this.width / 4, 0, 2 * Math.PI);
  120. context.fill();
  121. context.beginPath();
  122. context.arc(this.width / 6, 0, this.width / 6, 0, 2 * Math.PI);
  123. context.fill();
  124. context.restore();
  125. }
  126. draw(context) {
  127. if (this.spriteContext == null) {
  128. this.spriteCanvas = document.createElement("canvas");
  129. this.spriteCanvas.width = this.width;
  130. this.spriteCanvas.height = this.height;
  131. this.spriteContext = this.spriteCanvas.getContext("2d");
  132. this.preRender(this.spriteContext);
  133. }
  134. if (this.isDead) {
  135. return;
  136. }
  137. context.save();
  138. context.translate(this.position.x, this.position.y);
  139. context.rotate(this.targetAngle);
  140. if (this.isAccelerating) {
  141. context.fillStyle = "#7777FF";
  142. context.beginPath();
  143. context.moveTo(-this.width - 5, 0);
  144. context.lineTo(-this.width / 2, 3);
  145. context.lineTo(-this.width / 2, -3);
  146. context.lineTo(-this.width - 5, 0);
  147. context.fill();
  148. }
  149. context.drawImage(this.spriteCanvas, 0, 0, this.spriteCanvas.width, this.spriteCanvas.height,
  150. -this.spriteCanvas.width / 2, -this.spriteCanvas.height / 2, this.width, this.height);
  151. context.strokeStyle = "#00FF00";
  152. if (this.isTakingDamage) {
  153. context.strokeStyle = "#FF0000";
  154. this.isTakingDamage = false;
  155. }
  156. context.restore();
  157. this.controller.debugDraw(context);
  158. }
  159. trigger(eventId) {
  160. if (this.seen.indexOf(eventId) == -1) {
  161. this.seen.push(eventId);
  162. return true;
  163. }
  164. return false;
  165. }
  166. }