Initial Commit

This commit is contained in:
Marty Oehme 2018-07-05 17:59:11 +02:00
parent 869faf2919
commit 0277e5ddc7
13 changed files with 268 additions and 1 deletions

70
lib/App.dart Normal file
View file

@ -0,0 +1,70 @@
import 'dart:html' as html;
import 'package:rules_of_living/Cell.dart';
import 'package:rules_of_living/Grid.dart';
class App {
// Elapsed Time Counter - useful for Safety Timeout
Stopwatch _elapsed = new Stopwatch();
// Game Tick Rate - *does* impact game speed
final int _MS_PER_STEP = 1000 ~/ 1;
// Max Frame (i.e. Rendering) rate - does *not* impact game speed
final int _MS_PER_FRAME = 1000 ~/ 1;
// ms stuck in updateloop after which game will declare itself unresponsive
final int SAFETY_TIMEOUT = 1000;
num _updateLag = 0.0;
num _drawLag = 0.0;
final html.CanvasElement canvas;
final Grid grid = new Grid(20,20);
final List<List<Cell>> map = new Grid(20, 20).map;
App(this.canvas);
void process(num now) {
_drawLag = now;
_updateLag += _drawLag;
_elapsed.reset();
while (_updateLag >= _MS_PER_STEP) {
if (_elapsed.elapsedMilliseconds > SAFETY_TIMEOUT) {
// TODO stub - give warning etc when this occurs
break;
}
update();
_updateLag -= _MS_PER_STEP;
}
if (_drawLag >= _MS_PER_FRAME) {
render(_updateLag / _MS_PER_STEP);
_drawLag = 0;
}
}
void update() {
grid.update();
}
void render([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);
}
}
}
}

23
lib/Cell.dart Normal file
View file

@ -0,0 +1,23 @@
import 'package:rules_of_living/Rule.dart';
class Cell {
bool state;
List<Rule> surviveRules = new List<Rule>();
List<Rule> birthRules = new List<Rule>();
Cell([bool state = false]) : this.state = state;
void update(int neighbors) {
bool newState = false;
if (state == true) {
surviveRules.forEach((Rule rule) {
if (rule.evaluate(neighbors) == true) newState = true;
});
} else {
birthRules.forEach((Rule rule) {
if (rule.evaluate(neighbors) == true) newState = true;
});
}
state = newState;
}
}

63
lib/Grid.dart Normal file
View file

@ -0,0 +1,63 @@
import 'package:rules_of_living/Cell.dart';
import 'package:rules_of_living/Rule.dart';
class Grid {
final int w;
final int h;
final List<List<Cell>> map;
Grid(int w, int h)
: this.w = w,
this.h = h,
this.map = new List() {
map.addAll(_buildGrid(w, h));
map[5][5].state = true;
}
List<List<Cell>> _buildGrid(int w, int h) {
List<List<Cell>> grid = new List(h);
Rule threeTrue = new Rule((int n) {
if(n==3) return true;
else return false;
});
Rule twoTrue = new Rule((int n) {
if(n==2) return true;
else return false;
});
for (int y = 0; y < h; y++) {
grid[y] = new List(w);
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(twoTrue);
grid[y][x] = new Cell();
}
}
return grid;
}
void update() {
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( getNeighbors(x, y, 1) );
}
}
}
int getNeighbors(int x, int y, int range) {
int count = 0;
for (int iy = y - range ~/ 2; iy < iy + range / 2; iy++) {
for (int ix = x - range ~/ 2; ix < ix + range / 2; ix++) {
if (iy > 0 && iy < map.length && ix > 0 && ix < map[iy].length &&
map[iy][ix].state == true) count++;
}
}
return count;
}
}

5
lib/Rule.dart Normal file
View file

@ -0,0 +1,5 @@
class Rule {
final Function evaluate;
Rule(this.evaluate);
}