Separate Simulation calculating updates and merging

Simulation updates were one step of calculation and merging the calculations into the map in one function.

Separating the two allows checking for a new update without affecting the grid, allows passing the last Update around and allows custom changes to the grid by passing changes to the merge function that were not derived from the update function.
This commit is contained in:
Unknown 2018-10-18 10:58:06 +02:00
parent e16085153a
commit de1aa46743
2 changed files with 9 additions and 4 deletions

View File

@ -102,7 +102,10 @@ class Engine {
/// If simulation should be advanced manually one time, prefer using step().
void update() {
// TODO: create hasUpdated/hasAdvanced method in simulation to abstract actual updating away
if (_simulation.update().length == 0) running = false;
Map<int, bool> simulationUpdate = _simulation.update();
_simulation.mergeStateChanges(simulationUpdate);
if (simulationUpdate.length == 0) running = false;
}
/// Advances Logic One Update

View File

@ -72,12 +72,14 @@ class Simulation {
Map<int, bool> update() {
Map<int, bool> stateChanges = calculateNextState(map);
stateChanges.forEach((i, el) => map[i] = el);
stateChanges.length != 0 ? _dirty = true : false;
return stateChanges;
}
void mergeStateChanges(Map<int, bool> stateChanges) {
stateChanges.forEach((i, el) => map[i] = el);
if (stateChanges.length != 0) _dirty = true;
}
Map<int, bool> calculateNextState(Grid<bool> oldState) {
Map<int, bool> stateChanges = Map();