Merge branch 'random-starting-pattern'
This commit is contained in:
commit
a71d442b45
3 changed files with 44 additions and 11 deletions
|
@ -44,8 +44,12 @@ class AppComponent implements OnInit {
|
||||||
}
|
}
|
||||||
|
|
||||||
void onResetClicked() {
|
void onResetClicked() {
|
||||||
|
engine.running = false;
|
||||||
engine.reset();
|
engine.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
void onRandomClicked() {}
|
void onRandomClicked() {
|
||||||
|
engine.running = false;
|
||||||
|
engine.grid.addPattern();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,12 +24,13 @@ class App {
|
||||||
|
|
||||||
App(this.canvas) {
|
App(this.canvas) {
|
||||||
_elapsed.start();
|
_elapsed.start();
|
||||||
grid.startingPattern();
|
grid.addPattern(amount: 15, dispersal: 5);
|
||||||
}
|
}
|
||||||
|
|
||||||
void reset() {
|
void reset() {
|
||||||
grid = new Grid(100, 100);
|
// grid = new Grid(100, 100);
|
||||||
running = false;
|
// running = false;
|
||||||
|
grid.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
void process(num now) {
|
void process(num now) {
|
||||||
|
|
|
@ -12,6 +12,12 @@ class Grid {
|
||||||
final List<List<Cell>> map;
|
final List<List<Cell>> map;
|
||||||
|
|
||||||
bool _dirty = true;
|
bool _dirty = true;
|
||||||
|
int _startingSeed;
|
||||||
|
int _x;
|
||||||
|
int _y;
|
||||||
|
int _amount;
|
||||||
|
int _dispersal;
|
||||||
|
Pattern _pattern;
|
||||||
|
|
||||||
Grid(int w, int h)
|
Grid(int w, int h)
|
||||||
: this.w = w,
|
: this.w = w,
|
||||||
|
@ -22,9 +28,21 @@ class Grid {
|
||||||
print("Grid creation finished");
|
print("Grid creation finished");
|
||||||
}
|
}
|
||||||
|
|
||||||
void startingPattern(
|
void reset() {
|
||||||
{Pattern pattern, int x, int y, int amount, int dispersal}) {
|
map.setAll(0, _buildGrid(w, h));
|
||||||
math.Random rng = new math.Random();
|
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 cx = x ?? rng.nextInt(w ~/ 3) + (w ~/ 3);
|
||||||
int cy = y ?? rng.nextInt(h ~/ 3) + (h ~/ 3);
|
int cy = y ?? rng.nextInt(h ~/ 3) + (h ~/ 3);
|
||||||
switch (pattern) {
|
switch (pattern) {
|
||||||
|
@ -54,18 +72,28 @@ class Grid {
|
||||||
setCellState(0 + cx, 2 + cy, true);
|
setCellState(0 + cx, 2 + cy, true);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
for (var i = 0; i < amount ?? rng.nextInt(20); i++) {
|
int sanityCheck = 0;
|
||||||
setCellState(cx + dispersal ?? rng.nextInt(10),
|
for (var i = 0; i < (_amount); i++) {
|
||||||
cy + dispersal ?? rng.nextInt(10), true);
|
sanityCheck++;
|
||||||
|
getCellState(cx, cy)
|
||||||
|
? i--
|
||||||
|
: setCellState(cx + rng.nextInt(_dispersal),
|
||||||
|
cy + rng.nextInt(_dispersal), true);
|
||||||
|
if (sanityCheck > 100 && sanityCheck > i*3) break;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
_dirty = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setCellState(int x, int y, bool state) {
|
void setCellState(int x, int y, bool state) {
|
||||||
if (y < map.length && x < map[y].length) map[y][x].state = 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) {
|
List<List<Cell>> _buildGrid(int w, int h) {
|
||||||
print("grid being created");
|
print("grid being created");
|
||||||
List<List<Cell>> grid = new List(h);
|
List<List<Cell>> grid = new List(h);
|
||||||
|
@ -110,13 +138,13 @@ class Grid {
|
||||||
for (int x = 0; x < w; x++) {
|
for (int x = 0; x < w; x++) {
|
||||||
// DEFAULTS TO CONWAY GAME OF LIFE RANGE OF ONE
|
// DEFAULTS TO CONWAY GAME OF LIFE RANGE OF ONE
|
||||||
map[y][x].update(getSurroundingNeighbors(x, y, 1));
|
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 y = 0; y < h; y++) {
|
||||||
for (int x = 0; x < w; x++) {
|
for (int x = 0; x < w; x++) {
|
||||||
// DEFAULTS TO CONWAY GAME OF LIFE RANGE OF ONE
|
// DEFAULTS TO CONWAY GAME OF LIFE RANGE OF ONE
|
||||||
map[y][x].advanceState();
|
map[y][x].advanceState();
|
||||||
|
if (!_dirty && map[y][x].dirty) _dirty = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue