Added eye cell

This commit is contained in:
MaxRobinsonTheGreat
2020-08-10 19:19:52 -06:00
parent a6cfc916c2
commit 6003686a13
8 changed files with 107 additions and 17 deletions

2
dist/js/bundle.js vendored

File diff suppressed because one or more lines are too long

View File

@@ -54,9 +54,9 @@ class WorldEnvironment extends Environment{
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);
org.addCell(CellTypes.eye, 0, 0);
org.addCell(CellTypes.mouth, -1, -1);
org.addCell(CellTypes.mover, 1, 1);
this.addOrganism(org);
}

View File

@@ -7,9 +7,10 @@ const CellTypes = {
mover: 5,
killer: 6,
armor: 7,
colors: ['#121D29', 'green', 'gray', 'orange', 'white', '#3493eb', 'red', 'purple'],
eye: 8,
colors: ['#121D29', 'green', 'gray', 'orange', 'white', '#3493EB', 'red', 'purple', '#8D73A3'],
getRandomLivingType: function() {
return Math.floor(Math.random() * 5) + 3;
return Math.floor(Math.random() * 6) + 3;
}
}

View File

@@ -1,15 +1,24 @@
const CellTypes = require("./CellTypes");
const Directions = require("../Directions");
const Hyperparams = require("../../Hyperparameters");
const Eye = require("../Eye.js");
// 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){
constructor(type, loc_col, loc_row, eye=null){
this.type = type;
this.loc_col = loc_col;
this.loc_row = loc_row;
if (this.type == CellTypes.eye){
this.eye = new Eye(this);
if (eye != null) {
this.eye.direction = eye.direction;
}
}
}
rotatedCol(dir){
switch(dir){
case Directions.up:

View File

@@ -1,8 +1,8 @@
const Directions = {
up:0,
down:1,
left:2,
right:3,
right:1,
down:2,
left:3,
scalars:[[0,-1],[0,1],[-1,0],[1,0]],
getRandomDirection: function() {
return Math.floor(Math.random() * 4);

20
src/Organism/Eye.js Normal file
View File

@@ -0,0 +1,20 @@
const Directions = require("./Directions");
class Eye {
constructor(loc_cell, direction=-1) {
this.direction = direction;
if (direction == -1){
this.direction = Directions.getRandomDirection();
}
this.loc_cell = loc_cell
}
getAbsoluteDirection(parent_dir) {
var dir = parent_dir + this.direction;
if (dir > 3)
dir -= 4;
return dir;
}
}
module.exports = Eye;

View File

@@ -5,6 +5,7 @@ const LocalCell = require("./Cell/LocalCell");
const Neighbors = require("../Grid/Neighbors");
const Hyperparams = require("../Hyperparameters");
const Directions = require("./Directions");
const Eye = require("./Eye");
const directions = [[0,1],[0,-1],[1,0],[-1,0]]
@@ -32,7 +33,7 @@ class Organism {
}
}
addCell(type, c, r) {
addCell(type, c, r, eye=null) {
for (var cell of this.cells) {
if (cell.loc_col == c && cell.loc_row == r){
return false;
@@ -40,7 +41,7 @@ class Organism {
}
this.checkProducerMover(type);
this.cells.push(new LocalCell(type, c, r));
this.cells.push(new LocalCell(type, c, r, eye));
return true;
}
@@ -90,7 +91,10 @@ class Organism {
this.birth_distance = parent.birth_distance;
for (var c of parent.cells){
//deep copy parent cells
this.addCell(c.type, c.loc_col, c.loc_row);
if (c.type == CellTypes.eye)
this.addCell(c.type, c.loc_col, c.loc_row, c.eye);
else
this.addCell(c.type, c.loc_col, c.loc_row);
}
}
@@ -169,14 +173,20 @@ class Organism {
else if (choice <= Hyperparams.addProb + Hyperparams.changeProb){
// change cell
var cell = this.cells[Math.floor(Math.random() * this.cells.length)];
if (cell.type == CellTypes.eye) {
delete cell.eye;
}
cell.type = CellTypes.getRandomLivingType();
if (cell.type == CellTypes.eye) {
cell.eye = new Eye(cell);
}
this.checkProducerMover(cell.type);
mutated = true;
}
else if (choice <= Hyperparams.addProb + Hyperparams.changeProb + Hyperparams.removeProb){
// remove cell
if(this.cells.length > 1) {
cell = this.cells[Math.floor(Math.random() * this.cells.length)];
var cell = this.cells[Math.floor(Math.random() * this.cells.length)];
mutated = this.removeCell(cell.loc_col, cell.loc_row);
}
}
@@ -308,6 +318,9 @@ class Organism {
var real_c = this.c + cell.rotatedCol(this.rotation);
var real_r = this.r + cell.rotatedRow(this.rotation);
this.env.changeCell(real_c, real_r, cell.type, this);
if (cell.type == CellTypes.eye){
this.getRealCell(cell).direction = cell.eye.getAbsoluteDirection(this.rotation);
}
}
}
@@ -322,9 +335,12 @@ class Organism {
}
for (var cell of this.cells) {
this.getRealCell(cell).performFunction(this.env);
}
if (!this.living){
return this.living
if (!this.living){
return this.living
}
if (cell.type == CellTypes.eye && cell.eye == null){
console.log("whoe nellie");
}
}
if (this.is_mover) {
this.move_count++;

View File

@@ -1,3 +1,5 @@
const CellTypes = require("../Organism/Cell/CellTypes");
const Directions = require("../Organism/Directions");
// Renderer controls access to a canvas. There is one renderer for each canvas
class Renderer {
@@ -48,6 +50,48 @@ class Renderer {
renderCell(cell) {
this.ctx.fillStyle = cell.getColor();
this.ctx.fillRect(cell.x, cell.y, this.cell_size, this.cell_size);
if (cell.type == CellTypes.eye) {
this.renderEyeCell(cell);
}
}
renderEyeCell(cell) {
if(this.cell_size == 1)
return;
if (this.cell_size % 2 == 0){
//even
var w = 2;
}
else{
//odd
var w = 1;
}
var halfInt = Math.floor(this.cell_size/2);
var halfFloat = this.cell_size/2;
var h = this.cell_size/3;
var x = cell.x + h - Math.floor(w/2);
var y = cell.y;
this.ctx.translate(cell.x+halfFloat, cell.y+halfFloat);
this.ctx.rotate(cell.direction * 90 * Math.PI / 180);
// switch(cell.direction) {
// case Directions.up:
// this.ctx.rotate(90 * Math.PI / 180);
// break;
// case Directions.right:
// this.ctx.rotate(Math.PI / 180);
// break;
// case Directions.down:
// this.ctx.rotate(180 * Math.PI / 180);
// break;
// case Directions.left:
// this.ctx.rotate(270 * Math.PI / 180);
// break;
// }
this.ctx.fillStyle = '#FFFC5E';
this.ctx.fillRect(-halfFloat, -halfFloat, this.cell_size, h);
this.ctx.setTransform(1, 0, 0, 1, 0, 0);
}
renderOrganism(org) {