actor.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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, scale = 2) {
  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. if (typeof(scale) !== "number") {
  32. throw new TypeError("Scale must be an number.");
  33. }
  34. const rotate = cursor.x / size.x * 360 * scale;
  35. const bottom_rotate = cursor.y / size.y * 180 * scale;
  36. this.rotate_bottom(bottom_rotate);
  37. this.rotate_clockwise(rotate);
  38. }
  39. get movement() {
  40. return this.#movement;
  41. }
  42. get rotation() {
  43. return this.#rotation;
  44. }
  45. update() {
  46. this.#update_position();
  47. this.#update_rotation();
  48. }
  49. #update_rotation() {
  50. if (this.rotation.is_right) {
  51. this.rotate_clockwise(this.#speed * 10);
  52. }
  53. if (this.rotation.is_left) {
  54. this.rotate_counterclockwise(this.#speed * 10);
  55. }
  56. }
  57. #update_position() {
  58. if (this.movement.is_front) this.move_front(this.#speed);
  59. if (this.movement.is_back) this.move_back(this.#speed);
  60. if (this.movement.is_left) this.move_left(this.#speed);
  61. if (this.movement.is_right) this.move_right(this.#speed);
  62. }
  63. }
  64. export { actor };