Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int w;
- int h;
- int t;
- int currentT;
- float randomFillPercent;
- int[][] map;
- int[][][] map2;
- float sizeX, sizeY;
- float nextTime = 0;
- float timeOffset = 0.02;
- void setup() {
- size(1000, 1000, P3D);
- w = 50;
- h = 50;
- t = 200;
- currentT = 0;
- resetMap2();
- sizeX = 5;
- sizeY = 5;
- randomFillPercent = 0.2 ;
- generateMap();
- surface.setLocation(0, 0);
- ortho(-width/2, width/2, -height/2, height/2, 1, 10000);
- }
- void draw() {
- background(20);
- translate(width/2, height/2);
- rotateX(PI*0.25);
- rotateZ(-PI*0.75);
- translate(0, 0, -500);
- rotateZ(millis()*0.0005);
- noStroke();
- //stroke(0);
- lights();
- fill(255);
- for (int x = 0; x < w; x++) {
- for (int y = 0; y < h; y++) {
- for (int z = 0; z < t; z++) {
- //fill(map[x][y] == 0 ? 0 : 255);
- push();
- //rect(x * sizeX - (w*sizeX)/2.0, y * sizeY - (h*sizeY)/2.0, sizeX, sizeY);
- translate(x * sizeX - (w*sizeX)/2.0, y * sizeY - (h*sizeY)/2.0, z * sizeY);
- if (map2[x][y][z] == 1) box(sizeX);
- pop();
- }
- }
- }
- drawAxes();
- if (nextTime < millis()*0.001) {
- smoothMap();
- nextTime = millis()*0.001 + timeOffset;
- }
- }
- void drawAxes() {
- float axisLenght = 100;
- stroke(255, 0, 0);
- line(0, 0, 0, axisLenght, 0, 0);
- stroke(0, 255, 0);
- line(0, 0, 0, 0, axisLenght, 0);
- stroke(0, 0, 255);
- line(0, 0, 0, 0, 0, axisLenght);
- stroke(255, 255, 0);
- line(0, 0, 0, 0, 0, -axisLenght);
- }
- void keyPressed() {
- if (key == ' ') {
- randomFillPercent = map(mouseX, 0, width, 0.1, 0.9);
- generateMap();
- }
- if (key == 's') {
- smoothMap();
- }
- }
- /////////////////////////////////////////
- void generateMap() {
- resetMap2();
- map = new int[w][h];
- randomFillMap();
- smoothMap();
- }
- void randomFillMap() {
- for (int x = 0; x < w; x++) {
- for (int y = 0; y < h; y++) {
- if (x == 0 || x == w || y == 0 || y == h) {
- map[x][y] = 1;
- } else {
- map[x][y] = random(1) < randomFillPercent ? 1 : 0;
- }
- }
- }
- }
- void smoothMap() {
- int[][] newMap = new int[w][h];
- for (int x = 0; x < w; x++) {
- for (int y = 0; y < h; y++) {
- int neighbourWallTiles = getSurroundingWallCount(x, y);
- int currentCell = map[x][y];
- //if(neighbourWallTiles > 4) newMap[x][y] = 1;
- //else if(neighbourWallTiles < 4) newMap[x][y] = 0;
- if (currentCell == 1 && (neighbourWallTiles == 2 || neighbourWallTiles == 3)) newMap[x][y] = currentCell;
- else if (currentCell == 0 && neighbourWallTiles == 3) newMap[x][y] = 1;
- else newMap[x][y] = 0;
- }
- }
- map = newMap;
- copyMap();
- }
- int getSurroundingWallCount(int gridX, int gridY) {
- int wallCount = 0;
- for (int neighbourX = gridX - 1; neighbourX <= gridX + 1; neighbourX++) {
- for (int neighbourY = gridY - 1; neighbourY <= gridY + 1; neighbourY++) {
- if (neighbourX >= 0 && neighbourX < w && neighbourY >= 0 && neighbourY < h) {
- if (neighbourX != gridX || neighbourY != gridY) {
- wallCount += map[neighbourX][neighbourY];
- }
- } else {
- wallCount++;
- }
- }
- }
- return wallCount;
- }
- void copyMap() {
- if (currentT < t) {
- for (int x = 0; x < w; x++) {
- for (int y = 0; y < h; y++) {
- map2[x][y][currentT] = map[x][y];
- }
- }
- currentT++;
- }
- }
- void resetMap2() {
- map2 = new int[w][h][t];
- for (int x = 0; x < w; x++) {
- for (int y = 0; y < h; y++) {
- for (int z = 0; z < t; z++) {
- map2[x][y][z] = 0;
- }
- }
- }
- currentT = 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement