Add some typescript files and move dungeon.js code to them
This commit is contained in:
parent
778416a43a
commit
209070ac02
4 changed files with 244 additions and 111 deletions
66
assets/scripts/modules/sketches/console.ts
Normal file
66
assets/scripts/modules/sketches/console.ts
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
import {
|
||||
IntegerPoint as Point,
|
||||
IntegerSize as Size,
|
||||
Rect,
|
||||
} from "./geometry.js";
|
||||
|
||||
export class Console {
|
||||
#screen: Cell[];
|
||||
#size: Size;
|
||||
|
||||
constructor(size: Size) {
|
||||
const length = size.width * size.height;
|
||||
this.#size = size
|
||||
|
||||
this.#screen = new Array(length);
|
||||
for (let i = 0; i < length; i++) {
|
||||
this.#screen[i] = new Cell(' ')
|
||||
}
|
||||
}
|
||||
|
||||
drawInRect(rect: Rect) {
|
||||
const cellSize = new Size(
|
||||
rect.size.width / this.#size.width,
|
||||
rect.size.height / this.#size.height
|
||||
);
|
||||
|
||||
for (let y = 0; y < this.#size.height; y++) {
|
||||
for (let x = 0; x < this.#size.width; x++) {
|
||||
const pt = new Point(x, y);
|
||||
const character = this.characterAt(pt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
characterAt(pt: Point) {
|
||||
return this.#cellAt(pt).character;
|
||||
}
|
||||
|
||||
#cellAt(pt: Point): Cell {
|
||||
return this.#screen[this.#size.width * pt.y + pt.x];
|
||||
}
|
||||
}
|
||||
|
||||
export class Color {
|
||||
red: number;
|
||||
green: number;
|
||||
blue: number;
|
||||
|
||||
constructor(r: number, g: number, b: number) {
|
||||
this.red = Math.min(1, Math.max(0, r));
|
||||
this.green = Math.min(1, Math.max(0, g));
|
||||
this.blue = Math.min(1, Math.max(0, b));
|
||||
}
|
||||
}
|
||||
|
||||
export class Cell {
|
||||
character: String;
|
||||
foregroundColor: Color;
|
||||
backgroundColor?: Color;
|
||||
|
||||
constructor(character: String, foregroundColor?: Color, backgroundColor?: Color) {
|
||||
this.character = character ? character : " ";
|
||||
this.foregroundColor = foregroundColor ? foregroundColor : new Color(1, 1, 1);
|
||||
this.backgroundColor = backgroundColor;
|
||||
}
|
||||
}
|
||||
144
assets/scripts/modules/sketches/geometry.ts
Normal file
144
assets/scripts/modules/sketches/geometry.ts
Normal file
|
|
@ -0,0 +1,144 @@
|
|||
export class Point {
|
||||
x = 0;
|
||||
y = 0;
|
||||
|
||||
constructor(x: number = 0, y: number = 0) {
|
||||
if (x !== null && x !== undefined) {
|
||||
this.x = x;
|
||||
}
|
||||
if (y !== null && y !== undefined) {
|
||||
this.y = y;
|
||||
}
|
||||
}
|
||||
|
||||
toString() {
|
||||
return `(${this.x}, ${this.y})`;
|
||||
}
|
||||
|
||||
equalsPoint(other: Point): boolean {
|
||||
return this.x === other.x && this.y === other.y;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export class IntegerPoint extends Point {
|
||||
constructor(x: number = 0, y: number = 0) {
|
||||
super(x ? Math.floor(x) : 0, y ? Math.floor(y) : 0);
|
||||
}
|
||||
|
||||
*neighbors() {
|
||||
const x = this.x;
|
||||
const y = this.y;
|
||||
|
||||
yield new IntegerPoint(x - 1, y - 1);
|
||||
yield new IntegerPoint(x, y - 1);
|
||||
yield new IntegerPoint(x + 1, y - 1);
|
||||
yield new IntegerPoint(x - 1, y);
|
||||
yield new IntegerPoint(x + 1, y);
|
||||
yield new IntegerPoint(x - 1, y + 1);
|
||||
yield new IntegerPoint(x, y + 1);
|
||||
yield new IntegerPoint(x + 1, y + 1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export class Size {
|
||||
width = 0;
|
||||
height = 0;
|
||||
|
||||
constructor(width: number = 0, height: number = 0) {
|
||||
if (width !== null && width !== undefined) {
|
||||
this.width = width;
|
||||
}
|
||||
if (height !== null && height !== undefined) {
|
||||
this.height = height;
|
||||
}
|
||||
}
|
||||
|
||||
toString() {
|
||||
return `(${this.width}, ${this.height})`
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export class IntegerSize extends Size {
|
||||
constructor(w: number = 0, h: number = 0) {
|
||||
super(w ? Math.floor(w) : 0, h ? Math.floor(h) : 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export class Rect {
|
||||
origin = new Point();
|
||||
size = new Size();
|
||||
|
||||
static fromCoordinates(x: number, y: number, w: number, h: number) {
|
||||
return new Rect(new Point(x, y), new Size(w, h));
|
||||
}
|
||||
|
||||
constructor(origin: Point, size: Size) {
|
||||
if (origin instanceof Point) {
|
||||
this.origin = origin;
|
||||
} else {
|
||||
throw new Error("Invalid origin value");
|
||||
}
|
||||
|
||||
if (size instanceof Size) {
|
||||
this.size = size;
|
||||
} else {
|
||||
throw new Error("Invalid size value");
|
||||
}
|
||||
}
|
||||
|
||||
toString() {
|
||||
return `{${this.origin.toString()}, ${this.size.toString()}}`
|
||||
}
|
||||
|
||||
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 width() { return this.size.width; }
|
||||
get height() { return this.size.height; }
|
||||
get area() { return this.size.width * this.size.height; }
|
||||
|
||||
*xCoordinates() {
|
||||
for (let x = this.minX; x <= this.maxX; x++) {
|
||||
yield x;
|
||||
}
|
||||
}
|
||||
|
||||
*yCoordinates() {
|
||||
for (let y = this.minY; y <= this.maxY; y++) {
|
||||
yield y;
|
||||
}
|
||||
}
|
||||
|
||||
insetRect(inset: number) {
|
||||
const twiceInset = 2 * inset;
|
||||
|
||||
return Rect.fromCoordinates(
|
||||
this.origin.x + inset,
|
||||
this.origin.y + inset,
|
||||
this.size.width - twiceInset,
|
||||
this.size.height - twiceInset
|
||||
);
|
||||
}
|
||||
|
||||
intersects(other: Rect) {
|
||||
if (other.minX > this.maxX)
|
||||
return false;
|
||||
|
||||
if (other.maxX < this.minX)
|
||||
return false;
|
||||
|
||||
if (other.minY > this.maxY)
|
||||
return false;
|
||||
|
||||
if (other.maxY < this.minY)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue