eden/script.js

159 lines
3.6 KiB
JavaScript

// 0 - Plot
// 1 - Plant
// 2 - Anti-plant
// 3 - barrier
let grid = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
let current_tile = 0;
let clock_on = false;
function toggle_clock(check)
{
clock_on = !clock_on;
if(clock_on) {
check.innerHTML = "Clock is ON";
} else {
check.innerHTML= "Clock is OFF";
}
}
function clock()
{
if(clock_on) {
tick();
}
}
function set_tile(i)
{
current_tile = i;
}
function cell_click(cell, i)
{
if(clock_on) {
return;
}
grid[i] = current_tile;
update_grid();
}
function update_grid() {
let i = 0;
while(i < 256)
{
if(grid[i] == 0) {
document.getElementById(i).classList = "cell";
} else if(grid[i] == 1) {
document.getElementById(i).classList = "cell plant";
} else if(grid[i] == 2) {
document.getElementById(i).classList = "cell anti";
} else if(grid[i] == 3) {
document.getElementById(i).classList = "cell barrier";
}
i++;
}
}
function tick()
{
let i = 0;
let north = 0;
let south = 0;
let east = 0;
let west = 0;
let new_grid = structuredClone(grid);
while(i < 256)
{
north = i-16;
south = i+16;
east = i+1;
west = i-1;
if(i % 16 == 0) {
west = -1;
} else if(i % 16 == 15) {
east = -1;
}
if(i < 16) {
north = -1;
} else if(i > 239) {
south = -1;
}
if(grid[i] == 1) {
if(north >= 0 && new_grid[north] == 0) { new_grid[north] = 1; }
if(east >= 0 && new_grid[east] == 0) { new_grid[east] = 1; }
if(west >= 0 && new_grid[west] == 0) { new_grid[west] = 1; }
if(south >= 0 && new_grid[south] == 0) { new_grid[south] = 1; }
if(north >= 0 && new_grid[north] == 2 && Math.floor(Math.random() * 10) == 0) { new_grid[north] = 1; }
if(east >= 0 && new_grid[east] == 2 && Math.floor(Math.random() * 10) == 0) { new_grid[east] = 1; }
if(west >= 0 && new_grid[west] == 2 && Math.floor(Math.random() * 10) == 0) { new_grid[west] = 1; }
if(south >= 0 && new_grid[south] == 2 && Math.floor(Math.random() * 10) == 0) { new_grid[south] = 1; }
} else if(grid[i] == 2) {
if(north >= 0 && new_grid[north] == 0) { new_grid[north] = 2; }
if(east >= 0 && new_grid[east] == 0) { new_grid[east] = 2; }
if(west >= 0 && new_grid[west] == 0) { new_grid[west] = 2; }
if(south >= 0 && new_grid[south] == 0) { new_grid[south] = 2; }
if(north >= 0 && new_grid[north] == 1 && Math.floor(Math.random() * 10) == 0) { new_grid[north] = 2; }
if(east >= 0 && new_grid[east] == 1 && Math.floor(Math.random() * 10) == 0) { new_grid[east] = 2; }
if(west >= 0 && new_grid[west] == 1 && Math.floor(Math.random() * 10) == 0) { new_grid[west] = 2; }
if(south >= 0 && new_grid[south] == 1 && Math.floor(Math.random() * 10) == 0) { new_grid[south] = 2; }
}
i++;
}
grid = structuredClone(new_grid);
update_grid();
}
function kill_living() {
if(clock_on) {
return;
}
let i = 0;
while(i < 256)
{
if(grid[i] == 1 || grid[i] == 2) {
grid[i] = 0;
}
i++;
}
update_grid();
}
function clear_board() {
if(clock_on) {
return;
}
let i = 0;
while(i < 256)
{
grid[i] = 0;
i++;
}
update_grid();
}
setInterval(clock, 50);