shared/Rectangle.js

  1. 'use strict';
  2. /**
  3. * A rectangular hitbox. Remember that this rectangle describes the item as if
  4. * its settings were x=0, y=0, scale=1, rotation=0.
  5. *
  6. * @memberof module:shared
  7. * @implements {module:shared.Hitbox}
  8. *
  9. * @param {number} width - The width of the rectangle.
  10. * @param {number} height - The height of the rectangle.
  11. * @param {number} [x=0] - The x offset of the rectangle.
  12. * @param {number} [y=0] - The y offset of the rectangle.
  13. */
  14. class Rectangle {
  15. constructor(width, height, x = 0, y = 0) {
  16. /**
  17. * The width of the rectangle.
  18. *
  19. * @type {number}
  20. */
  21. this.width = width;
  22. /**
  23. * The height of the rectangle.
  24. *
  25. * @type {number}
  26. */
  27. this.height = height;
  28. /**
  29. * The x coordinate of the rectangle.
  30. *
  31. * @type {number}
  32. */
  33. this.x = x;
  34. /**
  35. * The y coordinate of the rectangle.
  36. *
  37. * @type {number}
  38. */
  39. this.y = y;
  40. }
  41. /**
  42. * Determines if a point is inside the rectangle.
  43. *
  44. * @param {module:shared.Point2D} point - The point to test.
  45. *
  46. * @return {boolean} true if the point is inside the rectangle, false
  47. * otherwise.
  48. */
  49. contains(point) {
  50. return point.x >= this.x && point.x <= this.x + this.width && point.y >= this.y && point.y <= this.y + this.height;
  51. }
  52. }
  53. module.exports = Rectangle;