UNPKG

@mousepox/math

Version:

Math-related objects and utilities

40 lines (39 loc) 1.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AutoGrid = void 0; const Grid_1 = require("./Grid"); class AutoGrid extends Grid_1.Grid { 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); } } exports.AutoGrid = AutoGrid;