position.js 868 B

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