SheepController.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. class SheepController {
  2. constructor() {
  3. this.sheep = null;
  4. this.proximity = 100;
  5. this.wanderRange = 50;
  6. this.wanderTimer = 5000;
  7. }
  8. init(systemObject, sheep) {
  9. this.system = systemObject;
  10. this.sheep = sheep;
  11. this.proximity = Math.floor(80 * Math.random() + 30);
  12. this.wanderRange = Math.floor(100 * Math.random() + 50);
  13. this.wanderTimer = Math.floor(15 * Math.random() + 5) * 1000;
  14. }
  15. reset() {
  16. }
  17. makeDecisions(player) {
  18. if(this.sheep.isRunningAway) {
  19. return;
  20. }
  21. let distance = Math.abs(this.system.distanceTo(this.sheep.position, player.position));
  22. if(distance < this.proximity) {
  23. clearTimeout(this.isIdle);
  24. this.isIdle = null;
  25. let deltaDistance = Math.floor(4 * Math.random() * (this.proximity - distance));
  26. let angleTo = this.system.angleTo(player.position, this.sheep.position);
  27. this.sheep.targetPosition.x = Math.floor(deltaDistance * Math.cos(angleTo)) + this.sheep.position.x;
  28. this.sheep.targetPosition.y = Math.floor(deltaDistance * Math.sin(angleTo)) + this.sheep.position.y;
  29. this.sheep.targetAngle = angleTo;
  30. this.sheep.isRunningAway = true;
  31. } else {
  32. if(distance < this.proximity * 2) {
  33. this.sheep.targetAngle = this.system.angleTo(this.sheep.position, player.position);
  34. }
  35. if(this.isIdle) {
  36. return;
  37. }
  38. this.isIdle = setTimeout(() => {this.pickNewDestination()}, this.wanderTimer);
  39. }
  40. }
  41. pickNewDestination() {
  42. this.isIdle = null;
  43. this.sheep.targetPosition.x = Math.floor(this.wanderRange * 2 * Math.random() - this.wanderRange) + this.sheep.position.x;
  44. this.sheep.targetPosition.y = Math.floor(this.wanderRange * 2 * Math.random() - this.wanderRange) + this.sheep.position.y;
  45. }
  46. contextMenu() {
  47. }
  48. debugDraw(context) {
  49. if(!localStorage['game.debug']) {
  50. return;
  51. }
  52. context.fillStyle = "red";
  53. context.strokeStyle = "red";
  54. context.lineWidth = 1;
  55. context.beginPath();
  56. context.moveTo(this.sheep.position.x, this.sheep.position.y);
  57. context.lineTo(this.sheep.targetPosition.x, this.sheep.targetPosition.y);
  58. context.stroke();
  59. context.beginPath();
  60. context.arc(this.sheep.position.x, this.sheep.position.y, this.proximity, 0, 2 * Math.PI);
  61. context.stroke();
  62. context.strokeStyle = "yellow";
  63. context.beginPath();
  64. context.arc(this.sheep.position.x, this.sheep.position.y, this.proximity * 2, 0, 2 * Math.PI);
  65. context.stroke();
  66. }
  67. }