Move Simulation Accesses to Simulation Class
Everything has been refactored away from engine, which now only controls updating & rendering within a specific timestep. (As well as stepping forward by calling a single update) Everything regarding grids, patterns and cells has been moved into the simulation and the Services have been updated to reflect that.
This commit is contained in:
parent
32a3676d95
commit
8db9cd6ff1
5 changed files with 33 additions and 63 deletions
|
@ -1,4 +1,5 @@
|
||||||
import 'package:rules_of_living/src/Engine.dart';
|
import 'package:rules_of_living/src/Engine.dart';
|
||||||
|
import 'package:rules_of_living/src/Simulation.dart';
|
||||||
|
|
||||||
class EngineService {
|
class EngineService {
|
||||||
Engine _uncachedEngineAccess;
|
Engine _uncachedEngineAccess;
|
||||||
|
@ -41,18 +42,7 @@ class EngineService {
|
||||||
int get simSpeed => engine.stepsPerSecond;
|
int get simSpeed => engine.stepsPerSecond;
|
||||||
void set simSpeed(int val) => engine.stepsPerSecond = val;
|
void set simSpeed(int val) => engine.stepsPerSecond = val;
|
||||||
|
|
||||||
void reset() {
|
|
||||||
engine.reset();
|
|
||||||
}
|
|
||||||
|
|
||||||
void addRandomPattern() {
|
|
||||||
engine.running = false;
|
|
||||||
engine.addPattern();
|
|
||||||
}
|
|
||||||
|
|
||||||
void clear() {
|
|
||||||
engine.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool get isRunning => engine.running;
|
bool get isRunning => engine.running;
|
||||||
|
|
||||||
|
void set simulation(Simulation value) => engine.simulation = value;
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,35 +2,42 @@ import 'dart:html' as html;
|
||||||
import 'dart:math';
|
import 'dart:math';
|
||||||
|
|
||||||
import 'package:rules_of_living/service/engine_service.dart';
|
import 'package:rules_of_living/service/engine_service.dart';
|
||||||
|
import 'package:rules_of_living/src/Simulation.dart';
|
||||||
|
|
||||||
class SimulationService {
|
class SimulationService {
|
||||||
final EngineService _engine;
|
// DEFAULT VALUES
|
||||||
|
static final int DEFAULT_GRID_SIZE = 50;
|
||||||
|
|
||||||
SimulationService(this._engine);
|
final EngineService _engine;
|
||||||
|
final Simulation _sim = Simulation(DEFAULT_GRID_SIZE, DEFAULT_GRID_SIZE);
|
||||||
|
|
||||||
|
SimulationService(this._engine) {
|
||||||
|
_engine.simulation = _sim;
|
||||||
|
_sim.addRandomPattern(amount: 15, dispersal: 5);
|
||||||
|
}
|
||||||
|
|
||||||
void reset() {
|
void reset() {
|
||||||
_engine.reset();
|
_sim.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
void addRandomPattern() {
|
void addRandomPattern() {
|
||||||
_engine.addRandomPattern();
|
_sim.addRandomPattern();
|
||||||
}
|
}
|
||||||
|
|
||||||
void clear() {
|
void clear() {
|
||||||
_engine.clear();
|
_sim.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Point<int> get gridSize => _sim.gridSize;
|
||||||
void set gridSize(Point<int> size) {
|
void set gridSize(Point<int> size) {
|
||||||
_engine.engine.gridSize = size;
|
_sim.gridSize = size;
|
||||||
}
|
}
|
||||||
|
|
||||||
Point<int> get gridSize => _engine.engine.gridSize;
|
|
||||||
|
|
||||||
//TODO split into RenderService when rendering is decoupled from engine.
|
//TODO split into RenderService when rendering is decoupled from engine.
|
||||||
html.CanvasElement get canvas => _engine.engine.canvas;
|
html.CanvasElement get canvas => _engine.engine.canvas;
|
||||||
void set canvas(html.CanvasElement canvas) => _engine.engine.canvas = canvas;
|
void set canvas(html.CanvasElement canvas) => _engine.engine.canvas = canvas;
|
||||||
|
|
||||||
void toggleGrid() {
|
void toggleGrid() {
|
||||||
_engine.engine.toggleEdgeRendering();
|
_sim.renderEdges = !_sim.renderEdges;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,16 +29,6 @@ class Engine {
|
||||||
// ms stuck in updateloop after which game will declare itself unresponsive
|
// ms stuck in updateloop after which game will declare itself unresponsive
|
||||||
final int SAFETY_TIMEOUT = 2000;
|
final int SAFETY_TIMEOUT = 2000;
|
||||||
|
|
||||||
/// Grid Size
|
|
||||||
///
|
|
||||||
/// Number of cells on x coordinate and y coordinate. Can be set individually.
|
|
||||||
Point get gridSize => Point<int>(_simulation.w, _simulation.h);
|
|
||||||
void set gridSize(Point<int> value) {
|
|
||||||
if (value.x <= 0 || value.y <= 0)
|
|
||||||
throw ArgumentError("grid size must not be smaller than 1");
|
|
||||||
_simulation = Simulation(value.x, value.y);
|
|
||||||
}
|
|
||||||
|
|
||||||
num _updateLag = 0.0;
|
num _updateLag = 0.0;
|
||||||
num _drawLag = 0.0;
|
num _drawLag = 0.0;
|
||||||
|
|
||||||
|
@ -51,11 +41,8 @@ class Engine {
|
||||||
Simulation _simulation;
|
Simulation _simulation;
|
||||||
bool running = false;
|
bool running = false;
|
||||||
|
|
||||||
Engine([x = 100, y = 100, this.canvas]) {
|
Engine() {
|
||||||
_simulation = Simulation(x, y);
|
|
||||||
|
|
||||||
_elapsed.start();
|
_elapsed.start();
|
||||||
_simulation.addRandomPattern(amount: 15, dispersal: 5);
|
|
||||||
html.window.animationFrame.then(animFrame);
|
html.window.animationFrame.then(animFrame);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,16 +51,6 @@ class Engine {
|
||||||
html.window.animationFrame.then(animFrame);
|
html.window.animationFrame.then(animFrame);
|
||||||
}
|
}
|
||||||
|
|
||||||
void reset() {
|
|
||||||
_simulation.reset();
|
|
||||||
running = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void clear() {
|
|
||||||
_simulation = new Simulation(gridSize.x, gridSize.y);
|
|
||||||
running = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void process(num now) {
|
void process(num now) {
|
||||||
_drawLag += _elapsed.elapsedMilliseconds;
|
_drawLag += _elapsed.elapsedMilliseconds;
|
||||||
_updateLag += _elapsed.elapsedMilliseconds;
|
_updateLag += _elapsed.elapsedMilliseconds;
|
||||||
|
@ -101,6 +78,8 @@ class Engine {
|
||||||
/// directly, since it is automatically taken care of by the processing function.
|
/// directly, since it is automatically taken care of by the processing function.
|
||||||
/// If simulation should be advanced manually one time, prefer using step().
|
/// If simulation should be advanced manually one time, prefer using step().
|
||||||
void update() {
|
void update() {
|
||||||
|
if (_simulation == null) return;
|
||||||
|
|
||||||
Map<int, bool> simulationUpdate = _simulation.update();
|
Map<int, bool> simulationUpdate = _simulation.update();
|
||||||
_simulation.mergeStateChanges(simulationUpdate);
|
_simulation.mergeStateChanges(simulationUpdate);
|
||||||
|
|
||||||
|
@ -123,16 +102,10 @@ class Engine {
|
||||||
/// the internal engine processing. Does not do anything if no canvas is
|
/// the internal engine processing. Does not do anything if no canvas is
|
||||||
/// defined.
|
/// defined.
|
||||||
void render([num interp]) {
|
void render([num interp]) {
|
||||||
if (canvas == null) return;
|
if (canvas == null || _simulation == null) return;
|
||||||
|
|
||||||
_simulation.render(canvas, interp);
|
_simulation.render(canvas, interp);
|
||||||
}
|
}
|
||||||
|
|
||||||
void addPattern({int amount, int dispersal}) {
|
void set simulation(Simulation value) => _simulation = value;
|
||||||
_simulation.addRandomPattern(amount: amount, dispersal: dispersal);
|
|
||||||
}
|
|
||||||
|
|
||||||
void toggleEdgeRendering() {
|
|
||||||
_simulation.renderEdges = !_simulation.renderEdges;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import 'dart:html' as html;
|
import 'dart:html' as html;
|
||||||
import 'dart:math' as math;
|
import 'dart:math' as math;
|
||||||
|
import 'dart:math';
|
||||||
|
|
||||||
import 'package:rules_of_living/src/Grid.dart';
|
import 'package:rules_of_living/src/Grid.dart';
|
||||||
import 'package:rules_of_living/src/rules/GameOfLife.dart';
|
import 'package:rules_of_living/src/rules/GameOfLife.dart';
|
||||||
|
@ -8,7 +9,7 @@ import 'package:rules_of_living/src/rules/RuleSet.dart';
|
||||||
enum CellPattern { SpaceShip, Blinker }
|
enum CellPattern { SpaceShip, Blinker }
|
||||||
|
|
||||||
class Simulation {
|
class Simulation {
|
||||||
final Grid<bool> map;
|
Grid<bool> map;
|
||||||
RuleSet rules = GameOfLife();
|
RuleSet rules = GameOfLife();
|
||||||
|
|
||||||
bool _dirty = true;
|
bool _dirty = true;
|
||||||
|
@ -20,6 +21,13 @@ class Simulation {
|
||||||
int get w => map.width;
|
int get w => map.width;
|
||||||
int get h => map.height;
|
int get h => map.height;
|
||||||
|
|
||||||
|
Point get gridSize => Point<int>(w, h);
|
||||||
|
void set gridSize(Point<int> value) {
|
||||||
|
if (value.x <= 0 || value.y <= 0)
|
||||||
|
throw ArgumentError("grid size must not be smaller than 1");
|
||||||
|
map = Grid(value.x, value.y);
|
||||||
|
}
|
||||||
|
|
||||||
Simulation(int w, int h) : this.map = new Grid(w, h) {
|
Simulation(int w, int h) : this.map = new Grid(w, h) {
|
||||||
reset();
|
reset();
|
||||||
print("Grid Created");
|
print("Grid Created");
|
||||||
|
|
|
@ -31,12 +31,4 @@ void main() {
|
||||||
expect(sut.canvas, isNotNull);
|
expect(sut.canvas, isNotNull);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
group("gridSize", () {
|
|
||||||
test("zero gridSizes throw ArgumentErrors", () {
|
|
||||||
expect(() => sut.gridSize = Point(0, 5), throwsArgumentError);
|
|
||||||
});
|
|
||||||
test("negative gridSizes throw ArgumentErrors", () {
|
|
||||||
expect(() => sut.gridSize = Point(1, -5), throwsArgumentError);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue