control tabs, organism editor, code refactor
This commit is contained in:
74
src/Grid/GridMap.js
Normal file
74
src/Grid/GridMap.js
Normal file
@@ -0,0 +1,74 @@
|
||||
const Cell = require('../Organism/Cell/Cell');
|
||||
const CellTypes = require('../Organism/Cell/CellTypes');
|
||||
|
||||
class GridMap {
|
||||
constructor(cols, rows, cell_size, filltype=CellTypes.empty) {
|
||||
this.grid = [];
|
||||
this.cols = cols;
|
||||
this.rows = rows;
|
||||
this.cell_size = cell_size;
|
||||
for(var c=0; c<cols; c++) {
|
||||
var row = [];
|
||||
for(var r=0; r<rows; r++) {
|
||||
var cell = new Cell(filltype, c, r, c*cell_size, r*cell_size);
|
||||
|
||||
row.push(cell);
|
||||
}
|
||||
this.grid.push(row);
|
||||
}
|
||||
}
|
||||
|
||||
fillGrid(type) {
|
||||
for (var col of this.grid) {
|
||||
for (var cell of col){
|
||||
cell.setType(type);
|
||||
cell.owner = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cellAt(col, row) {
|
||||
if (!this.isValidLoc(col, row)) {
|
||||
return null;
|
||||
}
|
||||
return this.grid[col][row];
|
||||
}
|
||||
|
||||
setCellType(col, row, type) {
|
||||
if (!this.isValidLoc(col, row)) {
|
||||
return;
|
||||
}
|
||||
this.grid[col][row].setType(type);
|
||||
}
|
||||
|
||||
setCellOwner(col, row, owner) {
|
||||
if (!this.isValidLoc(col, row)) {
|
||||
return;
|
||||
}
|
||||
this.grid[col][row].owner = owner;
|
||||
}
|
||||
|
||||
isValidLoc(col, row){
|
||||
return col<this.cols && row<this.rows && col>=0 && row>=0;
|
||||
}
|
||||
|
||||
getCenter(){
|
||||
return [Math.floor(this.cols/2), Math.floor(this.rows/2)]
|
||||
}
|
||||
|
||||
xyToColRow(x, y) {
|
||||
var c = Math.floor(x/this.cell_size);
|
||||
var r = Math.floor(y/this.cell_size);
|
||||
if (c >= this.cols)
|
||||
c = this.cols-1;
|
||||
else if (c < 0)
|
||||
c = 0;
|
||||
if (r >= this.rows)
|
||||
r = this.rows-1;
|
||||
else if (r < 0)
|
||||
r = 0;
|
||||
return [c, r];
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = GridMap;
|
||||
21
src/Grid/Neighbors.js
Normal file
21
src/Grid/Neighbors.js
Normal file
@@ -0,0 +1,21 @@
|
||||
// contains local cell values for the following:
|
||||
|
||||
//all ...
|
||||
// .x.
|
||||
// ...
|
||||
|
||||
//adjacent .
|
||||
// .x.
|
||||
// .
|
||||
|
||||
//corners . .
|
||||
// x
|
||||
// . .
|
||||
|
||||
const Neighbors = {
|
||||
all: [[0, 1],[0, -1],[1, 0],[-1, 0],[-1, -1],[1, 1],[-1, 1],[1, -1]],
|
||||
adjacent: [[0, 1],[0, -1],[1, 0],[-1, 0]],
|
||||
corners: [[-1, -1],[1, 1],[-1, 1],[1, -1]]
|
||||
}
|
||||
|
||||
module.exports = Neighbors;
|
||||
Reference in New Issue
Block a user