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

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
@ -33,14 +52,14 @@ class Game {
ctx.clearRect(0, 0, canvas.width, canvas.height);
_grid_foreach((x, y) {
Color col = _getOscillatedCol(grid[y][x]);
ctx.setFillColorRgb(col.r, col.g, col.b);
ctx.fillRect(x*brickW, y*brickH, brickW, brickH);
Color col = _getOscillatedCol(grid[y][x]);
ctx.setFillColorRgb(col.r, col.g, col.b);
ctx.fillRect(x * brickW, y * brickH, brickW, brickH);
});
// 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();
}
@ -81,14 +100,18 @@ class Game {
// true makes squares larger
// false makes squares smaller
void changeGridSize(bool larger) {
if(larger) {
if(grid.length <= 5) return;
if (larger) {
if (grid.length <= 5) return;
this.grid = _buildGrid(grid.length - 5, Color(255, 255, 255));
} else {
if(grid.length >= 60) return;
if (grid.length >= 60) return;
this.grid = _buildGrid(grid.length + 5, Color(255, 255, 255));
}
}
void toggleRandomOrder() {
isRandom = !isRandom;
}
}
class Color {