actor.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { coordinates } from "./coordinates.js";
  2. import { position } from "./position.js";
  3. import { moving_directions } from "./directions.js";
  4. import { rotating_directions } from "./directions.js";
  5. import { is_moving_direction } from "./directions.js";
  6. import { is_rotating_direction } from "./directions.js";
  7. class actor extends coordinates {
  8. #moving;
  9. #rotating;
  10. #speed;
  11. constructor() {
  12. super();
  13. this.#speed = 0.25;
  14. this.#moving = moving_directions.stop;
  15. this.#rotating = moving_directions.stop;
  16. }
  17. set speed(target) {
  18. if (typeof(target) !== "number") {
  19. throw new TypeError("Speed must be an number.");
  20. }
  21. this.#speed = target;
  22. }
  23. get speed() {
  24. return this.#speed;
  25. }
  26. rotate_cursor(cursor, size) {
  27. if (!(cursor instanceof position)) {
  28. throw new TypeError("Cursor compare must be an position object.");
  29. }
  30. if (!(size instanceof position)) {
  31. throw new TypeError("Size must be an position object.");
  32. }
  33. const rotate = cursor.x / size.x * 360;
  34. this.rotate_clockwise(rotate);
  35. }
  36. set moving(target) {
  37. if (!is_moving_direction(target)) {
  38. throw new TypeError("Direction must be in directions.");
  39. }
  40. this.#moving = target;
  41. }
  42. get moving() {
  43. return this.#moving;
  44. }
  45. set rotating(target) {
  46. if (!is_rotating_direction(target)) {
  47. throw new TypeError("Direction must be in directions.");
  48. }
  49. this.#rotating = target;
  50. }
  51. get rotating() {
  52. return this.#rotating;
  53. }
  54. update() {
  55. this.#update_position();
  56. this.#update_rotation();
  57. }
  58. #update_rotation() {
  59. switch (this.#rotating) {
  60. case rotating_directions.clockwise:
  61. this.rotate_clockwise(this.#speed * 10);
  62. return;
  63. case rotating_directions.counterclockwise:
  64. this.rotate_counterclockwise(this.#speed * 10);
  65. return;
  66. default:
  67. return;
  68. }
  69. }
  70. #update_position() {
  71. switch (this.#moving) {
  72. case moving_directions.front:
  73. this.move_front(this.#speed);
  74. return;
  75. case moving_directions.back:
  76. this.move_back(this.#speed);
  77. return;
  78. case moving_directions.left:
  79. this.move_left(this.#speed);
  80. return;
  81. case moving_directions.right:
  82. this.move_right(this.#speed);
  83. return;
  84. default:
  85. return;
  86. }
  87. }
  88. }
  89. export { actor };