basic evolution finished

This commit is contained in:
MaxRobinsonTheGreat
2020-07-04 14:01:30 -06:00
parent 8d208ca9cc
commit 9b24e4cc1c
16 changed files with 2878 additions and 7 deletions

103
src/Rendering/Renderer.js Normal file
View File

@@ -0,0 +1,103 @@
// Renderer controls access to a canvas. There is one renderer for each canvas
class Renderer {
constructor(canvas_id, env, cell_size) {
this.cell_size = cell_size;
this.env = env;
this.canvas = document.getElementById(canvas_id);
this.ctx = canvas.getContext("2d");
this.canvas.width = window.innerWidth;
this.canvas.height = window.innerHeight;
this.height = canvas.height;
this.width = canvas.width;
this.cells_to_render = new Set();
this.cells_to_highlight = new Set();
this.highlighted_cells = new Set();
}
clear() {
this.ctx.fillStyle = 'white';
this.ctx.fillRect(0, 0, this.height, this.width);
}
renderFullGrid() {
var grid = this.env.grid_map.grid;
for (var col of grid) {
for (var cell of col){
this.ctx.fillStyle = cell.getColor();
this.ctx.fillRect(cell.x, cell.y, this.cell_size, this.cell_size);
}
}
}
renderCells() {
for (var cell of this.cells_to_render) {
this.renderCell(cell);
}
this.cells_to_render.clear();
}
renderCell(cell) {
this.ctx.fillStyle = cell.getColor();
this.ctx.fillRect(cell.x, cell.y, this.cell_size, this.cell_size);
}
renderOrganism(org) {
for(var org_cell of org.cells) {
var cell = org.getRealCell(org_cell);
this.renderCell(cell);
}
}
addToRender(cell) {
if (this.highlighted_cells.has(cell)){
this.cells_to_highlight.add(cell);
}
this.cells_to_render.add(cell);
}
renderHighlights() {
for (var cell of this.cells_to_highlight) {
this.renderCellHighlight(cell);
this.highlighted_cells.add(cell);
}
this.cells_to_highlight.clear();
}
highlightOrganism(org) {
for(var org_cell of org.cells) {
var cell = org.getRealCell(org_cell);
this.cells_to_highlight.add(cell);
}
}
highlightCell(cell) {
this.cells_to_highlight.add(cell);
}
renderCellHighlight(cell, color="yellow") {
this.renderCell(cell);
this.ctx.fillStyle = color;
this.ctx.globalAlpha = 0.5;
this.ctx.fillRect(cell.x, cell.y, this.cell_size, this.cell_size);
this.ctx.globalAlpha = 1;
this.highlighted_cells.add(cell);
}
clearAllHighlights(clear_to_highlight=false) {
for (var cell of this.highlighted_cells) {
this.renderCell(cell);
}
this.highlighted_cells.clear();
if (clear_to_highlight) {
this.cells_to_highlight.clear();
}
}
}
// $("body").mousemove(function(e) {
// console.log("hello");
// });
module.exports = Renderer;