From 976ec8097da4de1fb302088100de7689c53e7bea Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Sat, 25 Aug 2018 09:33:15 +0200 Subject: [PATCH] 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; }