xxxxxxxxxx
181
let grid;
let numcells, cellsize;
const EMPTY = 0;
const WALL = 1;
const SAND = 2;
const LIGHT = 3;
let lastrow;
let colorGrid;
const cc239 = ["#e0eff0", "#e3dd34", "#78496b", "#f0527f", "#a7e0e2"];
const powerpuff = ["#201010", "#5dece1", "#ea50c4", "#47e752", "#130d0d"];
const miradors = ["#020202", "#ff6936", "#fddc3f", "#0075ca", "#03bb70"];
let colors;
function setup() {
createCanvas(1000, 1000);
noiseDetail(8,0.75);
colors = miradors;//random([cc239,powerpuff,miradors ]);
numcells = 80;
cellsize = width / numcells;
lastrow = numcells;
let z = random(10000);
grid = [];
colorGrid = [];
for (let r = 0; r < numcells; r++) {
grid[r] = [];
colorGrid[r] = [];
for (let c = 0; c < numcells; c++) {
grid[r][c] = EMPTY;
let n = noise(c*0.01, r*0.01, z)
colorGrid[r][c] = colors[int(map(n,0.0,1.0,0,colors.length-1))];
}
}
for (let r = 13; r < 20; r++) {
for (let c = 10; c < 15; c++) {
grid[r][c] = WALL;
grid[r][c] = WALL;
}
}
background(220);
frameRate(30);
stroke(220);
textSize(32);
}
function draw() {
// background(220);
fill(220);
noStroke();
rect(0, 0, width, lastrow * cellsize);
stroke(220);
let updaterow = false;
for (let r = lastrow - 1; r >= 0; r--) {
let filledcells = 0;
for (let c = 0; c < numcells; c++) {
if (grid[r][c] == SAND) {
let dir = random([-1, 1]);
let spd = random([1, 2, 3]);
if (spd == 3) {
if (
getCell(r + 3, c) == EMPTY &&
getCell(r + 2, c) == EMPTY &&
getCell(r + 1, c) == EMPTY
) {
grid[r][c] = EMPTY;
grid[r + 3][c] = SAND;
} else if (getCell(r + 2, c) == EMPTY && getCell(r + 1, c) == EMPTY) {
grid[r][c] = EMPTY;
grid[r + 2][c] = SAND;
} else if (getCell(r + 1, c) == EMPTY) {
// below
grid[r][c] = EMPTY;
grid[r + 1][c] = SAND;
} else if (getCell(r + 1, c + dir) == EMPTY) {
// side
grid[r][c] = EMPTY;
grid[r + 1][c + dir] = SAND;
}
} else if (spd == 2) {
if (getCell(r + 2, c) == EMPTY && getCell(r + 1, c) == EMPTY) {
grid[r][c] = EMPTY;
grid[r + 2][c] = SAND;
} else if (getCell(r + 1, c) == EMPTY) {
// below
grid[r][c] = EMPTY;
grid[r + 1][c] = SAND;
} else if (getCell(r + 1, c + dir) == EMPTY) {
// side
grid[r][c] = EMPTY;
grid[r + 1][c + dir] = SAND;
}
} else {
if (getCell(r + 1, c) == EMPTY) {
// below
grid[r][c] = EMPTY;
grid[r + 1][c] = SAND;
} else if (getCell(r + 1, c + dir) == EMPTY) {
// side
grid[r][c] = EMPTY;
grid[r + 1][c + dir] = SAND;
}
}
}
if (grid[r][c] == SAND) filledcells++;
}
if (filledcells == numcells) {
updaterow = true;
}
}
for (let r = 0; r < lastrow; r++) {
for (let c = 0; c < numcells; c++) {
// if (grid[r][c] == EMPTY) {
// fill(0);
// } else
if (grid[r][c] == SAND) {
// fill(color(100, 100, 0, 220));
let _c = color(colorGrid[r][c]);
_c.setAlpha(180);
fill(_c);
square(c * cellsize, r * cellsize, cellsize);
} else if (grid[r][c] == WALL) {
fill(color(100, 0, 100, 220));
square(c * cellsize, r * cellsize, cellsize);
}
}
}
if (updaterow && lastrow > 0) {
lastrow--;
}
if (1) {
//random() > 0.4) {
grid[0][random(0, numcells) | 0] = SAND;
}
// console.log(lastrow, numcells);
// if (random(1000) > 999) {
// clearRow();
// }
// if (random(1000) > 990) {
// fill(220);
// for (let c = 0; c < numcells; c++) {
// grid[numcells-1][c] = EMPTY;
// square(c * cellsize, (numcells-1) * cellsize, cellsize);
// }
// }
fill(0);
text(frameRate() | 0, 10, 30);
}
function keyPressed() {
if (key === "1") {
clearRow();
}
}
function clearRow() {
fill(220);
let r = random(0, numcells - 1) | 0;
for (let c = 0; c < numcells; c++) {
if (grid[r][c] == SAND) grid[r][c] = EMPTY;
square(c * cellsize, (numcells - 1) * cellsize, cellsize);
}
lastrow = numcells;
}
function getCell(r, c) {
if (r < 0 || r >= numcells || c < 0 || c >= numcells) return null;
return grid[r][c];
}