Fixed game of life.
1.1 --- a/factory/v2012/script.js Thu Jan 05 03:22:15 2012 +0100
1.2 +++ b/factory/v2012/script.js Thu Jan 05 15:32:41 2012 +0100
1.3 @@ -13,7 +13,7 @@
1.4 }, function() {
1.5 $(this).css("cursor", "auto");
1.6 });
1.7 - $("#sim").mousemove(function(event) { simulation.mouse_moved(mouse_pos(event, "#sim"));});
1.8 + $("#sim").mousemove(function(event) { simulation.mouse_moved(mouse_pos(event, "#sim")); });
1.9 var onEachFrame;
1.10 if (window.webkitRequestAnimationFrame) {
1.11 onEachFrame = function(cb) {
1.12 @@ -583,7 +583,7 @@
1.13 }
1.14 Simulation.prototype.draw = function() {
1.15 var now = $.now();
1.16 - while (this.redraw.length) {
1.17 + if (this.redraw.length) {
1.18 var cell = this.redraw.pop();
1.19 this.context.fillStyle = cell.value == 1 ? green.str() : background.str();
1.20 this.context.fillRect(cell.canvas_x, cell.canvas_y,
1.21 @@ -604,25 +604,24 @@
1.22 this.redraw.push(cell);
1.23 }
1.24 }
1.25 - if (this.last_update + 300 > now) {
1.26 + if (this.last_update + 1000 > now) {
1.27 return;
1.28 }
1.29 var changed = new Array();
1.30 - if (this.cell_queue.length) {
1.31 + var next_cell_queue = new Array();
1.32 + while (this.cell_queue.length) {
1.33 var cell = this.cell_queue.pop();
1.34 - if (changed[cell.hash()]) {
1.35 - console.log(cell)
1.36 - console.log(changed[cell.hash()]);
1.37 - return;
1.38 + if (changed[cell.hash()]) {
1.39 + continue;
1.40 }
1.41 var d = cell.density();
1.42 if (d == 3 && cell.value == 0) {
1.43 - changed[cell.hash()] = cell;
1.44 - this.cell_queue.concat(cell.neighbours());
1.45 + changed[cell.hash()] = cell;
1.46 + next_cell_queue = next_cell_queue.concat(cell.neighbours());
1.47 } else if (cell.value == 1) {
1.48 if (d < 2 || d > 3) {
1.49 changed[cell.hash()] = cell;
1.50 - this.cell_queue.concat(cell.neighbours());
1.51 + next_cell_queue = next_cell_queue.concat(cell.neighbours());
1.52 }
1.53 }
1.54 }
1.55 @@ -631,6 +630,7 @@
1.56 cell.set_value(1 - cell.value);
1.57 this.redraw.push(cell);
1.58 }
1.59 + this.cell_queue = this.cell_queue.concat(next_cell_queue);
1.60 this.last_update = now;
1.61 }
1.62 Simulation.prototype.mouse_moved = function(pos) {
2.1 --- a/script.js Thu Jan 05 03:22:15 2012 +0100
2.2 +++ b/script.js Thu Jan 05 15:32:41 2012 +0100
2.3 @@ -13,7 +13,7 @@
2.4 }, function() {
2.5 $(this).css("cursor", "auto");
2.6 });
2.7 - $("#sim").mousemove(function(event) { simulation.mouse_moved(mouse_pos(event, "#sim"));});
2.8 + $("#sim").mousemove(function(event) { simulation.mouse_moved(mouse_pos(event, "#sim")); });
2.9 var onEachFrame;
2.10 if (window.webkitRequestAnimationFrame) {
2.11 onEachFrame = function(cb) {
2.12 @@ -583,7 +583,7 @@
2.13 }
2.14 Simulation.prototype.draw = function() {
2.15 var now = $.now();
2.16 - while (this.redraw.length) {
2.17 + if (this.redraw.length) {
2.18 var cell = this.redraw.pop();
2.19 this.context.fillStyle = cell.value == 1 ? green.str() : background.str();
2.20 this.context.fillRect(cell.canvas_x, cell.canvas_y,
2.21 @@ -604,25 +604,24 @@
2.22 this.redraw.push(cell);
2.23 }
2.24 }
2.25 - if (this.last_update + 300 > now) {
2.26 + if (this.last_update + 1000 > now) {
2.27 return;
2.28 }
2.29 var changed = new Array();
2.30 - if (this.cell_queue.length) {
2.31 + var next_cell_queue = new Array();
2.32 + while (this.cell_queue.length) {
2.33 var cell = this.cell_queue.pop();
2.34 - if (changed[cell.hash()]) {
2.35 - console.log(cell)
2.36 - console.log(changed[cell.hash()]);
2.37 - return;
2.38 + if (changed[cell.hash()]) {
2.39 + continue;
2.40 }
2.41 var d = cell.density();
2.42 if (d == 3 && cell.value == 0) {
2.43 - changed[cell.hash()] = cell;
2.44 - this.cell_queue.concat(cell.neighbours());
2.45 + changed[cell.hash()] = cell;
2.46 + next_cell_queue = next_cell_queue.concat(cell.neighbours());
2.47 } else if (cell.value == 1) {
2.48 if (d < 2 || d > 3) {
2.49 changed[cell.hash()] = cell;
2.50 - this.cell_queue.concat(cell.neighbours());
2.51 + next_cell_queue = next_cell_queue.concat(cell.neighbours());
2.52 }
2.53 }
2.54 }
2.55 @@ -631,6 +630,7 @@
2.56 cell.set_value(1 - cell.value);
2.57 this.redraw.push(cell);
2.58 }
2.59 + this.cell_queue = this.cell_queue.concat(next_cell_queue);
2.60 this.last_update = now;
2.61 }
2.62 Simulation.prototype.mouse_moved = function(pos) {