Allow Switching between Random and Ordered Rectangle Updates

This commit is contained in:
Marty Oehme 2018-07-24 13:29:47 +02:00
parent f7ee7e3acb
commit 994a008482
1 changed files with 31 additions and 8 deletions

View File

@ -13,6 +13,9 @@ class Game {
bool _oscillDir = true; // oscillate upwards (true) or downwards (false)
double _OSCILLSPEED = 1.0;
bool _fwd = true;
Point _curPos = Point(-1, 0);
bool isRandom = false;
Game(CanvasElement this.canvas) {
grid = _buildGrid(5, new Color(255, 255, 255));
@ -20,8 +23,24 @@ class Game {
// In-World Logic Updates
void update([num dt]) {
Point next = isRandom ? nextPosRandom() : nextPosOrdered(_curPos);
print(_curPos.toString());
grid[next.y][next.x] =
new Color(rng.nextInt(255), rng.nextInt(255), rng.nextInt(255));
_curPos = next;
}
Point nextPosOrdered(Point curPos) {
Point pos = Point(curPos.x, curPos.y);
if (pos.x >= grid[pos.y].length - 1) pos = Point(-1, pos.y + 1);
if (pos.y >= grid.length) pos = Point(-1, 0);
pos = Point(pos.x + 1, pos.y);
return pos;
}
Point nextPosRandom() {
int ry = rng.nextInt(grid.length);
grid[ry][rng.nextInt(grid[ry].length)] = new Color(rng.nextInt(255), rng.nextInt(255), rng.nextInt(255));
return Point(rng.nextInt(grid[ry].length), ry);
}
// Render Pipeline
@ -40,7 +59,7 @@ class Game {
// This should usually be place in update()
// Placed here to highlight render speed changes for some examples
// See dirty flag Loop for details
// See variable timestep & dirty flag Loops for details
_oscillate();
}
@ -89,6 +108,10 @@ class Game {
this.grid = _buildGrid(grid.length + 5, Color(255, 255, 255));
}
}
void toggleRandomOrder() {
isRandom = !isRandom;
}
}
class Color {