2018-07-05 15:59:11 +00:00
|
|
|
import 'dart:html' as html;
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
// Game Tick Rate - *does* impact game speed
|
2018-07-06 14:47:20 +00:00
|
|
|
int _MS_PER_STEP = 1000 ~/ 3;
|
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
|
|
|
|
|
|
|
num _updateLag = 0.0;
|
|
|
|
num _drawLag = 0.0;
|
|
|
|
|
|
|
|
final html.CanvasElement canvas;
|
2018-07-07 18:49:02 +00:00
|
|
|
Grid grid = new Grid(100, 100);
|
2018-07-09 15:00:53 +00:00
|
|
|
bool _running = false;
|
2018-07-05 15:59:11 +00:00
|
|
|
|
2018-07-09 13:16:28 +00:00
|
|
|
Engine(this.canvas) {
|
2018-07-06 13:00:01 +00:00
|
|
|
_elapsed.start();
|
2018-07-07 19:43:24 +00:00
|
|
|
grid.addPattern(amount: 15, dispersal: 5);
|
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-07 19:43:24 +00:00
|
|
|
grid.reset();
|
2018-07-07 17:07:30 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 17:45:04 +00:00
|
|
|
void clear() {
|
|
|
|
grid = new Grid(100, 100);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void update() {
|
2018-07-06 14:47:20 +00:00
|
|
|
// print("updating");
|
2018-07-08 17:32:43 +00:00
|
|
|
if(!grid.update()) running = false;
|
2018-07-05 15:59:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void render([num interp]) {
|
2018-07-06 14:47:20 +00:00
|
|
|
// print("rendering");
|
|
|
|
grid.render(canvas, interp);
|
2018-07-05 15:59:11 +00:00
|
|
|
}
|
2018-07-09 15:00:53 +00:00
|
|
|
|
|
|
|
void set running(bool on) => _running = on;
|
|
|
|
bool get running => _running;
|
2018-07-07 18:49:02 +00:00
|
|
|
}
|