consolidate xy into method

This commit is contained in:
Tangent Wantwight 2024-01-13 11:34:37 -05:00
parent e57a376883
commit 6384048fb5
1 changed files with 7 additions and 13 deletions

View File

@ -1,21 +1,9 @@
import { Prng, mulberry32 } from "../lib/prng";
type Lookup2d = (x: number, y: number) => number;
function dim(width: number, height: number): Lookup2d {
return function xy(x: number, y: number) {
return (
(((x % width) + width) % width) +
width * (((y % height) + height) % height)
);
};
}
export class IslandGrid {
data: number[];
rng: Prng;
xy: Lookup2d;
basePos: number;
lobePos1: number;
lobePos2: number;
@ -25,7 +13,6 @@ export class IslandGrid {
constructor(public width: number, public height: number, seed: number) {
this.data = Array(width * height).fill(0);
this.rng = mulberry32(seed);
this.xy = dim(width, height);
this.basePos = this.data.length >> 1;
this.lobePos1 = this.xy(
@ -38,6 +25,13 @@ export class IslandGrid {
);
}
public xy(x: number, y: number): number {
return (
(((x % this.width) + this.width) % this.width) +
this.width * (((y % this.height) + this.height) % this.height)
);
}
public get(x: number, y: number): number {
return this.data[this.xy(x, y)];
}