Move Simulation Accesses to Simulation Class

Everything has been refactored away from engine, which now only controls updating & rendering within a specific timestep. (As well as stepping forward by calling a single update)

Everything regarding grids, patterns and cells has been moved into the simulation and the Services have been updated to reflect that.
This commit is contained in:
Unknown 2018-10-18 15:21:50 +02:00
parent 32a3676d95
commit 8db9cd6ff1
5 changed files with 33 additions and 63 deletions

View file

@ -29,16 +29,6 @@ class Engine {
// ms stuck in updateloop after which game will declare itself unresponsive
final int SAFETY_TIMEOUT = 2000;
/// Grid Size
///
/// Number of cells on x coordinate and y coordinate. Can be set individually.
Point get gridSize => Point<int>(_simulation.w, _simulation.h);
void set gridSize(Point<int> value) {
if (value.x <= 0 || value.y <= 0)
throw ArgumentError("grid size must not be smaller than 1");
_simulation = Simulation(value.x, value.y);
}
num _updateLag = 0.0;
num _drawLag = 0.0;
@ -51,11 +41,8 @@ class Engine {
Simulation _simulation;
bool running = false;
Engine([x = 100, y = 100, this.canvas]) {
_simulation = Simulation(x, y);
Engine() {
_elapsed.start();
_simulation.addRandomPattern(amount: 15, dispersal: 5);
html.window.animationFrame.then(animFrame);
}
@ -64,16 +51,6 @@ class Engine {
html.window.animationFrame.then(animFrame);
}
void reset() {
_simulation.reset();
running = false;
}
void clear() {
_simulation = new Simulation(gridSize.x, gridSize.y);
running = false;
}
void process(num now) {
_drawLag += _elapsed.elapsedMilliseconds;
_updateLag += _elapsed.elapsedMilliseconds;
@ -101,6 +78,8 @@ class Engine {
/// directly, since it is automatically taken care of by the processing function.
/// If simulation should be advanced manually one time, prefer using step().
void update() {
if (_simulation == null) return;
Map<int, bool> simulationUpdate = _simulation.update();
_simulation.mergeStateChanges(simulationUpdate);
@ -123,16 +102,10 @@ class Engine {
/// the internal engine processing. Does not do anything if no canvas is
/// defined.
void render([num interp]) {
if (canvas == null) return;
if (canvas == null || _simulation == null) return;
_simulation.render(canvas, interp);
}
void addPattern({int amount, int dispersal}) {
_simulation.addRandomPattern(amount: amount, dispersal: dispersal);
}
void toggleEdgeRendering() {
_simulation.renderEdges = !_simulation.renderEdges;
}
void set simulation(Simulation value) => _simulation = value;
}