Allow setting a canvas for an engine at any point
This commit is contained in:
parent
4c6dff35c3
commit
66bf87d9d8
2 changed files with 33 additions and 3 deletions
|
@ -22,6 +22,11 @@ class Engine {
|
|||
num _updateLag = 0.0;
|
||||
num _drawLag = 0.0;
|
||||
|
||||
/// 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.
|
||||
html.CanvasElement canvas;
|
||||
Grid _grid = new Grid(GRID_X, GRID_Y);
|
||||
bool running = false;
|
||||
|
@ -68,15 +73,30 @@ class Engine {
|
|||
}
|
||||
}
|
||||
|
||||
/// 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().
|
||||
void update() {
|
||||
if (!_grid.update()) running = false;
|
||||
}
|
||||
|
||||
/// 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).
|
||||
void step() {
|
||||
running = false;
|
||||
_grid.update();
|
||||
}
|
||||
|
||||
/// 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.
|
||||
void render([num interp]) {
|
||||
if(canvas != null) _grid.render(canvas, interp);
|
||||
}
|
||||
|
|
|
@ -1,22 +1,32 @@
|
|||
import 'dart:html' as html;
|
||||
|
||||
@TestOn('browser')
|
||||
|
||||
import 'package:rules_of_living/src/Engine.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
void main() {
|
||||
Engine sut;
|
||||
setUp(() {
|
||||
sut = Engine();
|
||||
});
|
||||
|
||||
test("Engine can be instantiated without canvas", () {
|
||||
expect(Engine(), isNot(throwsNoSuchMethodError));
|
||||
expect(sut, isNot(throwsNoSuchMethodError));
|
||||
});
|
||||
|
||||
test("Engine does not throw errors when calling render directly", () {
|
||||
Engine sut = new Engine();
|
||||
// anonymous function necessary since throws can not use functions with args
|
||||
expect(() => sut.render(), isNot(throwsNoSuchMethodError));
|
||||
});
|
||||
|
||||
test("Engine does not throw errors when processing without attached canvas", () {
|
||||
Engine sut = new Engine();
|
||||
// anonymous function necessary since throws can not use functions with args
|
||||
expect(() => sut.process(1000), isNot(throwsNoSuchMethodError));
|
||||
});
|
||||
|
||||
test("setCanvas allows setting a canvas for an engine at any point", () {
|
||||
sut.canvas = new html.CanvasElement();
|
||||
expect(sut.canvas, isNotNull);
|
||||
});
|
||||
}
|
Loading…
Reference in a new issue