coordinates.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. class coordinates {
  2. #x;
  3. #y;
  4. #z;
  5. #rotate;
  6. constructor() {
  7. this.#x = 0;
  8. this.#y = 0;
  9. this.#z = 0;
  10. this.#rotate = 0;
  11. }
  12. get x() {
  13. return this.#x;
  14. }
  15. get y() {
  16. return this.#y;
  17. }
  18. get z() {
  19. return this.#z;
  20. }
  21. get rotate() {
  22. return this.#rotate;
  23. }
  24. set x(target) {
  25. if (typeof(target) !== "number") {
  26. throw new TypeError("Coordinate must be numer.");
  27. }
  28. this.#x = target;
  29. }
  30. set y(target) {
  31. if (typeof(target) !== "number") {
  32. throw new TypeError("Coordinate must be numer.");
  33. }
  34. this.#y = target;
  35. }
  36. set z(target) {
  37. if (typeof(target) !== "number") {
  38. throw new TypeError("Coordinate must be numer.");
  39. }
  40. this.#z = target;
  41. }
  42. set rotate(target) {
  43. if (typeof(target) !== "number") {
  44. throw new TypeError("Coordinate must be numer.");
  45. }
  46. this.#rotate = target % 360;
  47. }
  48. #radians(change = 0) {
  49. return (this.rotate + change) * Math.PI / 180;
  50. }
  51. move_front(target = 1) {
  52. if (typeof(target) !== "number") {
  53. throw new TypeError("Steps must be number.");
  54. }
  55. this.#y += Math.sin(this.#radians()) * target;
  56. this.#x += Math.cos(this.#radians()) * target;
  57. }
  58. move_back(target = 1) {
  59. if (typeof(target) !== "number") {
  60. throw new TypeError("Steps must be number.");
  61. }
  62. this.move_front(-target);
  63. }
  64. move_left(target = 1) {
  65. if (typeof(target) !== "number") {
  66. throw new TypeError("Steps must be number.");
  67. }
  68. this.#y += Math.sin(this.#radians(-90)) * target;
  69. this.#x += Math.cos(this.#radians(-90)) * target;
  70. }
  71. move_right(target = 1) {
  72. if (typeof(target) !== "number") {
  73. throw new TypeError("Steps must be number.");
  74. }
  75. this.move_left(-target);
  76. }
  77. rotate_clockwise(target = 0) {
  78. if (typeof(target) !== "number") {
  79. throw new TypeError("Degrees must be number.");
  80. }
  81. this.#rotate -= target;
  82. }
  83. rotate_counterclockwise(target = 0) {
  84. if (typeof(target) !== "number") {
  85. throw new TypeError("Degrees must be number.");
  86. }
  87. this.#rotate += target;
  88. }
  89. }
  90. export { coordinates };