coordinates.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. class coordinates {
  2. #x;
  3. #y;
  4. #z;
  5. #rotate;
  6. #head_rotate;
  7. constructor() {
  8. this.#x = 0;
  9. this.#y = 0;
  10. this.#z = 0;
  11. this.#rotate = 0;
  12. this.#head_rotate = 0;
  13. }
  14. get x() {
  15. return this.#x;
  16. }
  17. get y() {
  18. return this.#y;
  19. }
  20. get z() {
  21. return this.#z;
  22. }
  23. get rotate() {
  24. this.#check_rotate();
  25. return this.#rotate;
  26. }
  27. set x(target) {
  28. if (typeof(target) !== "number") {
  29. throw new TypeError("Coordinate must be numer.");
  30. }
  31. this.#x = target;
  32. }
  33. set y(target) {
  34. if (typeof(target) !== "number") {
  35. throw new TypeError("Coordinate must be number.");
  36. }
  37. this.#y = target;
  38. }
  39. set z(target) {
  40. if (typeof(target) !== "number") {
  41. throw new TypeError("Coordinate must be number.");
  42. }
  43. this.#z = target;
  44. }
  45. set rotate(target) {
  46. if (typeof(target) !== "number") {
  47. throw new TypeError("Coordinate must be number.");
  48. }
  49. this.#rotate = target;
  50. this.#check_rotate();
  51. }
  52. #check_rotate() {
  53. while (this.#rotate < 0) {
  54. this.#rotate += 360;
  55. }
  56. this.#rotate = this.#rotate % 360;
  57. }
  58. set head_rotate(target) {
  59. if (typeof(target) !== "number") {
  60. throw new TypeError("Coordinate must be number.");
  61. }
  62. this.#head_rotate = target % 90;
  63. this.#check_head_rotate();
  64. }
  65. #check_head_rotate() {
  66. if (this.#head_rotate > 90) this.#head_rotate = 90;
  67. if (this.#head_rotate < -90) this.#head_rotate = -90;
  68. }
  69. get head_rotate() {
  70. this.#check_head_rotate();
  71. return this.#head_rotate;
  72. }
  73. #radians(change = 0) {
  74. return (this.rotate + change) * Math.PI / 180;
  75. }
  76. move_front(target = 1) {
  77. if (typeof(target) !== "number") {
  78. throw new TypeError("Steps must be number.");
  79. }
  80. this.#y -= Math.cos(this.#radians()) * target;
  81. this.#x -= Math.sin(this.#radians()) * target;
  82. }
  83. move_back(target = 1) {
  84. if (typeof(target) !== "number") {
  85. throw new TypeError("Steps must be number.");
  86. }
  87. this.move_front(-target);
  88. }
  89. move_left(target = 1) {
  90. if (typeof(target) !== "number") {
  91. throw new TypeError("Steps must be number.");
  92. }
  93. this.#y += Math.cos(this.#radians(-90)) * target;
  94. this.#x += Math.sin(this.#radians(-90)) * target;
  95. }
  96. move_right(target = 1) {
  97. if (typeof(target) !== "number") {
  98. throw new TypeError("Steps must be number.");
  99. }
  100. this.move_left(-target);
  101. }
  102. rotate_clockwise(target = 1) {
  103. if (typeof(target) !== "number") {
  104. throw new TypeError("Degrees must be number.");
  105. }
  106. this.#rotate -= target;
  107. }
  108. rotate_counterclockwise(target = 1) {
  109. if (typeof(target) !== "number") {
  110. throw new TypeError("Degrees must be number.");
  111. }
  112. this.#rotate += target;
  113. }
  114. rotate_top(target = 1) {
  115. if (typeof(target) !== "number") {
  116. throw new TypeError("Degrees must be number.");
  117. }
  118. this.#head_rotate += target;
  119. }
  120. rotate_bottom(target = 1) {
  121. if (typeof(target) !== "number") {
  122. throw new TypeError("Degrees must be number.");
  123. }
  124. this.#head_rotate -= target;
  125. }
  126. get as_string() {
  127. let dump = "";
  128. dump += "X: " + this.x + "\n";
  129. dump += "Y: " + this.y + "\n";
  130. dump += "Z: " + this.z + "\n";
  131. dump += "ROTATE: " + this.rotate;
  132. dump += "HEAD ROTATE: " + this.head_rotate;
  133. return dump;
  134. }
  135. }
  136. export { coordinates };