position.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. class position {
  2. #x;
  3. #y;
  4. #z;
  5. constructor(x = 0, y = 0, z = 0) {
  6. this.x = x;
  7. this.y = y;
  8. this.z = z;
  9. }
  10. set x(target) {
  11. if (typeof(target) !== "number") {
  12. throw new TypeError("X cord must be an number.");
  13. }
  14. this.#x = target;
  15. }
  16. set y(target) {
  17. if (typeof(target) !== "number") {
  18. throw new TypeError("Y cord must be an number.");
  19. }
  20. this.#y = target;
  21. }
  22. set z(target) {
  23. if (typeof(target) !== "number") {
  24. throw new TypeError("Z cord must be an number.");
  25. }
  26. this.#z = target;
  27. }
  28. get x() {
  29. return this.#x;
  30. }
  31. get y() {
  32. return this.#y;
  33. }
  34. get z() {
  35. return this.#z;
  36. }
  37. compare(target) {
  38. if (!(target instanceof position)) {
  39. throw new TypeError("Can only compare position with position.");
  40. }
  41. const result = new position();
  42. result.x = target.x - this.x;
  43. result.y = target.y - this.y;
  44. result.z = target.z - this.z;
  45. return result;
  46. }
  47. }
  48. export { position };