Enable Sequential Coloring

This commit is contained in:
Marty Oehme 2018-07-24 14:16:45 +02:00
parent 0384518b02
commit d6c6abf3f6

View file

@ -1,5 +1,4 @@
import 'dart:html'; import 'dart:html';
import 'dart:math'; import 'dart:math';
typedef void gridIterator(int x, int y); typedef void gridIterator(int x, int y);
@ -24,16 +23,18 @@ class Game {
// In-World Logic Updates // In-World Logic Updates
void update([num dt]) { void update([num dt]) {
Point next = isRandom ? nextPosRandom() : nextPosOrdered(_curPos); Point next = isRandom ? nextPosRandom() : nextPosOrdered(_curPos);
grid[next.y][next.x] = if (_curPos.x < 0) _curPos = Point(0, _curPos.y);
new Color(rng.nextInt(255), rng.nextInt(255), rng.nextInt(255)); Color newColor = isRandom ? randomColor() : nextColor(
grid[_curPos.y][_curPos.x]);
grid[next.y][next.x] = newColor;
_curPos = next; _curPos = next;
} }
Point nextPosOrdered(Point curPos) { Point nextPosOrdered(Point curPos) {
Point pos = Point(curPos.x, curPos.y); 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); 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; return pos;
} }
@ -42,6 +43,20 @@ class Game {
return Point(rng.nextInt(grid[ry].length), ry); 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 // Render Pipeline
void draw([num interp]) { void draw([num interp]) {
CanvasRenderingContext2D ctx = this.canvas.context2D; CanvasRenderingContext2D ctx = this.canvas.context2D;
@ -119,4 +134,9 @@ class Color {
final int b; final int b;
const Color(this.r, this.g, this.b); const Color(this.r, this.g, this.b);
@override
String toString() {
return "Color: (r:$r, g:$g, b:$b)";
}
} }