diff --git a/lib/app_component.dart b/lib/app_component.dart index e559ab7..c23443e 100644 --- a/lib/app_component.dart +++ b/lib/app_component.dart @@ -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(); + } } diff --git a/lib/src/App.dart b/lib/src/App.dart index cfbc032..8ab933a 100644 --- a/lib/src/App.dart +++ b/lib/src/App.dart @@ -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) { diff --git a/lib/src/Grid.dart b/lib/src/Grid.dart index c03b413..062a78c 100644 --- a/lib/src/Grid.dart +++ b/lib/src/Grid.dart @@ -12,6 +12,12 @@ class Grid { final List> 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> _buildGrid(int w, int h) { print("grid being created"); List> 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; } } }