Game - Add Color Oscillation

This commit is contained in:
Marty Oehme 2018-07-20 14:36:32 +02:00
parent 5c71f761e7
commit 5b7552216d

View file

@ -1,21 +1,29 @@
import 'dart:html'; import 'dart:html';
import 'dart:math';
typedef void gridIterator(int x, int y); typedef void gridIterator(int x, int y);
class Game { class Game {
List<List<Color>> grid; List<List<Color>> grid;
CanvasElement canvas; CanvasElement canvas;
CanvasRenderingContext2D ctx; 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; bool _fwd = true;
Game(CanvasElement this.canvas) { 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'); 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]) { void draw([num interp]) {
int brickW = (canvas.width ~/ grid[0].length); int brickW = (canvas.width ~/ grid[0].length);
@ -23,13 +31,22 @@ class Game {
ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.clearRect(0, 0, canvas.width, canvas.height);
grid_foreach((x, y) { grid_foreach((x, y) {
Color col = grid[y][x]; Color col = _oscillate(grid[y][x]);
ctx.setFillColorRgb(col.r+_oscill, col.g+_oscill, col.b+_oscill); ctx.setFillColorRgb(col.r, col.g, col.b);
ctx.fillRect(x*brickW, y*brickH, brickW, brickH); 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) { void grid_foreach(gridIterator fun) {
for (int y = 0; y < grid.length; y++) { for (int y = 0; y < grid.length; y++) {