actor.js 1.7 KB

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