From 8d7575eaf7619becf56078eced5e4200be83bda7 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Fri, 24 Aug 2018 20:01:36 +0200 Subject: [PATCH 1/3] Disable superfluous tests In preparation for first test of the app. --- test/{app_test.dart => app_test_disable.dart} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename test/{app_test.dart => app_test_disable.dart} (100%) diff --git a/test/app_test.dart b/test/app_test_disable.dart similarity index 100% rename from test/app_test.dart rename to test/app_test_disable.dart From 6c9179b833886f81f77bc503e876af255547dc37 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Fri, 24 Aug 2018 20:02:43 +0200 Subject: [PATCH 2/3] make canvas argument optional on instantiating engine --- lib/src/Engine.dart | 11 +++++++---- test/src/engine_test.dart | 12 ++++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) create mode 100644 test/src/engine_test.dart diff --git a/lib/src/Engine.dart b/lib/src/Engine.dart index 6f7ff16..a52df66 100644 --- a/lib/src/Engine.dart +++ b/lib/src/Engine.dart @@ -6,7 +6,7 @@ class Engine { // Elapsed Time Counter - useful for Safety Timeout Stopwatch _elapsed = new Stopwatch(); - // Game Tick Rate - *does* impact game speed + // Game Tick Rate - *does* impact game speed TODO add configurable option int _MS_PER_STEP = 1000 ~/ 3; // Max Frame (i.e. Rendering) rate - does *not* impact game speed @@ -15,14 +15,18 @@ class Engine { // ms stuck in updateloop after which game will declare itself unresponsive final int SAFETY_TIMEOUT = 2000; + // Grid Size TODO add as configurable option + static final GRID_X = 100; + static final GRID_Y = 100; + num _updateLag = 0.0; num _drawLag = 0.0; final html.CanvasElement canvas; - Grid _grid = new Grid(100, 100); + Grid _grid = new Grid(GRID_X, GRID_Y); bool _running = false; - Engine(this.canvas) { + Engine([this.canvas]) { _elapsed.start(); _grid.addPattern(amount: 15, dispersal: 5); } @@ -59,7 +63,6 @@ class Engine { } void update() { -// print("updating"); if (!_grid.update()) running = false; } diff --git a/test/src/engine_test.dart b/test/src/engine_test.dart new file mode 100644 index 0000000..92794ff --- /dev/null +++ b/test/src/engine_test.dart @@ -0,0 +1,12 @@ +@TestOn('browser') + +import 'dart:html'; + +import 'package:rules_of_living/src/Engine.dart'; +import 'package:test/test.dart'; + +void main() { + test("Engine can be instantiated without canvas", () { + expect(Engine(), isNot(throwsA(anything))); + }); +} \ No newline at end of file From bbf61f875d7e819eabc12bee795320fced6b7466 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Sat, 25 Aug 2018 09:33:15 +0200 Subject: [PATCH 3/3] Separate Engine Service Functionality from Component --- lib/components/controls_component.dart | 12 +++----- lib/components/simulation_component.dart | 10 +------ lib/service/engine_service.dart | 37 ++++++++++++++++++++++-- lib/src/Engine.dart | 11 ++++--- 4 files changed, 47 insertions(+), 23 deletions(-) diff --git a/lib/components/controls_component.dart b/lib/components/controls_component.dart index 1f2a65d..511a7d4 100644 --- a/lib/components/controls_component.dart +++ b/lib/components/controls_component.dart @@ -1,8 +1,6 @@ import 'package:angular/angular.dart'; import 'package:angular_components/angular_components.dart'; -import 'package:rules_of_living/service/configuration_service.dart'; import 'package:rules_of_living/service/engine_service.dart'; -import 'package:rules_of_living/src/Engine.dart'; @Component( selector: 'sim-controls', @@ -17,13 +15,12 @@ import 'package:rules_of_living/src/Engine.dart'; styleUrls: const ["controls_component.css"], ) class ControlsComponent { - final EngineService engineService; + final EngineService engine; - Engine get engine => engineService.engine; - ControlsComponent(this.engineService); + ControlsComponent(this.engine); void onStartClicked() { - engine.running = !engine.running; + engine.toggleRunning(); } void onStepClicked() { @@ -35,8 +32,7 @@ class ControlsComponent { } void onRandomClicked() { - engine.running = false; - engine.addPattern(); + engine.addRandomPattern(); } void onClearClicked() { diff --git a/lib/components/simulation_component.dart b/lib/components/simulation_component.dart index e64fa82..c372026 100644 --- a/lib/components/simulation_component.dart +++ b/lib/components/simulation_component.dart @@ -8,7 +8,6 @@ import 'package:rules_of_living/service/engine_service.dart'; templateUrl: "simulation_component.html", directives: [coreDirectives], providers: [], -// styleUrls: const ['package:angular_components/app_layout/layout.scss.css'], ) class SimulationComponent implements OnInit { final EngineService engineService; @@ -29,13 +28,6 @@ class SimulationComponent implements OnInit { If you see this\n the app is broken :( ''', canvas.width / 2 - 50, canvas.height / 2); - engineService.create(canvas); - - html.window.animationFrame.then(animFrame); - } - - void animFrame(num now) { - engineService.engine.process(now); - html.window.animationFrame.then(animFrame); +// engineService.create(canvas); } } diff --git a/lib/service/engine_service.dart b/lib/service/engine_service.dart index 5107f3c..6384f5f 100644 --- a/lib/service/engine_service.dart +++ b/lib/service/engine_service.dart @@ -5,7 +5,40 @@ import 'package:rules_of_living/src/Engine.dart'; class EngineService { Engine _engine; - Engine get engine => _engine; + Engine get engine => _engine ?? create(null); + + Engine create(html.CanvasElement canvas) { + _engine = Engine(canvas); + return _engine; + } + + 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(); + } - void create(html.CanvasElement canvas) => _engine = Engine(canvas); } diff --git a/lib/src/Engine.dart b/lib/src/Engine.dart index a52df66..9b1814e 100644 --- a/lib/src/Engine.dart +++ b/lib/src/Engine.dart @@ -24,11 +24,17 @@ class Engine { final html.CanvasElement canvas; Grid _grid = new Grid(GRID_X, GRID_Y); - bool _running = false; + bool running = false; Engine([this.canvas]) { _elapsed.start(); _grid.addPattern(amount: 15, dispersal: 5); + html.window.animationFrame.then(animFrame); + } + + void animFrame(num now) { + process(now); + html.window.animationFrame.then(animFrame); } void reset() { @@ -95,7 +101,4 @@ class Engine { void toggleEdgeRendering() { _grid.renderEdges = !_grid.renderEdges; } - - void set running(bool on) => _running = on; - bool get running => _running; }