Allow setting a canvas for an engine at any point

This commit is contained in:
Marty Oehme 2018-08-25 14:36:50 +02:00
parent 4c6dff35c3
commit 66bf87d9d8
2 changed files with 33 additions and 3 deletions

View file

@ -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);
}