actor.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { coordinates } from "./coordinates.js";
  2. import { position } from "./position.js";
  3. import { moving } from "./moving.js";
  4. import { rotating } from "./rotating.js";
  5. class actor extends coordinates {
  6. #movement;
  7. #rotation;
  8. #speed;
  9. constructor() {
  10. super();
  11. this.#speed = 0.25;
  12. this.#movement = new moving();
  13. this.#rotation = new rotating();
  14. }
  15. set speed(target) {
  16. if (typeof(target) !== "number") {
  17. throw new TypeError("Speed must be an number.");
  18. }
  19. this.#speed = target;
  20. }
  21. get speed() {
  22. return this.#speed;
  23. }
  24. rotate_cursor(cursor, size) {
  25. if (!(cursor instanceof position)) {
  26. throw new TypeError("Cursor compare must be an position object.");
  27. }
  28. if (!(size instanceof position)) {
  29. throw new TypeError("Size must be an position object.");
  30. }
  31. const rotate = cursor.x / size.x * 360;
  32. this.rotate_clockwise(rotate);
  33. }
  34. get movement() {
  35. return this.#movement;
  36. }
  37. get rotation() {
  38. return this.#rotation;
  39. }
  40. update() {
  41. this.#update_position();
  42. this.#update_rotation();
  43. }
  44. #update_rotation() {
  45. if (this.rotation.is_right) {
  46. this.rotate_clockwise(this.#speed * 10);
  47. }
  48. if (this.rotation.is_left) {
  49. this.rotate_counterclockwise(this.#speed * 10);
  50. }
  51. }
  52. #update_position() {
  53. if (this.movement.is_front) this.move_front(this.#speed);
  54. if (this.movement.is_back) this.move_back(this.#speed);
  55. if (this.movement.is_left) this.move_left(this.#speed);
  56. if (this.movement.is_right) this.move_right(this.#speed);
  57. }
  58. }
  59. export { actor };