Merge pull request #81 from MaxRobinsonTheGreat/random-walls

Random walls
This commit is contained in:
Max Robinson
2021-12-17 19:01:28 -06:00
committed by GitHub
6 changed files with 88 additions and 8 deletions

View File

@@ -5,6 +5,7 @@ const CellStates = require("../Organism/Cell/CellStates");
const Neighbors = require("../Grid/Neighbors");
const FossilRecord = require("../Stats/FossilRecord");
const Hyperparams = require("../Hyperparameters");
const Perlin = require("../Utils/Perlin");
class EnvironmentController extends CanvasController{
constructor(env, canvas) {
@@ -51,8 +52,34 @@ class EnvironmentController extends CanvasController{
this.scale = 1;
}
/*
Iterate over grid from 0,0 to env.num_cols,env.num_rows and create random walls using perlin noise to create a more organic shape.
*/
randomizeWalls(thickness=1) {
this.env.clearWalls();
const noise_threshold = -0.017;
let avg_noise = 0;
let resolution = 20;
Perlin.seed();
for (let r = 0; r < this.env.num_rows; r++) {
for (let c = 0; c < this.env.num_cols; c++) {
let xval = c/this.env.num_cols*(resolution/this.env.renderer.cell_size*(this.env.num_cols/this.env.num_rows));
let yval = r/this.env.num_rows*(resolution/this.env.renderer.cell_size*(this.env.num_rows/this.env.num_cols));
let noise = Perlin.get(xval, yval);
avg_noise += noise/(this.env.num_rows*this.env.num_cols);
if (noise > noise_threshold && noise < noise_threshold + thickness/resolution) {
let cell = this.env.grid_map.cellAt(c, r);
if (cell != null) {
if(cell.owner != null) cell.owner.die();
this.env.changeCell(c, r, CellStates.wall, null);
}
}
}
}
}
updateMouseLocation(offsetX, offsetY){
super.updateMouseLocation(offsetX, offsetY);
}