xxxxxxxxxx
167
let anyselected = false;
let select_info = { x: undefined, y: undefined, index: undefined };
class Cell {
constructor(x, y, alive = false){
this.x = x
this.y = y
this.alive = alive
this.selected = false;
}
update(){
if (this.selected) fill(255, 0, 0)
else if (this.alive) fill(255)
else fill(0x33)
rect(this.x * 10, this.y * 10, 10, 10)
}
select() {
if (this.alive == false) return;
if (this.selected) {
anyselected = false;
this.selected = false;
}
else {
if (anyselected) return;
anyselected = true;
this.selected = true;
}
}
}
let cells = [];
function setup() {
createCanvas(400, 400);
let halfheight = round((height / 10) / 2);
for (let y = 0; y < height/10; y++){
for (let x = 0; x < width/10; x++){
if (y >= 20)
cells.push( new Cell(x, y, true) )
else cells.push( new Cell(x, y) )
}
}
}
class Gene {
construtor(index, {x, y}) {
this.index = index
this.x = x
this.y = y
}
}
function draw() {
background(0x1c);
for (let cell of cells) cell.update()
}
function mousePressed() {
if (mouseButton === LEFT){
if (mouseX < 0 || mouseX > width ||
mouseY < 0 || mouseY > height) {
console.log("Outside window")
return
}
let x = int(mouseX / 10);
let y = int(mouseY / 10);
let index = int(x + y * (width / 10));
if (anyselected){
if (y + 1 < selectinfo.y && x == selectinfo.x) {
let jumperindex = int(x + (y + 1) * (width / 10))
jump(index, jumperindex)
}
else if (y - 1 > selectinfo.y && x == selectinfo.x) {
let jumperindex = int(x + (y - 1) * (width / 10))
jump(index, jumperindex)
}
else if (x + 1 < selectinfo.x && y == selectinfo.y) {
let jumperindex = int((x + 1) + y * (width / 10))
jump(index, jumperindex)
}
else if (x - 1 > selectinfo.x && y == selectinfo.y) {
let jumperindex = int((x - 1) + y * (width / 10))
jump(index, jumperindex)
}
else {
cells[index].select()
}
}
else {
cells[index].select()
selectinfo = {x: x, y: y, index: index}
}
}
}
function move(x, y, index){}
function jump(index, jumper){
if (cells[jumper].alive) {
cells[jumper].alive = false;
cells[selectinfo.index].selected = false
cells[selectinfo.index].alive = false
cells[index].alive = true
anyselected = false;
}
}
function resize() {
let width_input = document.getElementById('width');
let height_input = document.getElementById('height');
let new_grid = [];
for (let y = 0; y < height_input.value/10; y++){
for (let x = 0; x < width_input.value/10; x++){
if (y >= 20)
new_grid.push( new Cell(x, y, true) )
else new_grid.push( new Cell(x, y) )
}
}
for (let new_cell of new_grid){
for (let cell of cells){
if (new_cell.x == cell.x && new_cell.y == cell.y){
new_cell.alive = cell.alive
new_cell.selected = cell.selected;
}
}
}
cells = new_grid
resizeCanvas(width_input.value, height_input.value)
}