From 17697070ee4df34a9d7ed3f90cedd4ae06e9f418 Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 18 Oct 2018 11:58:38 +0200 Subject: [PATCH 01/10] Move ControlService tasks into EngineService All ControlService was used for was a redirection to the engine service. This will be further split up in the future into more logical units of responsibility. --- lib/app_component.dart | 2 -- lib/components/controls_component.dart | 4 +-- lib/service/control_service.dart | 38 -------------------------- lib/service/engine_service.dart | 31 +++++++++++++++++++++ 4 files changed, 33 insertions(+), 42 deletions(-) delete mode 100644 lib/service/control_service.dart diff --git a/lib/app_component.dart b/lib/app_component.dart index ae8a931..2d4557f 100644 --- a/lib/app_component.dart +++ b/lib/app_component.dart @@ -5,7 +5,6 @@ import 'package:rules_of_living/components/controls_component.dart'; import 'package:rules_of_living/components/header_component.dart'; import 'package:rules_of_living/components/simulation_component.dart'; import 'package:rules_of_living/service/configuration_service.dart'; -import 'package:rules_of_living/service/control_service.dart'; import 'package:rules_of_living/service/engine_service.dart'; @Component( @@ -25,7 +24,6 @@ import 'package:rules_of_living/service/engine_service.dart'; materialProviders, ClassProvider(EngineService), ClassProvider(ConfigurationService), - ClassProvider(ControlService) ], styleUrls: const [ 'package:angular_components/app_layout/layout.scss.css', diff --git a/lib/components/controls_component.dart b/lib/components/controls_component.dart index 954d94b..f4b5264 100644 --- a/lib/components/controls_component.dart +++ b/lib/components/controls_component.dart @@ -1,6 +1,6 @@ import 'package:angular/angular.dart'; import 'package:angular_components/angular_components.dart'; -import 'package:rules_of_living/service/control_service.dart'; +import 'package:rules_of_living/service/engine_service.dart'; @Component( selector: 'sim-controls', @@ -15,7 +15,7 @@ import 'package:rules_of_living/service/control_service.dart'; styleUrls: const ["controls_component.css"], ) class ControlsComponent { - final ControlService ctrl; + final EngineService ctrl; ControlsComponent(this.ctrl); diff --git a/lib/service/control_service.dart b/lib/service/control_service.dart deleted file mode 100644 index 0066106..0000000 --- a/lib/service/control_service.dart +++ /dev/null @@ -1,38 +0,0 @@ -import 'package:rules_of_living/service/engine_service.dart'; - -class ControlService { - EngineService _es; - - ControlService(this._es); - - void run() { - _es.engine.running = true; - } - - void stop() { - _es.engine.running = false; - } - - void toggleRunning() { - _es.engine.running = !_es.engine.running; - } - - void step() { - _es.engine.step(); - } - - void reset() { - _es.engine.reset(); - } - - void addRandomPattern() { - _es.engine.running = false; - _es.engine.addPattern(); - } - - void clear() { - _es.engine.clear(); - } - - bool get isRunning => _es.engine.running; -} diff --git a/lib/service/engine_service.dart b/lib/service/engine_service.dart index 8168735..df1492c 100644 --- a/lib/service/engine_service.dart +++ b/lib/service/engine_service.dart @@ -12,4 +12,35 @@ class EngineService { engine = newEngine; return newEngine; } + + void run() { + engine.running = true; + } + + void stop() { + engine.running = false; + } + + void toggleRunning() { + engine.running = !engine.running; + } + + void step() { + engine.step(); + } + + void reset() { + engine.reset(); + } + + void addRandomPattern() { + engine.running = false; + engine.addPattern(); + } + + void clear() { + engine.clear(); + } + + bool get isRunning => engine.running; } From 72ce25a8066f2d1a5f970d584644796dee77420d Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 18 Oct 2018 11:59:36 +0200 Subject: [PATCH 02/10] Rename Controls component variable accessing engine --- lib/components/controls_component.dart | 14 +++++++------- lib/components/controls_component.html | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/components/controls_component.dart b/lib/components/controls_component.dart index f4b5264..511a7d4 100644 --- a/lib/components/controls_component.dart +++ b/lib/components/controls_component.dart @@ -15,27 +15,27 @@ import 'package:rules_of_living/service/engine_service.dart'; styleUrls: const ["controls_component.css"], ) class ControlsComponent { - final EngineService ctrl; + final EngineService engine; - ControlsComponent(this.ctrl); + ControlsComponent(this.engine); void onStartClicked() { - ctrl.toggleRunning(); + engine.toggleRunning(); } void onStepClicked() { - ctrl.step(); + engine.step(); } void onResetClicked() { - ctrl.reset(); + engine.reset(); } void onRandomClicked() { - ctrl.addRandomPattern(); + engine.addRandomPattern(); } void onClearClicked() { - ctrl.clear(); + engine.clear(); } } diff --git a/lib/components/controls_component.html b/lib/components/controls_component.html index b4c43f3..42e79f7 100644 --- a/lib/components/controls_component.html +++ b/lib/components/controls_component.html @@ -1,7 +1,7 @@
- + From 6b4786fdd08e250da3cd8ac60dfab47d4d7b73db Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 18 Oct 2018 12:05:02 +0200 Subject: [PATCH 03/10] Add SimulationService to controls Will eventually attach to the Simulation directly without first going through Engine. For now just redirects calls to EngineService to keep functions intact. --- lib/app_component.dart | 2 ++ lib/components/controls_component.dart | 10 ++++++---- lib/service/simulation_service.dart | 19 +++++++++++++++++++ 3 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 lib/service/simulation_service.dart diff --git a/lib/app_component.dart b/lib/app_component.dart index 2d4557f..eaa2e41 100644 --- a/lib/app_component.dart +++ b/lib/app_component.dart @@ -6,6 +6,7 @@ import 'package:rules_of_living/components/header_component.dart'; import 'package:rules_of_living/components/simulation_component.dart'; import 'package:rules_of_living/service/configuration_service.dart'; import 'package:rules_of_living/service/engine_service.dart'; +import 'package:rules_of_living/service/simulation_service.dart'; @Component( selector: 'my-app', @@ -24,6 +25,7 @@ import 'package:rules_of_living/service/engine_service.dart'; materialProviders, ClassProvider(EngineService), ClassProvider(ConfigurationService), + ClassProvider(SimulationService) ], styleUrls: const [ 'package:angular_components/app_layout/layout.scss.css', diff --git a/lib/components/controls_component.dart b/lib/components/controls_component.dart index 511a7d4..e524e60 100644 --- a/lib/components/controls_component.dart +++ b/lib/components/controls_component.dart @@ -1,6 +1,7 @@ import 'package:angular/angular.dart'; import 'package:angular_components/angular_components.dart'; import 'package:rules_of_living/service/engine_service.dart'; +import 'package:rules_of_living/service/simulation_service.dart'; @Component( selector: 'sim-controls', @@ -16,8 +17,9 @@ import 'package:rules_of_living/service/engine_service.dart'; ) class ControlsComponent { final EngineService engine; + final SimulationService sim; - ControlsComponent(this.engine); + ControlsComponent(this.engine, this.sim); void onStartClicked() { engine.toggleRunning(); @@ -28,14 +30,14 @@ class ControlsComponent { } void onResetClicked() { - engine.reset(); + sim.reset(); } void onRandomClicked() { - engine.addRandomPattern(); + sim.addRandomPattern(); } void onClearClicked() { - engine.clear(); + sim.clear(); } } diff --git a/lib/service/simulation_service.dart b/lib/service/simulation_service.dart new file mode 100644 index 0000000..1081bf0 --- /dev/null +++ b/lib/service/simulation_service.dart @@ -0,0 +1,19 @@ +import 'package:rules_of_living/service/engine_service.dart'; + +class SimulationService { + final EngineService engine; + + SimulationService(this.engine); + + void reset() { + engine.reset(); + } + + void addRandomPattern() { + engine.addRandomPattern(); + } + + void clear() { + engine.clear(); + } +} From 7729da3a40be6d65e951190a5a7061828dd34766 Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 18 Oct 2018 12:16:47 +0200 Subject: [PATCH 04/10] Split ConfigurationService to use SimulationService Methods concerning engine make use of EngineService, those concerning grid and patterns make use of SimulationService. --- lib/service/configuration_service.dart | 19 ++++++++++--------- lib/service/simulation_service.dart | 18 +++++++++++++----- test/service/configuration_service_test.dart | 4 +++- 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/lib/service/configuration_service.dart b/lib/service/configuration_service.dart index e11a5e8..045e66c 100644 --- a/lib/service/configuration_service.dart +++ b/lib/service/configuration_service.dart @@ -2,15 +2,17 @@ import 'dart:html' as html; import 'dart:math'; import 'package:rules_of_living/service/engine_service.dart'; +import 'package:rules_of_living/service/simulation_service.dart'; class ConfigurationService { - final EngineService _es; + final EngineService _engine; + final SimulationService _sim; bool showGrid; int _simSpeed; - ConfigurationService(this._es) { + ConfigurationService(this._engine, this._sim) { showGrid = false; simSpeed = 5; } @@ -23,21 +25,20 @@ class ConfigurationService { int get simSpeed => _simSpeed; void set simSpeed(int val) { _simSpeed = val; - _es.engine.stepsPerSecond = simSpeed; + //TODO make method in EngineService to respect Demeter + _engine.engine.stepsPerSecond = simSpeed; } - void set canvas(html.CanvasElement canvas) => _es.engine.canvas = canvas; - html.CanvasElement get canvas => _es.engine.canvas; + void set canvas(html.CanvasElement canvas) => _engine.engine.canvas = canvas; + html.CanvasElement get canvas => _engine.engine.canvas; void toggleGrid() { showGrid = !showGrid; } void setGridSize({int x, int y}) { - x = x ?? _es.engine.gridSize.x; - y = y ?? _es.engine.gridSize.y; - _es.engine.gridSize = Point(x, y); + _sim.gridSize = Point(x ?? gridSize.x, y ?? gridSize.y); } - Point get gridSize => _es.engine.gridSize; + Point get gridSize => _sim.gridSize; } diff --git a/lib/service/simulation_service.dart b/lib/service/simulation_service.dart index 1081bf0..45a7b09 100644 --- a/lib/service/simulation_service.dart +++ b/lib/service/simulation_service.dart @@ -1,19 +1,27 @@ +import 'dart:math'; + import 'package:rules_of_living/service/engine_service.dart'; class SimulationService { - final EngineService engine; + final EngineService _engine; - SimulationService(this.engine); + SimulationService(this._engine); void reset() { - engine.reset(); + _engine.reset(); } void addRandomPattern() { - engine.addRandomPattern(); + _engine.addRandomPattern(); } void clear() { - engine.clear(); + _engine.clear(); } + + void set gridSize(Point size) { + _engine.engine.gridSize = size; + } + + Point get gridSize => _engine.engine.gridSize; } diff --git a/test/service/configuration_service_test.dart b/test/service/configuration_service_test.dart index 44e23b2..9aa4893 100644 --- a/test/service/configuration_service_test.dart +++ b/test/service/configuration_service_test.dart @@ -3,6 +3,7 @@ import 'dart:math'; import 'package:mockito/mockito.dart'; import 'package:rules_of_living/service/configuration_service.dart'; import 'package:rules_of_living/service/engine_service.dart'; +import 'package:rules_of_living/service/simulation_service.dart'; import 'package:rules_of_living/src/Engine.dart'; @TestOn('browser') import 'package:test/test.dart'; @@ -12,12 +13,13 @@ class MockEngine extends Mock implements Engine {} void main() { ConfigurationService sut; EngineService engineService; + SimulationService simService; MockEngine me; setUp(() { me = MockEngine(); engineService = EngineService(); engineService.engine = me; - sut = ConfigurationService(engineService); + sut = ConfigurationService(engineService, simService); }); group("simulation speed", () { From 99ead8691bc22b1dfe3676e61ded0757409f70be Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 18 Oct 2018 12:29:46 +0200 Subject: [PATCH 05/10] Make gridSize in Services pass correct signature Both need to conform to Point to be accepted by the engine. --- lib/service/configuration_service.dart | 2 +- lib/service/simulation_service.dart | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/service/configuration_service.dart b/lib/service/configuration_service.dart index 045e66c..ffd0b78 100644 --- a/lib/service/configuration_service.dart +++ b/lib/service/configuration_service.dart @@ -37,7 +37,7 @@ class ConfigurationService { } void setGridSize({int x, int y}) { - _sim.gridSize = Point(x ?? gridSize.x, y ?? gridSize.y); + _sim.gridSize = Point(x ?? gridSize.x, y ?? gridSize.y); } Point get gridSize => _sim.gridSize; diff --git a/lib/service/simulation_service.dart b/lib/service/simulation_service.dart index 45a7b09..0c967ef 100644 --- a/lib/service/simulation_service.dart +++ b/lib/service/simulation_service.dart @@ -19,7 +19,7 @@ class SimulationService { _engine.clear(); } - void set gridSize(Point size) { + void set gridSize(Point size) { _engine.engine.gridSize = size; } From 58971016dab2f92d0584548918b8262bf36bb2ad Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 18 Oct 2018 12:51:35 +0200 Subject: [PATCH 06/10] Remove ConfigurationService Replaced with direct access to both EngineService and SimulationService. --- lib/app_component.dart | 2 - lib/components/configuration_component.dart | 24 ++++++---- lib/components/controls_component.dart | 1 + lib/components/simulation_component.dart | 8 ++-- lib/service/configuration_service.dart | 44 ------------------ lib/service/engine_service.dart | 22 +++++++++ test/service/configuration_service_test.dart | 48 -------------------- 7 files changed, 41 insertions(+), 108 deletions(-) delete mode 100644 lib/service/configuration_service.dart delete mode 100644 test/service/configuration_service_test.dart diff --git a/lib/app_component.dart b/lib/app_component.dart index eaa2e41..1656739 100644 --- a/lib/app_component.dart +++ b/lib/app_component.dart @@ -4,7 +4,6 @@ import 'package:rules_of_living/components/configuration_component.dart'; import 'package:rules_of_living/components/controls_component.dart'; import 'package:rules_of_living/components/header_component.dart'; import 'package:rules_of_living/components/simulation_component.dart'; -import 'package:rules_of_living/service/configuration_service.dart'; import 'package:rules_of_living/service/engine_service.dart'; import 'package:rules_of_living/service/simulation_service.dart'; @@ -24,7 +23,6 @@ import 'package:rules_of_living/service/simulation_service.dart'; providers: [ materialProviders, ClassProvider(EngineService), - ClassProvider(ConfigurationService), ClassProvider(SimulationService) ], styleUrls: const [ diff --git a/lib/components/configuration_component.dart b/lib/components/configuration_component.dart index cc1ecd1..c72106c 100644 --- a/lib/components/configuration_component.dart +++ b/lib/components/configuration_component.dart @@ -1,3 +1,5 @@ +import 'dart:math'; + import 'package:angular/angular.dart'; import 'package:angular_components/material_button/material_button.dart'; import 'package:angular_components/material_icon/material_icon.dart'; @@ -5,7 +7,8 @@ import 'package:angular_components/material_input/material_input.dart'; import 'package:angular_components/material_input/material_number_accessor.dart'; import 'package:angular_components/material_slider/material_slider.dart'; import 'package:angular_components/material_tooltip/material_tooltip.dart'; -import 'package:rules_of_living/service/configuration_service.dart'; +import 'package:rules_of_living/service/engine_service.dart'; +import 'package:rules_of_living/service/simulation_service.dart'; @Component( selector: "configuration", @@ -23,28 +26,29 @@ import 'package:rules_of_living/service/configuration_service.dart'; NgModel ]) class ConfigurationComponent { - final ConfigurationService config; + final EngineService engine; + final SimulationService sim; - int get width => config.gridSize.x; + int get width => sim.gridSize.x; void set width(num value) { if (value == null || value <= 0) return; - config.setGridSize(x: value.toInt()); + sim.gridSize = Point(value, sim.gridSize.y); } - int get height => config.gridSize.y; + int get height => sim.gridSize.y; void set height(num value) { if (value == null || value <= 0) return; - config.setGridSize(y: value.toInt()); + sim.gridSize = Point(sim.gridSize.x, value); } - int get simSpeed => config.simSpeed; - void set simSpeed(int value) => config.simSpeed = value; + int get simSpeed => engine.simSpeed; + void set simSpeed(int value) => engine.simSpeed = value; String get speedSliderTooltip => "Simulation Speed: $simSpeed"; - ConfigurationComponent(this.config); + ConfigurationComponent(this.engine, this.sim); void onEdgesClicked() { - config.toggleGrid(); + engine.toggleGrid(); } } diff --git a/lib/components/controls_component.dart b/lib/components/controls_component.dart index e524e60..3f31741 100644 --- a/lib/components/controls_component.dart +++ b/lib/components/controls_component.dart @@ -35,6 +35,7 @@ class ControlsComponent { void onRandomClicked() { sim.addRandomPattern(); + engine.stop(); } void onClearClicked() { diff --git a/lib/components/simulation_component.dart b/lib/components/simulation_component.dart index 312eef4..91812fb 100644 --- a/lib/components/simulation_component.dart +++ b/lib/components/simulation_component.dart @@ -1,7 +1,7 @@ import 'dart:html' as html; import 'package:angular/angular.dart'; -import 'package:rules_of_living/service/configuration_service.dart'; +import 'package:rules_of_living/service/engine_service.dart'; @Component( selector: 'gol-simulation', @@ -10,9 +10,9 @@ import 'package:rules_of_living/service/configuration_service.dart'; providers: [], ) class SimulationComponent implements OnInit { - final ConfigurationService config; + final EngineService engine; - SimulationComponent(this.config); + SimulationComponent(this.engine); @override void ngOnInit() { @@ -29,6 +29,6 @@ class SimulationComponent implements OnInit { the canvas did not load correctly :( ''', canvas.width / 2 - 50, canvas.height / 2); - config.canvas = canvas; + engine.canvas = canvas; } } diff --git a/lib/service/configuration_service.dart b/lib/service/configuration_service.dart deleted file mode 100644 index ffd0b78..0000000 --- a/lib/service/configuration_service.dart +++ /dev/null @@ -1,44 +0,0 @@ -import 'dart:html' as html; -import 'dart:math'; - -import 'package:rules_of_living/service/engine_service.dart'; -import 'package:rules_of_living/service/simulation_service.dart'; - -class ConfigurationService { - final EngineService _engine; - final SimulationService _sim; - - bool showGrid; - - int _simSpeed; - - ConfigurationService(this._engine, this._sim) { - showGrid = false; - simSpeed = 5; - } - - /// Simulation Speed - /// - /// Sets the number of updates the simulation takes per second. Can range from - /// 1 to arbitrarily high numbers (though setting it too high can potentially - /// make the app brittle). - int get simSpeed => _simSpeed; - void set simSpeed(int val) { - _simSpeed = val; - //TODO make method in EngineService to respect Demeter - _engine.engine.stepsPerSecond = simSpeed; - } - - void set canvas(html.CanvasElement canvas) => _engine.engine.canvas = canvas; - html.CanvasElement get canvas => _engine.engine.canvas; - - void toggleGrid() { - showGrid = !showGrid; - } - - void setGridSize({int x, int y}) { - _sim.gridSize = Point(x ?? gridSize.x, y ?? gridSize.y); - } - - Point get gridSize => _sim.gridSize; -} diff --git a/lib/service/engine_service.dart b/lib/service/engine_service.dart index df1492c..6d40681 100644 --- a/lib/service/engine_service.dart +++ b/lib/service/engine_service.dart @@ -1,8 +1,14 @@ +import 'dart:html' as html; + import 'package:rules_of_living/src/Engine.dart'; class EngineService { Engine _uncachedEngineAccess; + EngineService() { + simSpeed = 5; + } + Engine get engine => _uncachedEngineAccess ?? _setCachedAndReturn(Engine()); void set engine(Engine newEngine) { _uncachedEngineAccess = newEngine; @@ -29,6 +35,22 @@ class EngineService { engine.step(); } + /// Simulation Speed + /// + /// Sets the number of updates the simulation takes per second. Can range from + /// 1 to arbitrarily high numbers (though setting it too high can potentially + /// make the app brittle). + int get simSpeed => engine.stepsPerSecond; + void set simSpeed(int val) => engine.stepsPerSecond = val; + + //TODO split into RenderService when rendering is decoupled from engine. + html.CanvasElement get canvas => engine.canvas; + void set canvas(html.CanvasElement canvas) => engine.canvas = canvas; + + void toggleGrid() { + engine.toggleEdgeRendering(); + } + void reset() { engine.reset(); } diff --git a/test/service/configuration_service_test.dart b/test/service/configuration_service_test.dart deleted file mode 100644 index 9aa4893..0000000 --- a/test/service/configuration_service_test.dart +++ /dev/null @@ -1,48 +0,0 @@ -import 'dart:math'; - -import 'package:mockito/mockito.dart'; -import 'package:rules_of_living/service/configuration_service.dart'; -import 'package:rules_of_living/service/engine_service.dart'; -import 'package:rules_of_living/service/simulation_service.dart'; -import 'package:rules_of_living/src/Engine.dart'; -@TestOn('browser') -import 'package:test/test.dart'; - -class MockEngine extends Mock implements Engine {} - -void main() { - ConfigurationService sut; - EngineService engineService; - SimulationService simService; - MockEngine me; - setUp(() { - me = MockEngine(); - engineService = EngineService(); - engineService.engine = me; - sut = ConfigurationService(engineService, simService); - }); - - group("simulation speed", () { - test("speed changes propagate to engine", () { - sut.simSpeed = 312; - verify(me.stepsPerSecond = 312); - }); - }); - - group("grid size", () { - test("grid changes are sent to engine", () { - sut.setGridSize(x: 512, y: 388); - verify(me.gridSize = Point(512, 388)); - }); - test("grid can be changed solely on x axis", () { - when(me.gridSize).thenReturn(Point(100, 100)); - sut.setGridSize(x: 555); - verify(me.gridSize = Point(555, 100)); - }); - test("grid can be changed solely on y axis", () { - when(me.gridSize).thenReturn(Point(100, 100)); - sut.setGridSize(y: 556); - verify(me.gridSize = Point(100, 556)); - }); - }); -} From 45e8f01acb2205896cd53387a5e5586e2cd34a72 Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 18 Oct 2018 14:41:57 +0200 Subject: [PATCH 07/10] Move Render Methods into SimulationService --- lib/components/configuration_component.dart | 2 +- lib/components/simulation_component.dart | 4 +++- lib/service/engine_service.dart | 10 ---------- lib/service/simulation_service.dart | 9 +++++++++ 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/lib/components/configuration_component.dart b/lib/components/configuration_component.dart index c72106c..abbd2e5 100644 --- a/lib/components/configuration_component.dart +++ b/lib/components/configuration_component.dart @@ -49,6 +49,6 @@ class ConfigurationComponent { ConfigurationComponent(this.engine, this.sim); void onEdgesClicked() { - engine.toggleGrid(); + sim.toggleGrid(); } } diff --git a/lib/components/simulation_component.dart b/lib/components/simulation_component.dart index 91812fb..1135865 100644 --- a/lib/components/simulation_component.dart +++ b/lib/components/simulation_component.dart @@ -2,6 +2,7 @@ import 'dart:html' as html; import 'package:angular/angular.dart'; import 'package:rules_of_living/service/engine_service.dart'; +import 'package:rules_of_living/service/simulation_service.dart'; @Component( selector: 'gol-simulation', @@ -11,6 +12,7 @@ import 'package:rules_of_living/service/engine_service.dart'; ) class SimulationComponent implements OnInit { final EngineService engine; + final SimulationService sim; SimulationComponent(this.engine); @@ -29,6 +31,6 @@ class SimulationComponent implements OnInit { the canvas did not load correctly :( ''', canvas.width / 2 - 50, canvas.height / 2); - engine.canvas = canvas; + sim.canvas = canvas; } } diff --git a/lib/service/engine_service.dart b/lib/service/engine_service.dart index 6d40681..a76fedb 100644 --- a/lib/service/engine_service.dart +++ b/lib/service/engine_service.dart @@ -1,5 +1,3 @@ -import 'dart:html' as html; - import 'package:rules_of_living/src/Engine.dart'; class EngineService { @@ -43,14 +41,6 @@ class EngineService { int get simSpeed => engine.stepsPerSecond; void set simSpeed(int val) => engine.stepsPerSecond = val; - //TODO split into RenderService when rendering is decoupled from engine. - html.CanvasElement get canvas => engine.canvas; - void set canvas(html.CanvasElement canvas) => engine.canvas = canvas; - - void toggleGrid() { - engine.toggleEdgeRendering(); - } - void reset() { engine.reset(); } diff --git a/lib/service/simulation_service.dart b/lib/service/simulation_service.dart index 0c967ef..b5a9021 100644 --- a/lib/service/simulation_service.dart +++ b/lib/service/simulation_service.dart @@ -1,3 +1,4 @@ +import 'dart:html' as html; import 'dart:math'; import 'package:rules_of_living/service/engine_service.dart'; @@ -24,4 +25,12 @@ class SimulationService { } 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(); + } } From bbfb2f735b26b3112f0773072b14bc5fb72e1138 Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 18 Oct 2018 14:41:57 +0200 Subject: [PATCH 08/10] Move Render Methods into SimulationService --- lib/components/configuration_component.dart | 2 +- lib/components/simulation_component.dart | 6 ++++-- lib/service/engine_service.dart | 10 ---------- lib/service/simulation_service.dart | 9 +++++++++ 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/lib/components/configuration_component.dart b/lib/components/configuration_component.dart index c72106c..abbd2e5 100644 --- a/lib/components/configuration_component.dart +++ b/lib/components/configuration_component.dart @@ -49,6 +49,6 @@ class ConfigurationComponent { ConfigurationComponent(this.engine, this.sim); void onEdgesClicked() { - engine.toggleGrid(); + sim.toggleGrid(); } } diff --git a/lib/components/simulation_component.dart b/lib/components/simulation_component.dart index 91812fb..1fcef71 100644 --- a/lib/components/simulation_component.dart +++ b/lib/components/simulation_component.dart @@ -2,6 +2,7 @@ import 'dart:html' as html; import 'package:angular/angular.dart'; import 'package:rules_of_living/service/engine_service.dart'; +import 'package:rules_of_living/service/simulation_service.dart'; @Component( selector: 'gol-simulation', @@ -11,8 +12,9 @@ import 'package:rules_of_living/service/engine_service.dart'; ) class SimulationComponent implements OnInit { final EngineService engine; + final SimulationService sim; - SimulationComponent(this.engine); + SimulationComponent(this.engine, this.sim); @override void ngOnInit() { @@ -29,6 +31,6 @@ class SimulationComponent implements OnInit { the canvas did not load correctly :( ''', canvas.width / 2 - 50, canvas.height / 2); - engine.canvas = canvas; + sim.canvas = canvas; } } diff --git a/lib/service/engine_service.dart b/lib/service/engine_service.dart index 6d40681..a76fedb 100644 --- a/lib/service/engine_service.dart +++ b/lib/service/engine_service.dart @@ -1,5 +1,3 @@ -import 'dart:html' as html; - import 'package:rules_of_living/src/Engine.dart'; class EngineService { @@ -43,14 +41,6 @@ class EngineService { int get simSpeed => engine.stepsPerSecond; void set simSpeed(int val) => engine.stepsPerSecond = val; - //TODO split into RenderService when rendering is decoupled from engine. - html.CanvasElement get canvas => engine.canvas; - void set canvas(html.CanvasElement canvas) => engine.canvas = canvas; - - void toggleGrid() { - engine.toggleEdgeRendering(); - } - void reset() { engine.reset(); } diff --git a/lib/service/simulation_service.dart b/lib/service/simulation_service.dart index 0c967ef..b5a9021 100644 --- a/lib/service/simulation_service.dart +++ b/lib/service/simulation_service.dart @@ -1,3 +1,4 @@ +import 'dart:html' as html; import 'dart:math'; import 'package:rules_of_living/service/engine_service.dart'; @@ -24,4 +25,12 @@ class SimulationService { } 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(); + } } From 32a3676d95aaef417342936ffdc6302ffd1d4464 Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 18 Oct 2018 15:19:54 +0200 Subject: [PATCH 09/10] Remove deprecated strict mode --- analysis_options.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/analysis_options.yaml b/analysis_options.yaml index caef4c8..78fb282 100644 --- a/analysis_options.yaml +++ b/analysis_options.yaml @@ -1,6 +1,6 @@ analyzer: exclude: [build/**] - strong-mode: true + errors: uri_has_not_been_generated: ignore plugins: From 8db9cd6ff1c363b21913b468ffa83be14db79055 Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 18 Oct 2018 15:21:50 +0200 Subject: [PATCH 10/10] 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); - }); - }); }