Re-Order Simulation Class
This commit is contained in:
parent
e40dddde52
commit
bdb698394a
1 changed files with 34 additions and 34 deletions
|
@ -38,40 +38,6 @@ class Simulation {
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
void addRandomPattern({int seed, int amount, num spreadFromCenter}) {
|
|
||||||
math.Random rng = _getRNG(seed ?? DateTime.now().millisecondsSinceEpoch);
|
|
||||||
amount ??= rng.nextInt(_RANDOM_PATTERN_AMOUNT);
|
|
||||||
spreadFromCenter ??= _RANDOM_PATTERN_SPREAD_FROM_CENTER;
|
|
||||||
|
|
||||||
int sanityCheck = 0;
|
|
||||||
Map<int, bool> changeSet = {};
|
|
||||||
for (var i = 0; i < (amount); i++) {
|
|
||||||
sanityCheck++;
|
|
||||||
math.Point cell = _getRandomPoint(rng, spreadFromCenter);
|
|
||||||
map.get(cell.x, cell.y)
|
|
||||||
? i--
|
|
||||||
: changeSet[map.toIndex(cell.x, cell.y)] = true;
|
|
||||||
if (sanityCheck > 100 && sanityCheck > i * 3) break;
|
|
||||||
}
|
|
||||||
mergeStateChanges(changeSet);
|
|
||||||
}
|
|
||||||
|
|
||||||
math.Random _getRNG(int seed) {
|
|
||||||
math.Random rng = new math.Random(seed);
|
|
||||||
return rng;
|
|
||||||
}
|
|
||||||
|
|
||||||
math.Point<int> _getRandomPoint(math.Random rng, num spreadFromCenter) {
|
|
||||||
math.Point absoluteSpread =
|
|
||||||
math.Point(map.width * spreadFromCenter, map.height * spreadFromCenter);
|
|
||||||
math.Point center = math.Point(map.width / 2, map.height / 2);
|
|
||||||
num cx = rng.nextInt(absoluteSpread.x.toInt()) +
|
|
||||||
(center.x - absoluteSpread.x / 2);
|
|
||||||
num cy = rng.nextInt(absoluteSpread.y.toInt()) +
|
|
||||||
(center.y - absoluteSpread.y / 2);
|
|
||||||
return math.Point<int>(cx.toInt(), cy.toInt());
|
|
||||||
}
|
|
||||||
|
|
||||||
void toggleCell(int x, int y) {
|
void toggleCell(int x, int y) {
|
||||||
map.set(x, y, !map.get(x, y));
|
map.set(x, y, !map.get(x, y));
|
||||||
}
|
}
|
||||||
|
@ -117,6 +83,40 @@ class Simulation {
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void addRandomPattern({int seed, int amount, num spreadFromCenter}) {
|
||||||
|
math.Random rng = _getRNG(seed ?? DateTime.now().millisecondsSinceEpoch);
|
||||||
|
amount ??= rng.nextInt(_RANDOM_PATTERN_AMOUNT);
|
||||||
|
spreadFromCenter ??= _RANDOM_PATTERN_SPREAD_FROM_CENTER;
|
||||||
|
|
||||||
|
int sanityCheck = 0;
|
||||||
|
Map<int, bool> changeSet = {};
|
||||||
|
for (var i = 0; i < (amount); i++) {
|
||||||
|
sanityCheck++;
|
||||||
|
math.Point cell = _getRandomPoint(rng, spreadFromCenter);
|
||||||
|
map.get(cell.x, cell.y)
|
||||||
|
? i--
|
||||||
|
: changeSet[map.toIndex(cell.x, cell.y)] = true;
|
||||||
|
if (sanityCheck > 100 && sanityCheck > i * 3) break;
|
||||||
|
}
|
||||||
|
mergeStateChanges(changeSet);
|
||||||
|
}
|
||||||
|
|
||||||
|
math.Random _getRNG(int seed) {
|
||||||
|
math.Random rng = new math.Random(seed);
|
||||||
|
return rng;
|
||||||
|
}
|
||||||
|
|
||||||
|
math.Point<int> _getRandomPoint(math.Random rng, num spreadFromCenter) {
|
||||||
|
math.Point absoluteSpread =
|
||||||
|
math.Point(map.width * spreadFromCenter, map.height * spreadFromCenter);
|
||||||
|
math.Point center = math.Point(map.width / 2, map.height / 2);
|
||||||
|
num cx = rng.nextInt(absoluteSpread.x.toInt()) +
|
||||||
|
(center.x - absoluteSpread.x / 2);
|
||||||
|
num cy = rng.nextInt(absoluteSpread.y.toInt()) +
|
||||||
|
(center.y - absoluteSpread.y / 2);
|
||||||
|
return math.Point<int>(cx.toInt(), cy.toInt());
|
||||||
|
}
|
||||||
|
|
||||||
void render(html.CanvasElement canvas, [num interp]) {
|
void render(html.CanvasElement canvas, [num interp]) {
|
||||||
// only renders if any cells changed between renders
|
// only renders if any cells changed between renders
|
||||||
if (!dirty) return;
|
if (!dirty) return;
|
||||||
|
|
Loading…
Reference in a new issue