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

93
src/Organism/Cell/Cell.js Normal file
View File

@@ -0,0 +1,93 @@
const CellTypes = require("./CellTypes");
const Hyperparams = require("../../Hyperparameters");
// A cell exists in a grid system.
class Cell{
constructor(type, col, row, x, y){
this.owner = null;
this.setType(type);
this.col = col;
this.row = row;
this.x = x;
this.y = y;
}
setType(type) {
this.type = type;
}
performFunction(env) {
switch(this.type){
case CellTypes.mouth:
eatFood(this, env);
break;
case CellTypes.producer:
growFood(this, env);
break;
case CellTypes.killer:
killNeighbors(this, env);
break;
}
}
getColor() {
return CellTypes.colors[this.type];
}
isLiving() {
return this.type != CellTypes.empty &&
this.type != CellTypes.food &&
this.type != CellTypes.wall;
}
}
function eatFood(self, env){
for (var loc of Hyperparams.edibleNeighbors){
var cell = env.grid_map.cellAt(self.col+loc[0], self.row+loc[1]);
eatNeighborFood(self, cell, env);
}
}
function eatNeighborFood(self, n_cell, env){
if (n_cell == null)
return;
if (n_cell.type == CellTypes.food){
env.changeCell(n_cell.col, n_cell.row, CellTypes.empty, null);
self.owner.food_collected++;
}
}
function growFood(self, env){
if (self.owner.is_mover)
return;
var prob = Hyperparams.foodProdProb;
if (Math.random() * 100 <= prob){
var loc = Hyperparams.growableNeighbors[Math.floor(Math.random() * Hyperparams.growableNeighbors.length)]
var c=loc[0];
var r=loc[1];
var cell = env.grid_map.cellAt(self.col+c, self.row+r);
if (cell != null && cell.type == CellTypes.empty){
env.changeCell(self.col+c, self.row+r, CellTypes.food, null);
return;
}
}
}
function killNeighbors(self, env) {
for (var loc of Hyperparams.killableNeighbors){
var cell = env.grid_map.cellAt(self.col+loc[0], self.row+loc[1]);
killNeighbor(self, cell);
}
}
function killNeighbor(self, n_cell) {
if(n_cell == null || n_cell.owner == null || self.owner == null || n_cell.owner == self.owner || !n_cell.owner.living || n_cell.type == CellTypes.armor)
return;
var is_hit = n_cell.type == CellTypes.killer; // has to be calculated before death
n_cell.owner.harm();
if (is_hit){
self.owner.harm();
}
}
module.exports = Cell;

View File

@@ -0,0 +1,16 @@
const CellTypes = {
empty: 0,
food: 1,
wall: 2,
mouth: 3,
producer: 4,
mover: 5,
killer: 6,
armor: 7,
colors: ['#121D29', 'green', 'gray', 'orange', 'white', '#3493eb', 'red', 'purple'],
getRandomLivingType: function() {
return Math.floor(Math.random() * 5) + 3;
}
}
module.exports = CellTypes;

View File

@@ -0,0 +1,40 @@
const CellTypes = require("./CellTypes");
const Directions = require("../Directions");
const Hyperparams = require("../../Hyperparameters");
// A local cell is a lightweight container for a cell in an organism. It does not directly exist in the grid
class LocalCell{
constructor(type, loc_col, loc_row){
this.type = type;
this.loc_col = loc_col;
this.loc_row = loc_row;
}
rotatedCol(dir){
switch(dir){
case Directions.up:
return this.loc_col;
case Directions.down:
return this.loc_col * -1;
case Directions.left:
return this.loc_row;
case Directions.right:
return this.loc_row * -1;
}
}
rotatedRow(dir){
switch(dir){
case Directions.up:
return this.loc_row;
case Directions.down:
return this.loc_row * -1;
case Directions.left:
return this.loc_col * -1;
case Directions.right:
return this.loc_col;
}
}
}
module.exports = LocalCell;