Refactor Rendering into Grid Class

This commit is contained in:
Marty Oehme 2018-07-06 16:47:20 +02:00
parent 6b7e8edb28
commit f5e528dceb
2 changed files with 86 additions and 34 deletions

View File

@ -8,10 +8,10 @@ class App {
Stopwatch _elapsed = new Stopwatch();
// Game Tick Rate - *does* impact game speed
final int _MS_PER_STEP = 1000;
int _MS_PER_STEP = 1000 ~/ 3;
// Max Frame (i.e. Rendering) rate - does *not* impact game speed
final int _MS_PER_FRAME = 1000 ~/ 2;
final int _MS_PER_FRAME = 1000 ~/ 30;
// ms stuck in updateloop after which game will declare itself unresponsive
final int SAFETY_TIMEOUT = 2000;
@ -21,12 +21,17 @@ class App {
final html.CanvasElement canvas;
final Grid grid = new Grid(20,20);
List<List<Cell>> map;
Grid grid = new Grid(100,100);
bool running = false;
App(this.canvas) {
this.map = this.grid.map;
_elapsed.start();
var runBtn = html.querySelector("#run");
runBtn.onClick.forEach((html.MouseEvent mouse) {
running = !running;
if(running) runBtn.innerHtml = "Stop";
if(!running) runBtn.innerHtml = "Start";
});
}
void process(num now) {
@ -51,26 +56,13 @@ class App {
}
void update() {
print("updating");
grid.update();
// print("updating");
if (running) grid.update();
}
void render([num interp]) {
print("rendering");
html.CanvasRenderingContext2D ctx = canvas.getContext('2d');
int brickW = (canvas.width ~/ map[0].length);
int brickH = (canvas.height ~/ map.length);
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (int y = 0; y < map.length; y++) {
for (int x = 0; x < map[y].length; x++) {
Cell c = map[y][x];
if (c.state == true)
ctx.setFillColorRgb(155, 155, 255);
else
ctx.setFillColorRgb(0, 0, 0);
ctx.fillRect(x * brickW, y * brickH, brickW, brickH);
}
}
// print("rendering");
grid.render(canvas, interp);
}
}

View File

@ -1,3 +1,5 @@
import 'dart:html' as html;
import 'Cell.dart';
import 'Rule.dart';
@ -12,21 +14,51 @@ class Grid {
this.map = new List() {
map.addAll(_buildGrid(w, h));
map[5][5].state = true;
map[5][6].state = true;
map[5][7].state = true;
// BLINKER
// map[5][5].state = true;
// map[5][6].state = true;
// map[6][5].state = true;
// map[6][6].state = true;
//
// map[7][7].state = true;
// map[7][8].state = true;
// map[8][7].state = true;
// map[8][8].state = true;
// SPACESHIP
setState(1 + 5, 0 + 5, true);
setState(2 + 5, 1 + 5, true);
setState(2 + 5, 2 + 5, true);
setState(1 + 5, 2 + 5, true);
setState(0 + 5, 2 + 5, true);
print("Grid creation finished");
}
void setState(int x, int y, bool state) {
if (y < map.length && x < map[y].length) map[y][x].state = state;
}
List<List<Cell>> _buildGrid(int w, int h) {
print("grid being created");
List<List<Cell>> grid = new List(h);
// GENERAL RULE LAYOUT
Rule threeTrue = new Rule((int n) {
if(n==3) return true;
if (n == 3) return true;
return false;
});
Rule twoTrue = new Rule((int n) {
if(n==2) return true;
if (n == 2) return true;
return false;
});
// DEBUG RULE TESTING FOR PATTERNS
Rule coagSurvive = new Rule((int n) {
if (n==1) return true;
return false;
});
Rule coagBirth = new Rule((int n) {
if (n==1) return true;
return false;
});
@ -35,9 +67,9 @@ class Grid {
for (int x = 0; x < w; x++) {
// GIVES RULES FOR CONWAY GAME OF LIFE BY DEFAULT S23/B3
Cell cell = new Cell();
cell.surviveRules.add(twoTrue);
cell.surviveRules.add(threeTrue);
cell.birthRules.add(threeTrue);
// cell.surviveRules.add(twoTrue);
cell.surviveRules.add(coagSurvive);
cell.birthRules.add(coagBirth);
grid[y][x] = cell;
}
@ -49,7 +81,13 @@ class Grid {
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
// DEFAULTS TO CONWAY GAME OF LIFE RANGE OF ONE
map[y][x].update( getSurroundingNeighbors(x, y, 1) );
map[y][x].update(getSurroundingNeighbors(x, y, 1));
}
}
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
// DEFAULTS TO CONWAY GAME OF LIFE RANGE OF ONE
map[y][x].advanceState();
}
}
}
@ -58,11 +96,33 @@ class Grid {
int count = 0;
for (int iy = y - range; iy <= y + range; iy++) {
for (int ix = x - range; ix <= x + range; ix++) {
if (ix > 0 && iy > 0 && iy < map.length && ix < map[iy].length && map[iy][ix].state == true && !(x == ix && y == iy)) {
count++;
}
if (ix > 0 &&
iy > 0 &&
iy < map.length &&
ix < map[iy].length &&
map[iy][ix].state == true &&
!(x == ix && y == iy)) {
count++;
}
}
}
return count;
}
void render(html.CanvasElement canvas, [num interp]) {
html.CanvasRenderingContext2D ctx = canvas.getContext('2d');
int brickW = (canvas.width ~/ map[0].length);
int brickH = (canvas.height ~/ map.length);
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (int y = 0; y < map.length; y++) {
for (int x = 0; x < map[y].length; x++) {
Cell c = map[y][x];
if (c.state == true)
ctx.setFillColorRgb(155, 155, 255);
else
ctx.setFillColorRgb(0, 0, 0);
ctx.fillRect(x * brickW, y * brickH, brickW, brickH);
}
}
}
}