Replace map generator again

This commit is contained in:
2025-02-15 21:21:12 -08:00
parent b20f2760d4
commit b1ac26fa78
9 changed files with 658 additions and 18 deletions

View File

@ -55,6 +55,10 @@ export class Point {
this.y = y;
}
toString(): string {
return `${this.x},${this.y}`
}
offset(other: Point | Size): Point {
if (other instanceof Point) {
return new Point(this.x + other.x, this.y + other.y);
@ -81,6 +85,10 @@ export class Point {
subtract(top: Point): Size {
return new Size(this.x - top.x, this.y - top.y);
}
manhattan(other: Point) {
return Math.abs(this.x - other.x) + Math.abs(this.y - other.y);
}
}
export class Size {
@ -121,6 +129,20 @@ export class Rect {
contains(other: Point) {
return (other.x >= this.top.x && other.y >= this.top.y && other.x < this.top.x + this.size.w && other.y < this.top.y + this.size.h);
}
overlaps(other: Rect) {
let ax0 = this.top.x;
let ay0 = this.top.y;
let ax1 = ax0 + this.size.w;
let ay1 = ay0 + this.size.h;
let bx0 = other.top.x;
let by0 = other.top.y;
let bx1 = bx0 + other.size.w;
let by1 = by0 + other.size.h;
let noOverlap = ax0 > bx1 || bx0 > ax1 || ay0 > by1 || by0 > ay1;
return !noOverlap;
}
}
export class Grid<T> {
@ -201,7 +223,7 @@ export class Grid<T> {
(position.x < 0 || position.x >= this.size.w || Math.floor(position.x) != position.x) ||
(position.y < 0 || position.y >= this.size.h || Math.floor(position.y) != position.y)
) {
throw `invalid position for ${this.size}: ${position}`
throw new Error(`invalid position for ${this.size}: ${position}`)
}
}