2018-10-18 12:41:57 +00:00
|
|
|
import 'dart:html' as html;
|
2018-10-18 10:16:47 +00:00
|
|
|
import 'dart:math';
|
|
|
|
|
2018-10-18 10:05:02 +00:00
|
|
|
import 'package:rules_of_living/service/engine_service.dart';
|
2018-10-18 13:21:50 +00:00
|
|
|
import 'package:rules_of_living/src/Simulation.dart';
|
2018-10-18 10:05:02 +00:00
|
|
|
|
|
|
|
class SimulationService {
|
2018-10-18 13:21:50 +00:00
|
|
|
// DEFAULT VALUES
|
|
|
|
static final int DEFAULT_GRID_SIZE = 50;
|
|
|
|
|
2018-10-18 10:16:47 +00:00
|
|
|
final EngineService _engine;
|
2018-10-19 18:50:24 +00:00
|
|
|
final Simulation _sim;
|
2018-10-18 10:05:02 +00:00
|
|
|
|
2018-10-19 18:50:24 +00:00
|
|
|
SimulationService(this._engine, [Simulation sim])
|
|
|
|
: this._sim = sim ?? Simulation(DEFAULT_GRID_SIZE, DEFAULT_GRID_SIZE) {
|
2018-10-18 13:21:50 +00:00
|
|
|
_engine.simulation = _sim;
|
|
|
|
_sim.addRandomPattern(amount: 15, dispersal: 5);
|
|
|
|
}
|
2018-10-18 10:05:02 +00:00
|
|
|
|
|
|
|
void reset() {
|
2018-10-21 12:00:01 +00:00
|
|
|
_sim.clearMap();
|
2018-10-18 10:05:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void addRandomPattern() {
|
2018-10-18 13:21:50 +00:00
|
|
|
_sim.addRandomPattern();
|
2018-10-18 10:05:02 +00:00
|
|
|
}
|
|
|
|
|
2018-10-18 13:21:50 +00:00
|
|
|
Point<int> get gridSize => _sim.gridSize;
|
2018-10-18 10:29:46 +00:00
|
|
|
void set gridSize(Point<int> size) {
|
2018-10-18 13:21:50 +00:00
|
|
|
_sim.gridSize = size;
|
2018-10-18 10:05:02 +00:00
|
|
|
}
|
2018-10-18 10:16:47 +00:00
|
|
|
|
2018-10-18 12:41:57 +00:00
|
|
|
//TODO split into RenderService when rendering is decoupled from engine.
|
|
|
|
html.CanvasElement get canvas => _engine.engine.canvas;
|
|
|
|
void set canvas(html.CanvasElement canvas) => _engine.engine.canvas = canvas;
|
|
|
|
|
|
|
|
void toggleGrid() {
|
2018-10-18 13:21:50 +00:00
|
|
|
_sim.renderEdges = !_sim.renderEdges;
|
2018-10-18 12:41:57 +00:00
|
|
|
}
|
2018-10-19 18:50:24 +00:00
|
|
|
|
|
|
|
void save() => _sim.saveSnapshot();
|
|
|
|
void load() => _sim.loadSnapshot();
|
2018-10-18 10:05:02 +00:00
|
|
|
}
|