From 2033e0c3a11fac1bf8af482bfda49bfff1161669 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Fri, 3 Feb 2023 18:01:11 -0800 Subject: [PATCH] Add some basic geometry types: Point, Size, Rect --- assets/scripts/nethack/dungeon.js | 67 +++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/assets/scripts/nethack/dungeon.js b/assets/scripts/nethack/dungeon.js index efaaca4..c8eaa37 100644 --- a/assets/scripts/nethack/dungeon.js +++ b/assets/scripts/nethack/dungeon.js @@ -8,6 +8,73 @@ class Cell { } } +class Point { + x = 0; + y = 0; + + constructor(x, y) { + this.x = x; + this.y = y; + } +} + +class Size { + width = 0; + height = 0; + + constructor(width, height) { + this.width = width; + this.height = height; + } +} + +class Rect { + origin = new Point(); + size = new Size(); + + static fromCoordinates(x, y, w, h) { + return new Rect(new Point(x, y), new Size(w, h)); + } + + constructor(origin, size) { + this.origin = origin; + this.size = size; + } + + get minX() { return this.origin.x; } + get minY() { return this.origin.y; } + get maxX() { return this.origin.x + this.size.width; } + get maxY() { return this.origin.y + this.size.height; } + get area() { return this.size.width * this.size.height; } + + insetRect(inset) { + const twiceInset = 2 * inset; + + return Rect.fromCoordinates( + this.origin.x + inset, + this.origin.y + inset, + this.size.width - twiceInset, + this.size.height - twiceInset + ); + } + + intersects(otherRect) { + if (otherRect.minX > this.maxX) + return false; + + if (otherRect.maxX < this.minX) + return false; + + if (otherRect.minY > this.maxY) + return false; + + if (otherRect.maxY < this.minY) + return false; + + return true; + } +} + class Grid { #width; #height;