From 5b7552216dbfd0aaa73b83b14edc18c8785e75f5 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Fri, 20 Jul 2018 14:36:32 +0200 Subject: [PATCH] Game - Add Color Oscillation --- lib/game/Game.dart | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/lib/game/Game.dart b/lib/game/Game.dart index 7291f59..1c16c5d 100644 --- a/lib/game/Game.dart +++ b/lib/game/Game.dart @@ -1,21 +1,29 @@ import 'dart:html'; +import 'dart:math'; + typedef void gridIterator(int x, int y); class Game { List> grid; CanvasElement canvas; CanvasRenderingContext2D ctx; + Random rng = new Random(); - double _oscill = 0.0; + double _oscill = 0.1; + bool _oscillDir = true; // oscillate upwards (true) or downwards (false) + double _OSCILLSPEED = 0.05; bool _fwd = true; Game(CanvasElement this.canvas) { - grid = buildGrid(5,5, new Color(255, 100, 255)); + grid = buildGrid(5, 5, new Color(255, 0, 255)); ctx = this.canvas.getContext('2d'); } - void update([num dt]) {} + void update([num dt]) { + int ry = rng.nextInt(grid.length); + grid[ry][rng.nextInt(grid[ry].length)] = new Color(rng.nextInt(255), rng.nextInt(255), rng.nextInt(255)); + } void draw([num interp]) { int brickW = (canvas.width ~/ grid[0].length); @@ -23,13 +31,22 @@ class Game { ctx.clearRect(0, 0, canvas.width, canvas.height); grid_foreach((x, y) { - Color col = grid[y][x]; - ctx.setFillColorRgb(col.r+_oscill, col.g+_oscill, col.b+_oscill); + Color col = _oscillate(grid[y][x]); + ctx.setFillColorRgb(col.r, col.g, col.b); ctx.fillRect(x*brickW, y*brickH, brickW, brickH); }); } - Color _oscillate(Color) {} + Color _oscillate(Color col) { + if(_oscill >= 50.0 || _oscill <= -50.0) { + _oscillDir = !_oscillDir; + _oscill = max( min(_oscill, 49.0), -49.0); + } + else _oscillDir == true ? _oscill += _OSCILLSPEED : _oscill -= _OSCILLSPEED; + + int o = _oscill.toInt(); + return new Color(col.r + o, col.g + o, col.b + o); + } void grid_foreach(gridIterator fun) { for (int y = 0; y < grid.length; y++) {