@mousepox/math
Version:
Math-related objects and utilities
38 lines (37 loc) • 1.24 kB
JavaScript
import { Grid } from "./Grid";
export class AutoGrid extends Grid {
source;
rules;
constructor(source, rules) {
super(source.width, source.height);
this.source = source;
this.rules = rules;
this.update();
}
update() {
this.forEach((_, x, y) => this.updateSingleCell(x, y));
}
updateSingleCell(x, y) {
const tile = this.source.get(x, y);
if (this.rules[tile] !== undefined) {
const flags = this.source.getAdjacentFlags(x, y);
if (this.rules[tile] !== undefined) {
for (const rule of this.rules[tile]) {
if (rule.flags.indexOf(flags) !== -1) {
let value = 0;
if (Array.isArray(rule.value)) {
const index = Math.floor(Math.random() * rule.value.length);
value = rule.value[index];
}
else {
value = rule.value;
}
this.set(x, y, value);
return;
}
}
}
}
this.set(x, y, tile);
}
}