Skip to content

Commit 858fa5c

Browse files
authored
feat: added Dungeon Generator, closes #1130
1 parent a9c8e04 commit 858fa5c

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* FXGL - JavaFX Game Library. The MIT License (MIT).
3+
* Copyright (c) AlmasB (almaslvl@gmail.com).
4+
* See LICENSE for details.
5+
*/
6+
7+
package com.almasb.fxgl.core.collection.grid;
8+
9+
import java.util.Random;
10+
import static java.lang.Math.abs;
11+
import static java.lang.Math.sqrt;
12+
13+
14+
public class Dungeon {
15+
16+
// Create tilemap of given height and width
17+
private DungeonCell[][] tileMap;
18+
19+
private int[][] roomPositions = new int[20][2];
20+
21+
public void GenerateDungeon(int dungeonWidth, int dungeonHeight){
22+
23+
Random rand = new Random();
24+
tileMap = new DungeonCell[dungeonWidth][dungeonHeight];
25+
26+
// Setup empty dungeon
27+
for (int i = 0; i < tileMap.length; i++){
28+
for (int j = 0; j < tileMap[0].length; j++){
29+
tileMap[i][j] = new DungeonCell(i, j);
30+
}
31+
}
32+
33+
// Pick initial room positions
34+
for (int i = 0; i < roomPositions.length; i++){
35+
roomPositions[i][0] = rand.nextInt(dungeonWidth);
36+
roomPositions[i][1] = rand.nextInt(dungeonHeight);
37+
}
38+
39+
// Clear out rooms
40+
for (int i = 0; i < roomPositions.length; i++){
41+
int subRooms = rand.nextInt(1) + 1;
42+
43+
// Clear Circle
44+
if (rand.nextInt(3) == 0) {
45+
ClearCircle(roomPositions[i][0], roomPositions[i][1], rand.nextInt(3) + 1);
46+
}
47+
48+
// Clear Rect
49+
else {
50+
ClearRect(roomPositions[i][0], roomPositions[i][1], rand.nextInt(3) + 4, rand.nextInt(3) + 4);
51+
}
52+
53+
// Connect Rooms
54+
int connectRoom = rand.nextInt(roomPositions.length);
55+
56+
ClearPath(roomPositions[i][0], roomPositions[i][1], roomPositions[connectRoom][0], roomPositions[connectRoom][1]);
57+
58+
}
59+
}
60+
61+
void ClearRect(int xPos, int yPos, int width, int height){
62+
for (int i = 0; i < tileMap.length; i++){
63+
for (int j = 0; j < tileMap[0].length; j++){
64+
int xDis = abs(i - xPos);
65+
int yDis = abs(j - yPos);
66+
67+
if (xDis <= width/2 && yDis <= height/2) { tileMap[i][j].SetType(0); }
68+
}
69+
}
70+
}
71+
72+
void ClearCircle(int xPos, int yPos, int radius){
73+
for (int i = 0; i < tileMap.length; i++){
74+
for (int j = 0; j < tileMap[0].length; j++){
75+
int xDis = abs(i - xPos);
76+
int yDis = abs(j - yPos);
77+
78+
double tileDis = sqrt(xDis*xDis + yDis*yDis);
79+
if (tileDis <= radius) { tileMap[i][j].SetType(0); }
80+
}
81+
}
82+
}
83+
84+
void ClearPath(int xStart, int yStart, int xEnd, int yEnd){
85+
int[] clearPos = new int[2];
86+
clearPos[0] = xStart;
87+
clearPos[1] = yStart;
88+
89+
while (clearPos[0] != xEnd || clearPos[1] != yEnd){
90+
if (clearPos[0] < xEnd) clearPos[0]++;
91+
else if (clearPos[0] > xEnd) clearPos[0]--;
92+
else if (clearPos[1] < yEnd) clearPos[1]++;
93+
else if (clearPos[1] > yEnd) clearPos[1]--;
94+
95+
tileMap[clearPos[0]][clearPos[1]].SetType(0);
96+
}
97+
}
98+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* FXGL - JavaFX Game Library. The MIT License (MIT).
3+
* Copyright (c) AlmasB (almaslvl@gmail.com).
4+
* See LICENSE for details.
5+
*/
6+
7+
package com.almasb.fxgl.core.collection.grid;
8+
9+
public class DungeonCell extends Cell {
10+
private int cellType;
11+
12+
public DungeonCell(int x, int y) {
13+
super(x, y);
14+
cellType = 1;
15+
}
16+
17+
public void SetType(int type){
18+
cellType = type;
19+
}
20+
21+
public int GetType(){
22+
return cellType;
23+
}
24+
}

0 commit comments

Comments
 (0)