From 8db9cd6ff1c363b21913b468ffa83be14db79055 Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 18 Oct 2018 15:21:50 +0200 Subject: [PATCH] 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. --- lib/service/engine_service.dart | 16 +++---------- lib/service/simulation_service.dart | 25 ++++++++++++------- lib/src/Engine.dart | 37 ++++------------------------- lib/src/Simulation.dart | 10 +++++++- test/src/engine_test.dart | 8 ------- 5 files changed, 33 insertions(+), 63 deletions(-) diff --git a/lib/service/engine_service.dart b/lib/service/engine_service.dart index a76fedb..b276d28 100644 --- a/lib/service/engine_service.dart +++ b/lib/service/engine_service.dart @@ -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; } diff --git a/lib/service/simulation_service.dart b/lib/service/simulation_service.dart index b5a9021..396290b 100644 --- a/lib/service/simulation_service.dart +++ b/lib/service/simulation_service.dart @@ -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 get gridSize => _sim.gridSize; void set gridSize(Point size) { - _engine.engine.gridSize = size; + _sim.gridSize = size; } - Point 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; } } diff --git a/lib/src/Engine.dart b/lib/src/Engine.dart index 3653353..ad101ca 100644 --- a/lib/src/Engine.dart +++ b/lib/src/Engine.dart @@ -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(_simulation.w, _simulation.h); - void set gridSize(Point 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 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; } diff --git a/lib/src/Simulation.dart b/lib/src/Simulation.dart index 42df2a4..fd8bbcb 100644 --- a/lib/src/Simulation.dart +++ b/lib/src/Simulation.dart @@ -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 map; + Grid 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(w, h); + void set gridSize(Point 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"); diff --git a/test/src/engine_test.dart b/test/src/engine_test.dart index fcfefc2..bd184a4 100644 --- a/test/src/engine_test.dart +++ b/test/src/engine_test.dart @@ -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); - }); - }); }