Enable Sequential Coloring

This commit is contained in:
Marty Oehme 2018-07-24 14:16:45 +02:00
parent 0384518b02
commit d6c6abf3f6
1 changed files with 25 additions and 5 deletions

View File

@ -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)";
}
}