Separate Engine Service Functionality from Component
This commit is contained in:
parent
842cbeca1f
commit
976ec8097d
4 changed files with 47 additions and 23 deletions
|
@ -1,8 +1,6 @@
|
||||||
import 'package:angular/angular.dart';
|
import 'package:angular/angular.dart';
|
||||||
import 'package:angular_components/angular_components.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/service/engine_service.dart';
|
||||||
import 'package:rules_of_living/src/Engine.dart';
|
|
||||||
|
|
||||||
@Component(
|
@Component(
|
||||||
selector: 'sim-controls',
|
selector: 'sim-controls',
|
||||||
|
@ -17,13 +15,12 @@ import 'package:rules_of_living/src/Engine.dart';
|
||||||
styleUrls: const ["controls_component.css"],
|
styleUrls: const ["controls_component.css"],
|
||||||
)
|
)
|
||||||
class ControlsComponent {
|
class ControlsComponent {
|
||||||
final EngineService engineService;
|
final EngineService engine;
|
||||||
|
|
||||||
Engine get engine => engineService.engine;
|
ControlsComponent(this.engine);
|
||||||
ControlsComponent(this.engineService);
|
|
||||||
|
|
||||||
void onStartClicked() {
|
void onStartClicked() {
|
||||||
engine.running = !engine.running;
|
engine.toggleRunning();
|
||||||
}
|
}
|
||||||
|
|
||||||
void onStepClicked() {
|
void onStepClicked() {
|
||||||
|
@ -35,8 +32,7 @@ class ControlsComponent {
|
||||||
}
|
}
|
||||||
|
|
||||||
void onRandomClicked() {
|
void onRandomClicked() {
|
||||||
engine.running = false;
|
engine.addRandomPattern();
|
||||||
engine.addPattern();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void onClearClicked() {
|
void onClearClicked() {
|
||||||
|
|
|
@ -8,7 +8,6 @@ import 'package:rules_of_living/service/engine_service.dart';
|
||||||
templateUrl: "simulation_component.html",
|
templateUrl: "simulation_component.html",
|
||||||
directives: [coreDirectives],
|
directives: [coreDirectives],
|
||||||
providers: [],
|
providers: [],
|
||||||
// styleUrls: const ['package:angular_components/app_layout/layout.scss.css'],
|
|
||||||
)
|
)
|
||||||
class SimulationComponent implements OnInit {
|
class SimulationComponent implements OnInit {
|
||||||
final EngineService engineService;
|
final EngineService engineService;
|
||||||
|
@ -29,13 +28,6 @@ class SimulationComponent implements OnInit {
|
||||||
If you see this\n
|
If you see this\n
|
||||||
the app is broken :(
|
the app is broken :(
|
||||||
''', canvas.width / 2 - 50, canvas.height / 2);
|
''', canvas.width / 2 - 50, canvas.height / 2);
|
||||||
engineService.create(canvas);
|
// engineService.create(canvas);
|
||||||
|
|
||||||
html.window.animationFrame.then(animFrame);
|
|
||||||
}
|
|
||||||
|
|
||||||
void animFrame(num now) {
|
|
||||||
engineService.engine.process(now);
|
|
||||||
html.window.animationFrame.then(animFrame);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,40 @@ import 'package:rules_of_living/src/Engine.dart';
|
||||||
class EngineService {
|
class EngineService {
|
||||||
Engine _engine;
|
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);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,11 +24,17 @@ class Engine {
|
||||||
|
|
||||||
final html.CanvasElement canvas;
|
final html.CanvasElement canvas;
|
||||||
Grid _grid = new Grid(GRID_X, GRID_Y);
|
Grid _grid = new Grid(GRID_X, GRID_Y);
|
||||||
bool _running = false;
|
bool running = false;
|
||||||
|
|
||||||
Engine([this.canvas]) {
|
Engine([this.canvas]) {
|
||||||
_elapsed.start();
|
_elapsed.start();
|
||||||
_grid.addPattern(amount: 15, dispersal: 5);
|
_grid.addPattern(amount: 15, dispersal: 5);
|
||||||
|
html.window.animationFrame.then(animFrame);
|
||||||
|
}
|
||||||
|
|
||||||
|
void animFrame(num now) {
|
||||||
|
process(now);
|
||||||
|
html.window.animationFrame.then(animFrame);
|
||||||
}
|
}
|
||||||
|
|
||||||
void reset() {
|
void reset() {
|
||||||
|
@ -95,7 +101,4 @@ class Engine {
|
||||||
void toggleEdgeRendering() {
|
void toggleEdgeRendering() {
|
||||||
_grid.renderEdges = !_grid.renderEdges;
|
_grid.renderEdges = !_grid.renderEdges;
|
||||||
}
|
}
|
||||||
|
|
||||||
void set running(bool on) => _running = on;
|
|
||||||
bool get running => _running;
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue