Make age 0-indexed

This commit is contained in:
Riley Shaw 2014-09-02 10:41:38 -07:00
parent 5f659888a3
commit cf088f2ced
4 changed files with 15 additions and 12 deletions

View File

@ -3,10 +3,10 @@ var _ = require('./util.js');
// abstract factory that adds a superclass of baseCreature // abstract factory that adds a superclass of baseCreature
var factory = (function () { var factory = (function () {
function baseCreature() { function baseCreature() {
this.age = 0; this.age = -1;
} }
function baseCA() { function baseCA() {
this.age = 0; this.age = -1;
} }
baseCreature.prototype.initialEnergy = 50; baseCreature.prototype.initialEnergy = 50;

View File

@ -30,7 +30,7 @@ function Terrarium(width, height, id, cellSize, insertAfter) {
* @return {grid} a grid adhering to the above rules * @return {grid} a grid adhering to the above rules
*/ */
Terrarium.prototype.makeGrid = function (content) { Terrarium.prototype.makeGrid = function (content) {
var grid = [], type = typeof content, creature; var grid = [], type = typeof content;
for (var x = 0, _w = this.width; x < _w; x++) { for (var x = 0, _w = this.width; x < _w; x++) {
grid.push([]); grid.push([]);
for (var y = 0, _h = this.height; y < _h; y++) { for (var y = 0, _h = this.height; y < _h; y++) {
@ -190,7 +190,7 @@ Terrarium.prototype.animate = function (steps, fn) {
if (i++ !== steps) self.nextFrame = requestAnimationFrame(tick); if (i++ !== steps) self.nextFrame = requestAnimationFrame(tick);
else { else {
self.nextFrame = false; self.nextFrame = false;
fn(); if (fn) fn();
} }
} }

17
dist/terra.js vendored
View File

@ -14,9 +14,11 @@ var _ = require('./util.js');
// abstract factory that adds a superclass of baseCreature // abstract factory that adds a superclass of baseCreature
var factory = (function () { var factory = (function () {
function baseCreature() { function baseCreature() {
this.age = 0; this.age = -1;
}
function baseCA() {
this.age = -1;
} }
function baseCA() {}
baseCreature.prototype.initialEnergy = 50; baseCreature.prototype.initialEnergy = 50;
baseCreature.prototype.maxEnergy = 100; baseCreature.prototype.maxEnergy = 100;
@ -107,7 +109,7 @@ var factory = (function () {
return true; return true;
}; };
baseCreature.prototype.process = function (neighbors) { baseCreature.prototype.process = function (neighbors, x, y) {
var step = {}; var step = {};
var maxEnergy = this.maxEnergy; var maxEnergy = this.maxEnergy;
@ -133,7 +135,7 @@ var factory = (function () {
baseCA.prototype.boundEnergy = function () {}; baseCA.prototype.boundEnergy = function () {};
baseCA.prototype.isDead = function () { return false; }; baseCA.prototype.isDead = function () { return false; };
baseCA.prototype.process = function (neighbors) {}; baseCA.prototype.process = function (neighbors, x, y) {};
baseCA.prototype.wait = function () {}; baseCA.prototype.wait = function () {};
// Storage for our creature types // Storage for our creature types
@ -322,7 +324,7 @@ function Terrarium(width, height, id, cellSize, insertAfter) {
* @return {grid} a grid adhering to the above rules * @return {grid} a grid adhering to the above rules
*/ */
Terrarium.prototype.makeGrid = function (content) { Terrarium.prototype.makeGrid = function (content) {
var grid = [], type = typeof content, creature; var grid = [], type = typeof content;
for (var x = 0, _w = this.width; x < _w; x++) { for (var x = 0, _w = this.width; x < _w; x++) {
grid.push([]); grid.push([]);
for (var y = 0, _h = this.height; y < _h; y++) { for (var y = 0, _h = this.height; y < _h; y++) {
@ -359,6 +361,7 @@ Terrarium.prototype.step = function (steps) {
function copyAndRemoveInner (origCreature) { function copyAndRemoveInner (origCreature) {
if (origCreature) { if (origCreature) {
var copy = _.assign(new (origCreature.constructor)(), origCreature); var copy = _.assign(new (origCreature.constructor)(), origCreature);
copy.age++;
return copy && !copy.isDead() ? copy : false; return copy && !copy.isDead() ? copy : false;
} else return false; } else return false;
} }
@ -391,7 +394,7 @@ Terrarium.prototype.step = function (steps) {
_.getNeighborCoords(x, y, gridWidth - 1, gridHeight - 1, creature.actionRadius), _.getNeighborCoords(x, y, gridWidth - 1, gridHeight - 1, creature.actionRadius),
zipCoordsWithNeighbors zipCoordsWithNeighbors
); );
var result = creature.process(neighbors); var result = creature.process(neighbors, x, y);
if (result) { if (result) {
var eigenColumn = eigenGrid[result.x]; var eigenColumn = eigenGrid[result.x];
if (!eigenColumn[result.y]) eigenColumn[result.y] = []; if (!eigenColumn[result.y]) eigenColumn[result.y] = [];
@ -481,7 +484,7 @@ Terrarium.prototype.animate = function (steps, fn) {
if (i++ !== steps) self.nextFrame = requestAnimationFrame(tick); if (i++ !== steps) self.nextFrame = requestAnimationFrame(tick);
else { else {
self.nextFrame = false; self.nextFrame = false;
fn(); if (fn) fn();
} }
} }

2
dist/terra.min.js vendored

File diff suppressed because one or more lines are too long