From d6c6abf3f649c3d1475e18e45d902a8297f8c497 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Tue, 24 Jul 2018 14:16:45 +0200 Subject: [PATCH] Enable Sequential Coloring --- lib/game/Game.dart | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/lib/game/Game.dart b/lib/game/Game.dart index 48a5e70..ff879fd 100644 --- a/lib/game/Game.dart +++ b/lib/game/Game.dart @@ -1,5 +1,4 @@ import 'dart:html'; - import 'dart:math'; typedef void gridIterator(int x, int y); @@ -24,16 +23,18 @@ class Game { // In-World Logic Updates void update([num dt]) { Point next = isRandom ? nextPosRandom() : nextPosOrdered(_curPos); - grid[next.y][next.x] = - new Color(rng.nextInt(255), rng.nextInt(255), rng.nextInt(255)); + if (_curPos.x < 0) _curPos = Point(0, _curPos.y); + Color newColor = isRandom ? randomColor() : nextColor( + grid[_curPos.y][_curPos.x]); + grid[next.y][next.x] = newColor; _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); + if (pos.x >= grid[pos.y].length) pos = Point(0, pos.y + 1); + if (pos.y >= grid.length) pos = Point(0, 0); return pos; } @@ -42,6 +43,20 @@ class Game { return Point(rng.nextInt(grid[ry].length), ry); } + Color randomColor([int max = 255]) { + return new Color(rng.nextInt(max), rng.nextInt(max), rng.nextInt(max)); + } + + Color nextColor(Color col) { + if (col.r > 254 || col.g > 254 || col.b > 254) return randomColor(100); + if (col.r > col.g && col.r > col.b) + return Color(col.r + 1, col.g, col.b); + else if (col.b > col.r && col.b > col.g) + return Color(col.r, col.g, col.b + 1); + else + return Color(col.r, col.g + 1, col.b); + } + // Render Pipeline void draw([num interp]) { CanvasRenderingContext2D ctx = this.canvas.context2D; @@ -119,4 +134,9 @@ class Color { final int b; const Color(this.r, this.g, this.b); + + @override + String toString() { + return "Color: (r:$r, g:$g, b:$b)"; + } }