enlarging crowd of cubes 2
@author
int BOX_NUM = 25;
float BOX_SIZE = 20;
int[][][] states;
int[][][] neighbors;
int[][] moves = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}, {-1, 0, 0}, {0, -1, 0}, {0, 0, -1}};
ArrayList<Block> blocks;
ArrayList<Block> pBlocks;
void setup(){
size(500, 500, P3D);
stroke(255);
strokeWeight(2);
fill(255, 20, 147);
neighbors = new int[BOX_NUM][BOX_NUM][BOX_NUM];
states = new int[BOX_NUM][BOX_NUM][BOX_NUM];
blocks = new ArrayList<Block>();
pBlocks = new ArrayList<Block>();
for(int i = 0; i < 10; i++){
addBlock(int(random(BOX_NUM)), int(random(BOX_NUM)), int(random(BOX_NUM)));
}
}
void addBlock(int x, int y, int z){
if(states[x][y][z] != 2 && states[x][y][z] != 3){
Block block = new Block(x, y, z);
blocks.add(block);
states[x][y][z] = 2;
for(int[] move: moves){
int nx = x + move[0];
int ny = y + move[1];
int nz = z + move[2];
if(0 <= nx && nx < BOX_NUM && 0 <= ny && ny < BOX_NUM && 0 <= nz && nz < BOX_NUM){
neighbors[nx][ny][nz]++;
if(neighbors[nx][ny][nz] == 6){
states[nx][ny][nz] = 3;
}
if(states[nx][ny][nz] == 0){
states[nx][ny][nz] = 1;
pBlocks.add(new Block(nx, ny, nz));
}
}
}
}
}
void draw(){
background(255);
translate(width / 2, height / 2, BOX_NUM * BOX_SIZE / 2 - 500);
lights();
rotateY(map(mouseX, 0, width, -PI / 4, PI / 4));
rotateX(map(mouseY, 0, height, -PI / 4, PI / 4));
for(Block block: blocks){
block.display();
}
Block pBlock = pBlocks.remove(int(random(pBlocks.size())));
addBlock(pBlock.x, pBlock.y, pBlock.z);
}
class Block{
int x, y, z;
Block(int _x, int _y, int _z){
x = _x;
y = _y;
z = _z;
}
void display(){
if(states[x][y][z] != 3){
pushMatrix();
translate(x * BOX_SIZE + BOX_SIZE / 2 - width / 2, y * BOX_SIZE + BOX_SIZE / 2 - height / 2, z * BOX_SIZE + BOX_SIZE / 2 - BOX_NUM * BOX_SIZE / 2);
box(BOX_SIZE);
popMatrix();
}
}
}