v0.7.5: added new functionality to maze generation

This commit is contained in:
Evan Debenham 2019-08-22 15:23:33 -04:00
parent 71b406c84d
commit de10df7866
4 changed files with 37 additions and 11 deletions

View File

@ -56,6 +56,19 @@ public class Maze {
return generate(r.width()+1, r.height()+1); return generate(r.width()+1, r.height()+1);
} }
public static boolean[][] generate(Rect r, int[] terrain, int width, int filledTerrainType){
boolean[][] maze = new boolean[r.width()][r.height()];
for (int x = 0; x < maze.length; x++) {
for (int y = 0; y < maze[0].length; y++) {
if (terrain[x + r.left + (y + r.top)*width] == filledTerrainType){
maze[x][y] = FILLED;
}
}
}
return generate(maze);
}
public static boolean[][] generate(int width, int height){ public static boolean[][] generate(int width, int height){
return generate(new boolean[width][height]); return generate(new boolean[width][height]);
} }
@ -123,22 +136,32 @@ public class Maze {
return null; return null;
} }
public static boolean allowDiagonals = false;
private static boolean checkValidMove( boolean[][] maze, int x, int y, int[] mov){ private static boolean checkValidMove( boolean[][] maze, int x, int y, int[] mov){
int sideX = 1 - Math.abs(mov[0]); int sideX = 1 - Math.abs(mov[0]);
int sideY = 1 - Math.abs(mov[1]); int sideY = 1 - Math.abs(mov[1]);
//checking two tiles forward in the movement, and the tiles to their left/right x += mov[0];
for (int i = 0; i < 2; i ++) { y += mov[1];
x += mov[0];
y += mov[1]; if ( x <= 0 || x >= maze.length-1 || y <= 0 || y >= maze[0].length-1){
//checks if tiles we're examining are valid and open return false;
if ( x > 0 && x < maze.length-1 && y > 0 && y < maze[0].length-1 && } else if (maze[x][y] || maze[x + sideX][y + sideY] || maze[x - sideX][y - sideY]){
!maze[x][y] && !maze[x + sideX][y+ sideY] && !maze[x - sideX][y - sideY]){ return false;
continue;
} else {
return false;
}
} }
x += mov[0];
y += mov[1];
if ( x <= 0 || x >= maze.length-1 || y <= 0 || y >= maze[0].length-1){
return false;
} else if (maze[x][y]){
return false;
} else if (!allowDiagonals && (maze[x + sideX][y + sideY] || maze[x - sideX][y - sideY])){
return false;
}
return true; return true;
} }
} }

View File

@ -33,6 +33,7 @@ public class MazeRoom extends Room {
Painter.fill(level, this, 1, Terrain.EMPTY); Painter.fill(level, this, 1, Terrain.EMPTY);
//true = space, false = wall //true = space, false = wall
Maze.allowDiagonals = false;
boolean[][] maze = Maze.generate(this); boolean[][] maze = Maze.generate(this);
Painter.fill(level, this, 1, Terrain.EMPTY); Painter.fill(level, this, 1, Terrain.EMPTY);

View File

@ -33,6 +33,7 @@ public class MazeConnectionRoom extends ConnectionRoom {
Painter.fill(level, this, 1, Terrain.EMPTY); Painter.fill(level, this, 1, Terrain.EMPTY);
//true = space, false = wall //true = space, false = wall
Maze.allowDiagonals = false;
boolean[][] maze = Maze.generate(this); boolean[][] maze = Maze.generate(this);
Painter.fill(level, this, 1, Terrain.EMPTY); Painter.fill(level, this, 1, Terrain.EMPTY);

View File

@ -62,6 +62,7 @@ public class SecretMazeRoom extends SecretRoom {
Painter.fill(level, this, 1, Terrain.EMPTY); Painter.fill(level, this, 1, Terrain.EMPTY);
//true = space, false = wall //true = space, false = wall
Maze.allowDiagonals = false;
boolean[][] maze = Maze.generate(this); boolean[][] maze = Maze.generate(this);
boolean[] passable = new boolean[width()*height()]; boolean[] passable = new boolean[width()*height()];