control tabs, organism editor, code refactor

This commit is contained in:
MaxRobinsonTheGreat
2020-07-18 00:31:46 -06:00
parent 2fc2ba7b80
commit ebb39df34a
21 changed files with 524 additions and 402 deletions

View File

@@ -0,0 +1,95 @@
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 Environment{
constructor(cell_size) {
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;
}
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);
}
}
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);
}
averageMutability() {
if (this.organisms.length < 1)
return 0;
return this.total_mutability / this.organisms.length;
}
changeCell(c, r, type, owner) {
this.grid_map.setCellType(c, r, type);
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)
this.changeCell(wall.col, wall.row, CellTypes.empty, null);
}
clearOrganisms() {
for (var org of this.organisms)
org.die();
this.organisms = [];
}
reset() {
this.organisms = [];
this.grid_map.fillGrid(CellTypes.empty);
this.renderer.renderFullGrid(this.grid_map.grid);
this.total_mutability = 0;
this.OriginOfLife();
}
}
module.exports = Environment;

View File

@@ -0,0 +1,84 @@
const Organism = require("../Organism/Organism");
const GridMap = require('../Grid/GridMap');
const Renderer = require('../Rendering/Renderer');
const CellTypes = require('../Organism/Cell/CellTypes');
const EditorController = require("../Controllers/EditorController");
const Cell = require("../Organism/Cell/Cell");
class OrganismEditor {
constructor() {
this.is_active = true;
var cell_size = 20;
this.grid_map = new GridMap(11, 11, cell_size);
this.renderer = new Renderer('editor-canvas', 'editor-env', cell_size);
this.controller = new EditorController(this, this.renderer.canvas);
this.clear();
this.renderer.renderFullGrid(this.grid_map.grid);
}
update() {
if (this.is_active){
this.renderer.renderHighlights();
}
}
changeCell(c, r, type, owner) {
this.grid_map.setCellType(c, r, type);
this.grid_map.setCellOwner(c, r, owner);
this.renderer.renderFullGrid(this.grid_map.grid);
}
// absolute c r, not local
addCellToOrg(c, r, type) {
var center = this.grid_map.getCenter();
var loc_c = c - center[0];
var loc_r = r - center[1];
var prev_cell = this.organism.getLocalCell(loc_c, loc_r)
if (prev_cell != null) {
prev_cell.type = type;
this.changeCell(c, r, type, this.organism);
}
else if (this.organism.addCell(type, loc_c, loc_r)){
this.changeCell(c, r, type, this.organism);
}
}
removeCellFromOrg(c, r) {
var center = this.grid_map.getCenter();
var loc_c = c - center[0];
var loc_r = r - center[1];
if (loc_c == 0 && loc_r == 0){
alert("Cannot remove center cell");
return;
}
var prev_cell = this.organism.getLocalCell(loc_c, loc_r)
if (prev_cell != null) {
if (this.organism.removeCell(loc_c, loc_r)) {
this.changeCell(c, r, CellTypes.empty, null);
}
}
}
setOrganismToCopyOf(orig_org){
this.grid_map.fillGrid(CellTypes.empty);
var center = this.grid_map.getCenter();
this.organism = new Organism(center[0], center[1], this, orig_org);
this.organism.updateGrid();
}
getCopyOfOrg() {
var new_org = new Organism(0, 0, null, this.organism);
return new_org;
}
clear() {
this.grid_map.fillGrid(CellTypes.empty);
var center = this.grid_map.getCenter();
this.organism = new Organism(center[0], center[1], this, null);
this.organism.addCell(CellTypes.mouth, 0, 0);
this.organism.updateGrid();
}
}
module.exports = OrganismEditor;