Merge branch 'random-starting-pattern'

This commit is contained in:
Marty Oehme 2018-07-08 19:00:07 +02:00
commit a71d442b45
3 changed files with 44 additions and 11 deletions

View File

@ -44,8 +44,12 @@ class AppComponent implements OnInit {
}
void onResetClicked() {
engine.running = false;
engine.reset();
}
void onRandomClicked() {}
void onRandomClicked() {
engine.running = false;
engine.grid.addPattern();
}
}

View File

@ -24,12 +24,13 @@ class App {
App(this.canvas) {
_elapsed.start();
grid.startingPattern();
grid.addPattern(amount: 15, dispersal: 5);
}
void reset() {
grid = new Grid(100, 100);
running = false;
// grid = new Grid(100, 100);
// running = false;
grid.reset();
}
void process(num now) {

View File

@ -12,6 +12,12 @@ class Grid {
final List<List<Cell>> map;
bool _dirty = true;
int _startingSeed;
int _x;
int _y;
int _amount;
int _dispersal;
Pattern _pattern;
Grid(int w, int h)
: this.w = w,
@ -22,9 +28,21 @@ class Grid {
print("Grid creation finished");
}
void startingPattern(
{Pattern pattern, int x, int y, int amount, int dispersal}) {
math.Random rng = new math.Random();
void reset() {
map.setAll(0, _buildGrid(w, h));
if(_startingSeed != null) addPattern(pattern: _pattern, dispersal: _dispersal,amount: _amount,seed: _startingSeed, x: _x, y: _y);
_dirty = true;
}
void addPattern(
{Pattern pattern, int x, int y, int amount, int dispersal, int seed}) {
_startingSeed = seed ?? DateTime.now().millisecondsSinceEpoch;
math.Random rng = new math.Random(_startingSeed);
_x = x;
_y = y;
_amount = amount ?? rng.nextInt(20);
_dispersal = dispersal ?? 10;
_pattern = pattern;
int cx = x ?? rng.nextInt(w ~/ 3) + (w ~/ 3);
int cy = y ?? rng.nextInt(h ~/ 3) + (h ~/ 3);
switch (pattern) {
@ -54,18 +72,28 @@ class Grid {
setCellState(0 + cx, 2 + cy, true);
break;
default:
for (var i = 0; i < amount ?? rng.nextInt(20); i++) {
setCellState(cx + dispersal ?? rng.nextInt(10),
cy + dispersal ?? rng.nextInt(10), true);
int sanityCheck = 0;
for (var i = 0; i < (_amount); i++) {
sanityCheck++;
getCellState(cx, cy)
? i--
: setCellState(cx + rng.nextInt(_dispersal),
cy + rng.nextInt(_dispersal), true);
if (sanityCheck > 100 && sanityCheck > i*3) break;
}
break;
}
_dirty = true;
}
void setCellState(int x, int y, bool state) {
if (y < map.length && x < map[y].length) map[y][x].state = state;
}
bool getCellState(int x, int y) {
if (y < map.length && x < map[y].length) return map[y][x].state;
}
List<List<Cell>> _buildGrid(int w, int h) {
print("grid being created");
List<List<Cell>> grid = new List(h);
@ -110,13 +138,13 @@ class Grid {
for (int x = 0; x < w; x++) {
// DEFAULTS TO CONWAY GAME OF LIFE RANGE OF ONE
map[y][x].update(getSurroundingNeighbors(x, y, 1));
if (!_dirty && map[y][x].dirty) _dirty = true;
}
}
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
// DEFAULTS TO CONWAY GAME OF LIFE RANGE OF ONE
map[y][x].advanceState();
if (!_dirty && map[y][x].dirty) _dirty = true;
}
}
}