diff --git a/lib/src/Engine.dart b/lib/src/Engine.dart index 601e5ac..539e6d2 100644 --- a/lib/src/Engine.dart +++ b/lib/src/Engine.dart @@ -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); } diff --git a/test/src/engine_test.dart b/test/src/engine_test.dart index 72f8b0d..0712ba8 100644 --- a/test/src/engine_test.dart +++ b/test/src/engine_test.dart @@ -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); + }); } \ No newline at end of file