From 76b0e68cf1878a376c0eefdccdad652d6d37e9e6 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Fri, 20 Jul 2018 19:51:13 +0200 Subject: [PATCH] Examples - Add Grid Size Up/Down Buttons --- lib/game/Game.dart | 38 +++++++++++++++++++++++++++++--------- web/main.dart | 14 +++++++++++++- 2 files changed, 42 insertions(+), 10 deletions(-) diff --git a/lib/game/Game.dart b/lib/game/Game.dart index 43f13f0..4ff1b7f 100644 --- a/lib/game/Game.dart +++ b/lib/game/Game.dart @@ -11,11 +11,11 @@ class Game { double _oscill = 0.1; bool _oscillDir = true; // oscillate upwards (true) or downwards (false) - double _OSCILLSPEED = 0.1; + double _OSCILLSPEED = 1.0; bool _fwd = true; Game(CanvasElement this.canvas) { - grid = _buildGrid(5, 5, new Color(255, 0, 255)); + grid = _buildGrid(5, new Color(255, 255, 255)); } // In-World Logic Updates @@ -33,20 +33,27 @@ class Game { ctx.clearRect(0, 0, canvas.width, canvas.height); _grid_foreach((x, y) { - Color col = _oscillate(grid[y][x]); + 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 + _oscillate(); } // Slightly oscillating colors to highlight rendering updates - Color _oscillate(Color col) { + void _oscillate() { if(_oscill >= 50.0 || _oscill <= -50.0) { _oscillDir = !_oscillDir; _oscill = max( min(_oscill, 49.0), -49.0); } else _oscillDir == true ? _oscill += _OSCILLSPEED : _oscill -= _OSCILLSPEED; + } + Color _getOscillatedCol(Color col) { int o = _oscill.toInt(); return new Color(col.r + o, col.g + o, col.b + o); } @@ -59,16 +66,29 @@ class Game { } } - List> _buildGrid(int w, int h, Color col) { - List> grid = new List(h); - for (int y = 0; y < h; y++) { - grid[y] = new List(w); - for (int x = 0; x < w; x++) { + List> _buildGrid(int size, Color col) { + List> grid = new List(size); + for (int y = 0; y < size; y++) { + grid[y] = new List(size); + for (int x = 0; x < size; x++) { grid[y][x] = col; } } return grid; } + + // Grid Size Button Implementation + // true makes squares larger + // false makes squares smaller + void changeGridSize(bool larger) { + if(larger) { + if(grid.length <= 5) return; + this.grid = _buildGrid(grid.length - 5, Color(255, 255, 255)); + } else { + if(grid.length >= 60) return; + this.grid = _buildGrid(grid.length + 5, Color(255, 255, 255)); + } + } } class Color { diff --git a/web/main.dart b/web/main.dart index c920a80..ecde8b5 100644 --- a/web/main.dart +++ b/web/main.dart @@ -112,7 +112,19 @@ void addControls(Example ex) { ..text = "Stop" ..onClick.listen((e) { ex.loop.stop(); - }))); + })) + ..append(new ButtonElement() + ..text = "+" + ..onClick.listen((e) { + ex.loop.game.changeGridSize(true); + })) + ..append(new ButtonElement() + ..text = "-" + ..onClick.listen((e) { + ex.loop.game.changeGridSize(false); + })) + ); + // Don't add controls which don't work anyways (for simple examples) if(examples.indexOf(ex) <= 1) return;