2018-10-18 10:51:35 +00:00
|
|
|
import 'dart:html' as html;
|
|
|
|
|
2018-08-22 18:16:31 +00:00
|
|
|
import 'package:rules_of_living/src/Engine.dart';
|
|
|
|
|
|
|
|
class EngineService {
|
2018-08-27 18:50:12 +00:00
|
|
|
Engine _uncachedEngineAccess;
|
2018-08-25 07:33:15 +00:00
|
|
|
|
2018-10-18 10:51:35 +00:00
|
|
|
EngineService() {
|
|
|
|
simSpeed = 5;
|
|
|
|
}
|
|
|
|
|
2018-08-27 18:50:12 +00:00
|
|
|
Engine get engine => _uncachedEngineAccess ?? _setCachedAndReturn(Engine());
|
|
|
|
void set engine(Engine newEngine) {
|
|
|
|
_uncachedEngineAccess = newEngine;
|
2018-08-25 07:33:15 +00:00
|
|
|
}
|
|
|
|
|
2018-08-27 18:50:12 +00:00
|
|
|
Engine _setCachedAndReturn(Engine newEngine) {
|
|
|
|
engine = newEngine;
|
|
|
|
return newEngine;
|
2018-08-25 07:33:15 +00:00
|
|
|
}
|
2018-10-18 09:58:38 +00:00
|
|
|
|
|
|
|
void run() {
|
|
|
|
engine.running = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void stop() {
|
|
|
|
engine.running = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void toggleRunning() {
|
|
|
|
engine.running = !engine.running;
|
|
|
|
}
|
|
|
|
|
|
|
|
void step() {
|
|
|
|
engine.step();
|
|
|
|
}
|
|
|
|
|
2018-10-18 10:51:35 +00:00
|
|
|
/// Simulation Speed
|
|
|
|
///
|
|
|
|
/// Sets the number of updates the simulation takes per second. Can range from
|
|
|
|
/// 1 to arbitrarily high numbers (though setting it too high can potentially
|
|
|
|
/// make the app brittle).
|
|
|
|
int get simSpeed => engine.stepsPerSecond;
|
|
|
|
void set simSpeed(int val) => engine.stepsPerSecond = val;
|
|
|
|
|
|
|
|
//TODO split into RenderService when rendering is decoupled from engine.
|
|
|
|
html.CanvasElement get canvas => engine.canvas;
|
|
|
|
void set canvas(html.CanvasElement canvas) => engine.canvas = canvas;
|
|
|
|
|
|
|
|
void toggleGrid() {
|
|
|
|
engine.toggleEdgeRendering();
|
|
|
|
}
|
|
|
|
|
2018-10-18 09:58:38 +00:00
|
|
|
void reset() {
|
|
|
|
engine.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void addRandomPattern() {
|
|
|
|
engine.running = false;
|
|
|
|
engine.addPattern();
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear() {
|
|
|
|
engine.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool get isRunning => engine.running;
|
2018-08-22 18:16:31 +00:00
|
|
|
}
|