Refactor Directories

Move Engine Files to /src Directory
This commit is contained in:
Marty Oehme 2018-07-07 16:42:30 +02:00
parent f3dfc3b368
commit 827ab83b88
4 changed files with 5 additions and 5 deletions

68
lib/src/App.dart Normal file
View file

@ -0,0 +1,68 @@
import 'dart:html' as html;
import 'package:rules_of_living/src/Cell.dart';
import 'package:rules_of_living/src/Grid.dart';
class App {
// Elapsed Time Counter - useful for Safety Timeout
Stopwatch _elapsed = new Stopwatch();
// Game Tick Rate - *does* impact game speed
int _MS_PER_STEP = 1000 ~/ 3;
// Max Frame (i.e. Rendering) rate - does *not* impact game speed
final int _MS_PER_FRAME = 1000 ~/ 30;
// ms stuck in updateloop after which game will declare itself unresponsive
final int SAFETY_TIMEOUT = 2000;
num _updateLag = 0.0;
num _drawLag = 0.0;
final html.CanvasElement canvas;
Grid grid = new Grid(100,100);
bool running = false;
App(this.canvas) {
_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) {
_drawLag+= _elapsed.elapsedMilliseconds;
_updateLag += _elapsed.elapsedMilliseconds;
_elapsed.reset();
while (_updateLag >= _MS_PER_STEP) {
if (_elapsed.elapsedMilliseconds > SAFETY_TIMEOUT) {
// TODO stub - give warning etc when this occurs
print("ERROR STUCK IN UPDATE LOOP");
break;
}
update();
_updateLag -= _MS_PER_STEP;
}
if (_drawLag >= _MS_PER_FRAME) {
render(_updateLag / _MS_PER_STEP);
_drawLag = 0;
}
}
void update() {
// print("updating");
if (running) grid.update();
}
void render([num interp]) {
// print("rendering");
grid.render(canvas, interp);
}
}

27
lib/src/Cell.dart Normal file
View file

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

128
lib/src/Grid.dart Normal file
View file

@ -0,0 +1,128 @@
import 'dart:html' as html;
import 'package:rules_of_living/src/Cell.dart';
import 'package:rules_of_living/src/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));
// 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;
return false;
});
Rule twoTrue = new Rule((int n) {
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;
});
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(coagSurvive);
cell.birthRules.add(coagBirth);
grid[y][x] = 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(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();
}
}
}
int getSurroundingNeighbors(int x, int y, int range) {
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++;
}
}
}
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);
}
}
}
}

5
lib/src/Rule.dart Normal file
View file

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