mild code refactoring
This commit is contained in:
2
dist/js/bundle.js
vendored
2
dist/js/bundle.js
vendored
File diff suppressed because one or more lines are too long
@@ -1,4 +1,4 @@
|
|||||||
const Environment = require('./Environments/Environment');
|
const WorldEnvironment = require('./Environments/WorldEnvironment');
|
||||||
const ControlPanel = require('./Controllers/ControlPanel');
|
const ControlPanel = require('./Controllers/ControlPanel');
|
||||||
const OrganismEditor = require('./Environments/OrganismEditor');
|
const OrganismEditor = require('./Environments/OrganismEditor');
|
||||||
|
|
||||||
@@ -7,7 +7,7 @@ const render_speed = 60;
|
|||||||
class Engine{
|
class Engine{
|
||||||
constructor(){
|
constructor(){
|
||||||
this.fps = 60;
|
this.fps = 60;
|
||||||
this.env = new Environment(4);
|
this.env = new WorldEnvironment(4);
|
||||||
this.organism_editor = new OrganismEditor();
|
this.organism_editor = new OrganismEditor();
|
||||||
this.controlpanel = new ControlPanel(this);
|
this.controlpanel = new ControlPanel(this);
|
||||||
this.env.OriginOfLife();
|
this.env.OriginOfLife();
|
||||||
|
|||||||
@@ -1,122 +1,18 @@
|
|||||||
const Grid = require('../Grid/GridMap');
|
|
||||||
const Renderer = require('../Rendering/Renderer');
|
|
||||||
const GridMap = require('../Grid/GridMap');
|
|
||||||
const Organism = require('../Organism/Organism');
|
|
||||||
const CellTypes = require('../Organism/Cell/CellTypes');
|
|
||||||
const Cell = require('../Organism/Cell/Cell');
|
|
||||||
const EnvironmentController = require('../Controllers/EnvironmentController');
|
|
||||||
|
|
||||||
|
//An evironment has a grid_map, controller, and renderer
|
||||||
class Environment{
|
class Environment{
|
||||||
constructor(cell_size) {
|
constructor() {
|
||||||
this.renderer = new Renderer('env-canvas', 'env', cell_size);
|
|
||||||
this.controller = new EnvironmentController(this, this.renderer.canvas);
|
|
||||||
this.grid_rows = Math.floor(this.renderer.height / cell_size);
|
|
||||||
this.grid_cols = Math.floor(this.renderer.width / cell_size);
|
|
||||||
this.grid_map = new GridMap(this.grid_cols, this.grid_rows, cell_size);
|
|
||||||
this.renderer.renderFullGrid(this.grid_map.grid);
|
|
||||||
this.organisms = [];
|
|
||||||
this.walls = [];
|
|
||||||
this.total_mutability = 0;
|
|
||||||
this.auto_reset = true;
|
|
||||||
this.largest_cell_count = 0;
|
|
||||||
this.reset_count = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
update(delta_time) {
|
update(){
|
||||||
var to_remove = [];
|
alert("Environment.update() must be overriden");
|
||||||
for (var i in this.organisms) {
|
|
||||||
var org = this.organisms[i];
|
|
||||||
if (!org.living || !org.update()) {
|
|
||||||
to_remove.push(i);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
this.removeOrganisms(to_remove);
|
|
||||||
}
|
|
||||||
|
|
||||||
render() {
|
|
||||||
this.renderer.renderCells();
|
|
||||||
this.renderer.renderHighlights();
|
|
||||||
}
|
|
||||||
|
|
||||||
removeOrganisms(org_indeces) {
|
|
||||||
for (var i of org_indeces.reverse()){
|
|
||||||
this.total_mutability -= this.organisms[i].mutability;
|
|
||||||
this.organisms.splice(i, 1);
|
|
||||||
}
|
|
||||||
if (this.organisms.length == 0 && this.auto_reset){
|
|
||||||
this.reset_count++;
|
|
||||||
this.reset();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
OriginOfLife() {
|
|
||||||
var center = this.grid_map.getCenter();
|
|
||||||
var org = new Organism(center[0], center[1], this);
|
|
||||||
org.addCell(CellTypes.mouth, 0, 0);
|
|
||||||
org.addCell(CellTypes.producer, -1, -1);
|
|
||||||
org.addCell(CellTypes.producer, 1, 1);
|
|
||||||
this.addOrganism(org);
|
|
||||||
}
|
|
||||||
|
|
||||||
addOrganism(organism) {
|
|
||||||
organism.updateGrid();
|
|
||||||
this.total_mutability += organism.mutability;
|
|
||||||
this.organisms.push(organism);
|
|
||||||
if (organism.cells.length > this.largest_cell_count)
|
|
||||||
this.largest_cell_count = organism.cells.length;
|
|
||||||
}
|
|
||||||
|
|
||||||
averageMutability() {
|
|
||||||
if (this.organisms.length < 1)
|
|
||||||
return 0;
|
|
||||||
return this.total_mutability / this.organisms.length;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
changeCell(c, r, type, owner) {
|
changeCell(c, r, type, owner) {
|
||||||
this.grid_map.setCellType(c, r, type);
|
this.grid_map.setCellType(c, r, type);
|
||||||
this.grid_map.setCellOwner(c, r, owner);
|
this.grid_map.setCellOwner(c, r, owner);
|
||||||
this.renderer.addToRender(this.grid_map.cellAt(c, r));
|
|
||||||
if(type == CellTypes.wall)
|
|
||||||
this.walls.push(this.grid_map.cellAt(c, r));
|
|
||||||
}
|
|
||||||
|
|
||||||
clearWalls() {
|
|
||||||
for(var wall of this.walls){
|
|
||||||
if (this.grid_map.cellAt(wall.col, wall.row).type == CellTypes.wall)
|
|
||||||
this.changeCell(wall.col, wall.row, CellTypes.empty, null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
clearOrganisms() {
|
|
||||||
for (var org of this.organisms)
|
|
||||||
org.die();
|
|
||||||
this.organisms = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
reset(clear_walls=true) {
|
|
||||||
this.organisms = [];
|
|
||||||
this.grid_map.fillGrid(CellTypes.empty);
|
|
||||||
this.renderer.renderFullGrid(this.grid_map.grid);
|
|
||||||
this.total_mutability = 0;
|
|
||||||
this.OriginOfLife();
|
|
||||||
}
|
|
||||||
|
|
||||||
resizeGridColRow(cell_size, cols, rows) {
|
|
||||||
this.renderer.cell_size = cell_size;
|
|
||||||
this.renderer.fillShape(rows*cell_size, cols*cell_size);
|
|
||||||
this.grid_map.resize(cols, rows, cell_size);
|
|
||||||
this.reset();
|
|
||||||
}
|
|
||||||
|
|
||||||
resizeFillWindow(cell_size) {
|
|
||||||
this.renderer.cell_size = cell_size;
|
|
||||||
this.renderer.fillWindow('env');
|
|
||||||
var cols = Math.floor(this.renderer.width / cell_size);
|
|
||||||
var rows = Math.floor(this.renderer.height / cell_size);
|
|
||||||
this.grid_map.resize(cols, rows, cell_size);
|
|
||||||
this.reset();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = Environment;
|
|
||||||
|
|
||||||
|
module.exports = Environment;
|
||||||
@@ -1,17 +1,19 @@
|
|||||||
const Organism = require("../Organism/Organism");
|
const Environment = require('./Environment');
|
||||||
|
const Organism = require('../Organism/Organism');
|
||||||
const GridMap = require('../Grid/GridMap');
|
const GridMap = require('../Grid/GridMap');
|
||||||
const Renderer = require('../Rendering/Renderer');
|
const Renderer = require('../Rendering/Renderer');
|
||||||
const CellTypes = require('../Organism/Cell/CellTypes');
|
const CellTypes = require('../Organism/Cell/CellTypes');
|
||||||
const EditorController = require("../Controllers/EditorController");
|
const EditorController = require("../Controllers/EditorController");
|
||||||
const Cell = require("../Organism/Cell/Cell");
|
const Cell = require("../Organism/Cell/Cell");
|
||||||
|
|
||||||
class OrganismEditor {
|
class OrganismEditor extends Environment{
|
||||||
constructor() {
|
constructor() {
|
||||||
|
super(cell_size);
|
||||||
this.is_active = true;
|
this.is_active = true;
|
||||||
var cell_size = 13;
|
var cell_size = 13;
|
||||||
this.grid_map = new GridMap(15, 15, cell_size);
|
|
||||||
this.renderer = new Renderer('editor-canvas', 'editor-env', cell_size);
|
this.renderer = new Renderer('editor-canvas', 'editor-env', cell_size);
|
||||||
this.controller = new EditorController(this, this.renderer.canvas);
|
this.controller = new EditorController(this, this.renderer.canvas);
|
||||||
|
this.grid_map = new GridMap(15, 15, cell_size);
|
||||||
this.clear();
|
this.clear();
|
||||||
|
|
||||||
this.renderer.renderFullGrid(this.grid_map.grid);
|
this.renderer.renderFullGrid(this.grid_map.grid);
|
||||||
@@ -24,8 +26,7 @@ class OrganismEditor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
changeCell(c, r, type, owner) {
|
changeCell(c, r, type, owner) {
|
||||||
this.grid_map.setCellType(c, r, type);
|
super.changeCell(c, r, type, owner);
|
||||||
this.grid_map.setCellOwner(c, r, owner);
|
|
||||||
this.renderer.renderFullGrid(this.grid_map.grid);
|
this.renderer.renderFullGrid(this.grid_map.grid);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
124
src/Environments/WorldEnvironment.js
Normal file
124
src/Environments/WorldEnvironment.js
Normal file
@@ -0,0 +1,124 @@
|
|||||||
|
const Environment = require('./Environment');
|
||||||
|
const Grid = require('../Grid/GridMap');
|
||||||
|
const Renderer = require('../Rendering/Renderer');
|
||||||
|
const GridMap = require('../Grid/GridMap');
|
||||||
|
const Organism = require('../Organism/Organism');
|
||||||
|
const CellTypes = require('../Organism/Cell/CellTypes');
|
||||||
|
const Cell = require('../Organism/Cell/Cell');
|
||||||
|
const EnvironmentController = require('../Controllers/EnvironmentController');
|
||||||
|
|
||||||
|
class WorldEnvironment extends Environment{
|
||||||
|
constructor(cell_size) {
|
||||||
|
super(cell_size);
|
||||||
|
|
||||||
|
this.renderer = new Renderer('env-canvas', 'env', cell_size);
|
||||||
|
this.controller = new EnvironmentController(this, this.renderer.canvas);
|
||||||
|
var grid_rows = Math.floor(this.renderer.height / cell_size);
|
||||||
|
var grid_cols = Math.floor(this.renderer.width / cell_size);
|
||||||
|
this.grid_map = new GridMap(grid_cols, grid_rows, cell_size);
|
||||||
|
this.renderer.renderFullGrid(this.grid_map.grid);
|
||||||
|
this.organisms = [];
|
||||||
|
this.walls = [];
|
||||||
|
this.total_mutability = 0;
|
||||||
|
this.auto_reset = true;
|
||||||
|
this.largest_cell_count = 0;
|
||||||
|
this.reset_count = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
update(delta_time) {
|
||||||
|
var to_remove = [];
|
||||||
|
for (var i in this.organisms) {
|
||||||
|
var org = this.organisms[i];
|
||||||
|
if (!org.living || !org.update()) {
|
||||||
|
to_remove.push(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.removeOrganisms(to_remove);
|
||||||
|
}
|
||||||
|
|
||||||
|
render() {
|
||||||
|
this.renderer.renderCells();
|
||||||
|
this.renderer.renderHighlights();
|
||||||
|
}
|
||||||
|
|
||||||
|
removeOrganisms(org_indeces) {
|
||||||
|
for (var i of org_indeces.reverse()){
|
||||||
|
this.total_mutability -= this.organisms[i].mutability;
|
||||||
|
this.organisms.splice(i, 1);
|
||||||
|
}
|
||||||
|
if (this.organisms.length == 0 && this.auto_reset){
|
||||||
|
this.reset_count++;
|
||||||
|
this.reset();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
OriginOfLife() {
|
||||||
|
var center = this.grid_map.getCenter();
|
||||||
|
var org = new Organism(center[0], center[1], this);
|
||||||
|
org.addCell(CellTypes.mouth, 0, 0);
|
||||||
|
org.addCell(CellTypes.producer, -1, -1);
|
||||||
|
org.addCell(CellTypes.producer, 1, 1);
|
||||||
|
this.addOrganism(org);
|
||||||
|
}
|
||||||
|
|
||||||
|
addOrganism(organism) {
|
||||||
|
organism.updateGrid();
|
||||||
|
this.total_mutability += organism.mutability;
|
||||||
|
this.organisms.push(organism);
|
||||||
|
if (organism.cells.length > this.largest_cell_count)
|
||||||
|
this.largest_cell_count = organism.cells.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
averageMutability() {
|
||||||
|
if (this.organisms.length < 1)
|
||||||
|
return 0;
|
||||||
|
return this.total_mutability / this.organisms.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
changeCell(c, r, type, owner) {
|
||||||
|
super.changeCell(c, r, type, owner);
|
||||||
|
this.renderer.addToRender(this.grid_map.cellAt(c, r));
|
||||||
|
if(type == CellTypes.wall)
|
||||||
|
this.walls.push(this.grid_map.cellAt(c, r));
|
||||||
|
}
|
||||||
|
|
||||||
|
clearWalls() {
|
||||||
|
for(var wall of this.walls){
|
||||||
|
if (this.grid_map.cellAt(wall.col, wall.row).type == CellTypes.wall)
|
||||||
|
this.changeCell(wall.col, wall.row, CellTypes.empty, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
clearOrganisms() {
|
||||||
|
for (var org of this.organisms)
|
||||||
|
org.die();
|
||||||
|
this.organisms = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
reset(clear_walls=true) {
|
||||||
|
this.organisms = [];
|
||||||
|
this.grid_map.fillGrid(CellTypes.empty);
|
||||||
|
this.renderer.renderFullGrid(this.grid_map.grid);
|
||||||
|
this.total_mutability = 0;
|
||||||
|
this.OriginOfLife();
|
||||||
|
}
|
||||||
|
|
||||||
|
resizeGridColRow(cell_size, cols, rows) {
|
||||||
|
this.renderer.cell_size = cell_size;
|
||||||
|
this.renderer.fillShape(rows*cell_size, cols*cell_size);
|
||||||
|
this.grid_map.resize(cols, rows, cell_size);
|
||||||
|
this.reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
resizeFillWindow(cell_size) {
|
||||||
|
this.renderer.cell_size = cell_size;
|
||||||
|
this.renderer.fillWindow('env');
|
||||||
|
var cols = Math.floor(this.renderer.width / cell_size);
|
||||||
|
var rows = Math.floor(this.renderer.height / cell_size);
|
||||||
|
this.grid_map.resize(cols, rows, cell_size);
|
||||||
|
this.reset();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = WorldEnvironment;
|
||||||
|
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
const CellTypes = require("./CellTypes");
|
const CellTypes = require("./CellTypes");
|
||||||
const Hyperparams = require("../../Hyperparameters");
|
const Hyperparams = require("../../Hyperparameters");
|
||||||
|
|
||||||
// A cell exists in a grid system.
|
// A cell exists in a grid map.
|
||||||
class Cell{
|
class Cell{
|
||||||
constructor(type, col, row, x, y){
|
constructor(type, col, row, x, y){
|
||||||
this.owner = null;
|
this.owner = null;
|
||||||
@@ -10,24 +10,29 @@ class Cell{
|
|||||||
this.row = row;
|
this.row = row;
|
||||||
this.x = x;
|
this.x = x;
|
||||||
this.y = y;
|
this.y = y;
|
||||||
|
this.func = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
setType(type) {
|
setType(type) {
|
||||||
this.type = type;
|
this.type = type;
|
||||||
|
switch(this.type){
|
||||||
|
case CellTypes.mouth:
|
||||||
|
this.func = eatFood;
|
||||||
|
break;
|
||||||
|
case CellTypes.producer:
|
||||||
|
this.func = growFood;
|
||||||
|
break;
|
||||||
|
case CellTypes.killer:
|
||||||
|
this.func = killNeighbors;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
this.func = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
performFunction(env) {
|
performFunction(env) {
|
||||||
switch(this.type){
|
if (this.func == null) return;
|
||||||
case CellTypes.mouth:
|
this.func(this, env);
|
||||||
eatFood(this, env);
|
|
||||||
break;
|
|
||||||
case CellTypes.producer:
|
|
||||||
growFood(this, env);
|
|
||||||
break;
|
|
||||||
case CellTypes.killer:
|
|
||||||
killNeighbors(this, env);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getColor() {
|
getColor() {
|
||||||
|
|||||||
Reference in New Issue
Block a user