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

@ -1,4 +1,5 @@
import 'package:rules_of_living/src/Engine.dart';
import 'package:rules_of_living/src/Simulation.dart';
class EngineService {
Engine _uncachedEngineAccess;
@ -41,18 +42,7 @@ class EngineService {
int get simSpeed => engine.stepsPerSecond;
void set simSpeed(int val) => engine.stepsPerSecond = val;
void reset() {
engine.reset();
}
void addRandomPattern() {
engine.running = false;
engine.addPattern();
}
void clear() {
engine.clear();
}
bool get isRunning => engine.running;
void set simulation(Simulation value) => engine.simulation = value;
}

View file

@ -2,35 +2,42 @@ import 'dart:html' as html;
import 'dart:math';
import 'package:rules_of_living/service/engine_service.dart';
import 'package:rules_of_living/src/Simulation.dart';
class SimulationService {
final EngineService _engine;
// DEFAULT VALUES
static final int DEFAULT_GRID_SIZE = 50;
SimulationService(this._engine);
final EngineService _engine;
final Simulation _sim = Simulation(DEFAULT_GRID_SIZE, DEFAULT_GRID_SIZE);
SimulationService(this._engine) {
_engine.simulation = _sim;
_sim.addRandomPattern(amount: 15, dispersal: 5);
}
void reset() {
_engine.reset();
_sim.reset();
}
void addRandomPattern() {
_engine.addRandomPattern();
_sim.addRandomPattern();
}
void clear() {
_engine.clear();
_sim.reset();
}
Point<int> get gridSize => _sim.gridSize;
void set gridSize(Point<int> size) {
_engine.engine.gridSize = size;
_sim.gridSize = size;
}
Point<int> get gridSize => _engine.engine.gridSize;
//TODO split into RenderService when rendering is decoupled from engine.
html.CanvasElement get canvas => _engine.engine.canvas;
void set canvas(html.CanvasElement canvas) => _engine.engine.canvas = canvas;
void toggleGrid() {
_engine.engine.toggleEdgeRendering();
_sim.renderEdges = !_sim.renderEdges;
}
}

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;
}

View file

@ -1,5 +1,6 @@
import 'dart:html' as html;
import 'dart:math' as math;
import 'dart:math';
import 'package:rules_of_living/src/Grid.dart';
import 'package:rules_of_living/src/rules/GameOfLife.dart';
@ -8,7 +9,7 @@ import 'package:rules_of_living/src/rules/RuleSet.dart';
enum CellPattern { SpaceShip, Blinker }
class Simulation {
final Grid<bool> map;
Grid<bool> map;
RuleSet rules = GameOfLife();
bool _dirty = true;
@ -20,6 +21,13 @@ class Simulation {
int get w => map.width;
int get h => map.height;
Point get gridSize => Point<int>(w, h);
void set gridSize(Point<int> value) {
if (value.x <= 0 || value.y <= 0)
throw ArgumentError("grid size must not be smaller than 1");
map = Grid(value.x, value.y);
}
Simulation(int w, int h) : this.map = new Grid(w, h) {
reset();
print("Grid Created");

View file

@ -31,12 +31,4 @@ void main() {
expect(sut.canvas, isNotNull);
});
});
group("gridSize", () {
test("zero gridSizes throw ArgumentErrors", () {
expect(() => sut.gridSize = Point(0, 5), throwsArgumentError);
});
test("negative gridSizes throw ArgumentErrors", () {
expect(() => sut.gridSize = Point(1, -5), throwsArgumentError);
});
});
}