From de1aa46743709fe45057a8972cfe9ac1f829b22c Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 18 Oct 2018 10:58:06 +0200 Subject: [PATCH] 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. --- lib/src/Engine.dart | 5 ++++- lib/src/Simulation.dart | 8 +++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/src/Engine.dart b/lib/src/Engine.dart index e00b50f..59bcc66 100644 --- a/lib/src/Engine.dart +++ b/lib/src/Engine.dart @@ -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 simulationUpdate = _simulation.update(); + _simulation.mergeStateChanges(simulationUpdate); + + if (simulationUpdate.length == 0) running = false; } /// Advances Logic One Update diff --git a/lib/src/Simulation.dart b/lib/src/Simulation.dart index b0bbb46..8cc04ef 100644 --- a/lib/src/Simulation.dart +++ b/lib/src/Simulation.dart @@ -72,12 +72,14 @@ class Simulation { Map update() { Map stateChanges = calculateNextState(map); - - stateChanges.forEach((i, el) => map[i] = el); - stateChanges.length != 0 ? _dirty = true : false; return stateChanges; } + void mergeStateChanges(Map stateChanges) { + stateChanges.forEach((i, el) => map[i] = el); + if (stateChanges.length != 0) _dirty = true; + } + Map calculateNextState(Grid oldState) { Map stateChanges = Map();