Split ConfigurationService to use SimulationService

Methods concerning engine make use of EngineService, those concerning grid and patterns make use of SimulationService.
This commit is contained in:
Unknown 2018-10-18 12:16:47 +02:00
parent 6b4786fdd0
commit 7729da3a40
3 changed files with 26 additions and 15 deletions

View file

@ -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<int> get gridSize => _es.engine.gridSize;
Point<int> get gridSize => _sim.gridSize;
}

View file

@ -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<int> get gridSize => _engine.engine.gridSize;
}

View file

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