coordinates.js 3.1 KB

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