2018-07-05 15:59:11 +00:00
|
|
|
import 'dart:html' as html;
|
2018-08-25 15:23:06 +00:00
|
|
|
import 'dart:math';
|
2018-07-05 15:59:11 +00:00
|
|
|
|
2018-07-07 14:42:30 +00:00
|
|
|
import 'package:rules_of_living/src/Grid.dart';
|
2018-07-05 15:59:11 +00:00
|
|
|
|
2018-07-09 13:16:28 +00:00
|
|
|
class Engine {
|
2018-07-05 15:59:11 +00:00
|
|
|
// Elapsed Time Counter - useful for Safety Timeout
|
|
|
|
Stopwatch _elapsed = new Stopwatch();
|
|
|
|
|
2018-08-25 14:41:11 +00:00
|
|
|
/// Game Tick Rate
|
|
|
|
///
|
|
|
|
/// *does* impact game speed; dictates how long each logic step takes. Only
|
|
|
|
/// interesting for engine internal calculations, to set simulation speed
|
|
|
|
/// from the outside use stepsPerSecond instead.
|
2018-07-06 14:47:20 +00:00
|
|
|
int _MS_PER_STEP = 1000 ~/ 3;
|
2018-08-25 14:41:11 +00:00
|
|
|
|
|
|
|
/// Set Logic Updates per Second
|
|
|
|
///
|
|
|
|
/// Dictates simulation speed. Sets the amount of (logic) updates per second.
|
|
|
|
/// Translations between number of updates and their timings is not exact so
|
|
|
|
/// comparing this variable to fixed ints might not yield the expected results.
|
|
|
|
/// Does not affect render frequency, which is handled by framesPerSecond.
|
2018-08-25 13:51:17 +00:00
|
|
|
int get stepsPerSecond => 1000 ~/ _MS_PER_STEP;
|
|
|
|
set stepsPerSecond(int val) => _MS_PER_STEP = 1000 ~/ val;
|
2018-07-05 15:59:11 +00:00
|
|
|
|
|
|
|
// Max Frame (i.e. Rendering) rate - does *not* impact game speed
|
2018-07-06 14:47:20 +00:00
|
|
|
final int _MS_PER_FRAME = 1000 ~/ 30;
|
2018-07-05 15:59:11 +00:00
|
|
|
|
|
|
|
// ms stuck in updateloop after which game will declare itself unresponsive
|
2018-07-06 12:27:17 +00:00
|
|
|
final int SAFETY_TIMEOUT = 2000;
|
2018-07-05 15:59:11 +00:00
|
|
|
|
2018-08-25 15:23:06 +00:00
|
|
|
/// Grid Size
|
|
|
|
///
|
|
|
|
/// Number of cells on x coordinate and y coordinate. Can be set individually.
|
2018-08-27 18:18:19 +00:00
|
|
|
Point get gridSize => Point<int>(_grid.w, _grid.h);
|
|
|
|
void set gridSize(Point<int> value) {
|
|
|
|
if (value.x <= 0 || value.y <= 0)
|
|
|
|
throw ArgumentError("grid size must not be smaller than 1");
|
|
|
|
_grid = Grid(value.x, value.y);
|
|
|
|
}
|
2018-08-24 18:02:43 +00:00
|
|
|
|
2018-07-05 15:59:11 +00:00
|
|
|
num _updateLag = 0.0;
|
|
|
|
num _drawLag = 0.0;
|
|
|
|
|
2018-08-25 12:36:50 +00:00
|
|
|
/// The Canvas to Paint on
|
|
|
|
///
|
|
|
|
/// Manually define or change the canvas the engine should render to. Should
|
|
|
|
/// be used if no canvas was defined at engine creation and it should be
|
|
|
|
/// rendered later.
|
2018-08-25 12:10:56 +00:00
|
|
|
html.CanvasElement canvas;
|
2018-08-25 15:23:06 +00:00
|
|
|
Grid _grid;
|
2018-08-25 07:33:15 +00:00
|
|
|
bool running = false;
|
2018-07-05 15:59:11 +00:00
|
|
|
|
2018-08-27 18:13:19 +00:00
|
|
|
Engine([x = 100, y = 100, this.canvas]) {
|
|
|
|
_grid = Grid(x, y);
|
2018-08-25 15:23:06 +00:00
|
|
|
|
2018-07-06 13:00:01 +00:00
|
|
|
_elapsed.start();
|
2018-07-09 15:31:46 +00:00
|
|
|
_grid.addPattern(amount: 15, dispersal: 5);
|
2018-08-25 07:33:15 +00:00
|
|
|
html.window.animationFrame.then(animFrame);
|
|
|
|
}
|
|
|
|
|
|
|
|
void animFrame(num now) {
|
|
|
|
process(now);
|
|
|
|
html.window.animationFrame.then(animFrame);
|
2018-07-06 12:27:17 +00:00
|
|
|
}
|
2018-07-05 15:59:11 +00:00
|
|
|
|
2018-07-07 18:49:02 +00:00
|
|
|
void reset() {
|
2018-07-09 15:31:46 +00:00
|
|
|
_grid.reset();
|
2018-07-09 15:32:59 +00:00
|
|
|
running = false;
|
2018-07-07 17:07:30 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 17:45:04 +00:00
|
|
|
void clear() {
|
2018-08-25 15:23:06 +00:00
|
|
|
_grid = new Grid(gridSize.x, gridSize.y);
|
2018-07-08 17:45:04 +00:00
|
|
|
running = false;
|
|
|
|
}
|
|
|
|
|
2018-07-05 15:59:11 +00:00
|
|
|
void process(num now) {
|
2018-07-07 18:49:02 +00:00
|
|
|
_drawLag += _elapsed.elapsedMilliseconds;
|
2018-07-06 13:00:01 +00:00
|
|
|
_updateLag += _elapsed.elapsedMilliseconds;
|
2018-07-05 15:59:11 +00:00
|
|
|
_elapsed.reset();
|
|
|
|
|
|
|
|
while (_updateLag >= _MS_PER_STEP) {
|
|
|
|
if (_elapsed.elapsedMilliseconds > SAFETY_TIMEOUT) {
|
|
|
|
// TODO stub - give warning etc when this occurs
|
2018-07-06 12:27:17 +00:00
|
|
|
print("ERROR STUCK IN UPDATE LOOP");
|
2018-07-05 15:59:11 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-07-07 17:07:30 +00:00
|
|
|
if (running == true) update();
|
2018-07-05 15:59:11 +00:00
|
|
|
_updateLag -= _MS_PER_STEP;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_drawLag >= _MS_PER_FRAME) {
|
|
|
|
render(_updateLag / _MS_PER_STEP);
|
|
|
|
_drawLag = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-25 12:36:50 +00:00
|
|
|
/// Update Engine Logic
|
|
|
|
///
|
|
|
|
/// Updates the logic of the engine by one tick. Should usually not be called
|
|
|
|
/// directly, since it is automatically taken care of by the processing function.
|
|
|
|
/// If simulation should be advanced manually one time, prefer using step().
|
2018-07-05 15:59:11 +00:00
|
|
|
void update() {
|
2018-07-09 15:31:46 +00:00
|
|
|
if (!_grid.update()) running = false;
|
|
|
|
}
|
2018-07-09 15:27:56 +00:00
|
|
|
|
2018-08-25 12:36:50 +00:00
|
|
|
/// Advances Logic One Update
|
|
|
|
///
|
|
|
|
/// Moves logic of the engine one step forward and pauses the
|
|
|
|
/// simulation. Does not automatically re-render the new state
|
|
|
|
/// (though this should usually not pose a problem).
|
2018-07-09 15:27:56 +00:00
|
|
|
void step() {
|
|
|
|
running = false;
|
|
|
|
_grid.update();
|
2018-07-05 15:59:11 +00:00
|
|
|
}
|
|
|
|
|
2018-08-25 12:36:50 +00:00
|
|
|
/// Renders the Current Simulation State
|
|
|
|
///
|
|
|
|
/// Renders the simulation once. Will usually automatically be called by
|
|
|
|
/// the internal engine processing. Does not do anything if no canvas is
|
|
|
|
/// defined.
|
2018-07-05 15:59:11 +00:00
|
|
|
void render([num interp]) {
|
2018-08-27 18:13:19 +00:00
|
|
|
if (canvas != null) _grid.render(canvas, interp);
|
2018-07-09 15:31:46 +00:00
|
|
|
}
|
|
|
|
|
2018-07-09 15:32:35 +00:00
|
|
|
void addPattern(
|
2018-08-23 10:38:34 +00:00
|
|
|
{CellPattern pattern,
|
|
|
|
int x,
|
|
|
|
int y,
|
|
|
|
int amount,
|
|
|
|
int dispersal,
|
|
|
|
int seed}) {
|
2018-07-09 15:32:35 +00:00
|
|
|
_grid.addPattern(
|
|
|
|
pattern: pattern,
|
|
|
|
x: x,
|
|
|
|
y: y,
|
|
|
|
amount: amount,
|
|
|
|
dispersal: dispersal,
|
|
|
|
seed: seed);
|
|
|
|
}
|
|
|
|
|
2018-07-09 15:27:11 +00:00
|
|
|
void toggleEdgeRendering() {
|
|
|
|
_grid.renderEdges = !_grid.renderEdges;
|
2018-07-05 15:59:11 +00:00
|
|
|
}
|
2018-07-07 18:49:02 +00:00
|
|
|
}
|