Re-Order Simulation Class

This commit is contained in:
Unknown 2018-10-23 21:02:42 +02:00
parent e40dddde52
commit bdb698394a

View file

@ -38,40 +38,6 @@ class Simulation {
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) {
map.set(x, y, !map.get(x, y));
}
@ -117,6 +83,40 @@ class Simulation {
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]) {
// only renders if any cells changed between renders
if (!dirty) return;