Separate Engine Service Functionality from Component

This commit is contained in:
Marty Oehme 2018-08-25 09:33:15 +02:00
parent 6c9179b833
commit bbf61f875d
4 changed files with 47 additions and 23 deletions

View file

@ -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() {

View file

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

View file

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

View file

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