Organic looking random walls using noise start

This commit is contained in:
M4YX0R
2021-12-11 09:33:12 +03:00
parent 7a4c19bbdf
commit f9da6ac81f
4 changed files with 28 additions and 3 deletions

2
dist/index.html vendored
View File

@@ -35,6 +35,8 @@
<button id='reset-env' title='Restarts simulation with default organism.'>Reset</button>
<button id='clear-env' title="Removes all organisms and walls. Will disable 'reset on extinction'"">Clear</button>
<br>
<button id='random-walls' title="Generates random walls.">Randomize Walls</button>
<br>
<label for="auto-reset">Reset on total extinction</label>
<input type="checkbox" id="auto-reset" checked>
<br>

View File

@@ -327,6 +327,9 @@ class ControlPanel {
env.auto_reset = false;
$('#auto-reset').prop('checked', false);;
});
$('#random-walls').click( function() {
this.env_controller.randomizeWalls();
}.bind(this));
$('#auto-reset').change(function() {
env.auto_reset = this.checked;
});

View File

@@ -51,6 +51,26 @@ 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) {
var noise_scale = 0.05;
var noise_offset = 0.5;
var noise_threshold = 0.5;
var noise_multiplier = 0.5;
var noise_offset_x = this.env.num_cols/2;
var noise_offset_y = this.env.num_rows/2;
for (var r = 0; r < this.env.num_rows; r++) {
for (var c = 0; c < this.env.num_cols; c++) {
var noise = noise_multiplier * noise_offset + noise_scale * noise_offset * Math.sin(noise_scale * (c + noise_offset_x) + noise_scale * (r + noise_offset_y));
if (noise > noise_threshold && noise < noise_threshold + thickness/10) {
this.dropCellType(c, r, CellStates.wall, true);
}
}
}
}
updateMouseLocation(offsetX, offsetY){
super.updateMouseLocation(offsetX, offsetY);

View File

@@ -12,9 +12,9 @@ class WorldEnvironment extends Environment{
super();
this.renderer = new Renderer('env-canvas', 'env', cell_size);
this.controller = new EnvironmentController(this, this.renderer.canvas);
var grid_rows = Math.ceil(this.renderer.height / cell_size);
var grid_cols = Math.ceil(this.renderer.width / cell_size);
this.grid_map = new GridMap(grid_cols, grid_rows, cell_size);
this.num_rows = Math.ceil(this.renderer.height / cell_size);
this.num_cols = Math.ceil(this.renderer.width / cell_size);
this.grid_map = new GridMap(num_cols, num_rows, cell_size);
this.organisms = [];
this.walls = [];
this.total_mutability = 0;