From 994a00848208d4dcf75ea9fd08978fae1757790c Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Tue, 24 Jul 2018 13:29:47 +0200 Subject: [PATCH] Allow Switching between Random and Ordered Rectangle Updates --- lib/game/Game.dart | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/lib/game/Game.dart b/lib/game/Game.dart index 4ff1b7f..489a7ea 100644 --- a/lib/game/Game.dart +++ b/lib/game/Game.dart @@ -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 {