Skip to content
This repository was archived by the owner on Oct 23, 2023. It is now read-only.

Commit ea8647c

Browse files
committed
smoothed out floors
1 parent ed5391a commit ea8647c

5 files changed

Lines changed: 105 additions & 39 deletions

File tree

src/display/Bitmap.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ public class Bitmap extends JPanel {
1717
final int offsetX = 0, offsetY = 0;
1818
int fieldSize = 5, padding = 0, rectSize = fieldSize - padding;
1919

20-
private Color[] color = { Color.decode("#A1D490"), Color.decode("#D4B790"), Color.decode("#801B1B"),
20+
private Color[] color = { Color.decode("#454545"), Color.decode("#A1D490"), Color.decode("#D4B790"),
21+
Color.decode("#B39B7B"), Color.decode("#801B1B"), Color.decode("#000000") };
2122

2223
public void setRoom(Map map) {
2324
this.map = map;

src/dungeon/Cell.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,30 @@ public class Cell {
55
private int x, y;
66
private Ground ground;
77

8+
/**
9+
* public constructor, default ground is wall (no passing)
10+
* @param x
11+
* @param y
12+
*/
813
public Cell(int x, int y) {
914
this.x = x;
1015
this.y = y;
1116
this.ground = Ground.WALL;
1217
}
1318

14-
public void set(Ground ground) {
19+
/**
20+
* setter method for ground state
21+
* @param ground
22+
*/
23+
public void setGround(Ground ground) {
1524
this.ground = ground;
1625
}
1726

18-
public Ground get() {
27+
/**
28+
* getter method for ground state
29+
* @return
30+
*/
31+
public Ground getGround() {
1932
return ground;
2033
}
2134
}

src/dungeon/Generator.java

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ public class Generator {
2323
// constants
2424

2525
final int ROOM_LIMIT = 300;
26-
final int[][] neighsAll = new int[][] { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, 1 } };
27-
final int[][] neighsOdd = new int[][] { { 2, 0 }, { -2, 0 }, { 0, 2 }, { 0, 2 } };
26+
final int[][] neighsAll = new int[][] { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } };
27+
final int[][] neighsOdd = new int[][] { { 2, 0 }, { -2, 0 }, { 0, 2 }, { 0, -2 } };
2828

2929
/**
3030
* constructor for generate levels with some fixed size
@@ -53,7 +53,8 @@ public Map newLevel() {
5353
// 4 general steps to level generation
5454
placeRooms();
5555
placeMaze();
56-
removeDeadends(5);
56+
removeDeadends();
57+
plazeLoops();
5758

5859
return map;
5960
}
@@ -94,7 +95,7 @@ private void placeRooms() {
9495
}
9596

9697
// place room
97-
map.newRoom(xStart, xLen, yStart, yLen);
98+
map.setNewRoom(xStart, xLen, yStart, yLen);
9899

99100
// insert room into memory
100101
roomTable[roomNum] = new int[] { xStart, xLen, yStart, yLen };
@@ -235,37 +236,56 @@ private void connectRoom(int xStart, int xLen, int yStart, int yLen) {
235236
break;
236237
}
237238
int l = rand.nextInt(k);
238-
map.setGround(candidates[l][0], candidates[l][1], CONNECTOR);
239+
map.setGround(candidates[l][0], candidates[l][1], FLOOR);
239240
} while (rand.nextDouble() < 0.3);
240241
}
241242

242243
/**
243244
* internal function to remove the dead ends of the maze
244245
*/
245-
@SuppressWarnings("unused")
246-
private void removeDeadends(int iter) {
247-
// declarations
248-
Queue<int[]> q = new ArrayDeque<int[]>();
249-
int cur[], perm[], neigh[][];
250-
251-
// push every cell on stack
252-
// because connectors cannot be a dead end
253-
for (int i = 0; i < n; i += 1) {
254-
for (int j = 0; j < m; j += 1) {
255-
if (map.getGround(i, j) != WALL) {
256-
q.add(new int[] { i, j });
246+
private void removeDeadends() {
247+
int count;
248+
boolean repeat = true, deadend[][];
249+
250+
while (repeat) {
251+
// fresh inits for single execution of elemination
252+
deadend = new boolean[n][m];
253+
repeat = false;
254+
255+
// dead end iff 3 neighbours are walls
256+
for (int x = 0; x < n; x++) {
257+
for (int y = 0; y < m; y++) {
258+
if (map.getGround(x, y) == WALL) {
259+
continue;
260+
}
261+
262+
count = 0;
263+
for (int j = 0; j < 4; j++) {
264+
if (map.getGround(x + neighsAll[j][0], y + neighsAll[j][1]) == WALL)
265+
count++;
266+
}
267+
268+
if (count == 3) {
269+
deadend[x][y] = true;
270+
repeat = true;
271+
}
257272
}
258273
}
259-
}
260-
q.add(new int[] { -1, -1 });
261-
262-
// remove if dead end and push the neighbour on stack which might be a
263-
// dead end now
264-
while (!q.isEmpty()) {
265-
cur = q.poll();
266274

275+
// remove dead ends
276+
for (int x = 0; x < n; x++) {
277+
for (int y = 0; y < m; y++) {
278+
if (deadend[x][y])
279+
map.setGround(x, y, WALL);
280+
}
281+
}
267282
}
268-
269283
}
270284

285+
/**
286+
* internal function to create loops inside the map
287+
*/
288+
private void plazeLoops() {
289+
290+
}
271291
}

src/dungeon/Ground.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
public enum Ground {
44
WALL,
55
FLOOR,
6-
CONNECTOR,
76
DOOR,
87
ROOM,
98
ERROR

src/dungeon/Map.java

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,43 +20,76 @@ public Map(int n, int m) {
2020
for (int i = 0; i < n; i++) {
2121
for (int j = 0; j < m; j++) {
2222
map[i][j] = new Cell(i, j);
23-
map[i][j].set(WALL);
23+
map[i][j].setGround(WALL);
2424
}
2525
}
2626
}
27-
27+
28+
/**
29+
* getter method for length n
30+
* @return n
31+
*/
2832
public int getN() {
2933
return n;
3034
}
31-
35+
36+
/**
37+
* getter method for length m
38+
* @return m
39+
*/
3240
public int getM() {
3341
return m;
3442
}
3543

44+
/**
45+
* setter method for cell
46+
* @param x coordinate
47+
* @param y coordinate
48+
* @return
49+
*/
3650
public Cell getCell(int x, int y) {
3751
return map[x][y];
3852
}
39-
53+
54+
/**
55+
* getther method for ground state
56+
* @param x coordinate
57+
* @param y coordinate
58+
* @return
59+
*/
4060
public Ground getGround(int x, int y) {
4161
if (x < 0 || x >= n || y < 0 || y >= m) {
4262
return ERROR;
4363
}
4464

45-
return map[x][y].get();
65+
return map[x][y].getGround();
4666
}
47-
67+
68+
/**
69+
* setter method for ground state
70+
* @param x
71+
* @param y
72+
* @param ground
73+
*/
4874
public void setGround(int x, int y, Ground ground) {
4975
if (x < 0 || x >= n || y < 0 || y >= m) {
5076
return;
5177
}
5278

53-
map[x][y].set(ground);
79+
map[x][y].setGround(ground);
5480
}
55-
56-
public void newRoom(int xStart, int xLen, int yStart, int yLen) {
81+
82+
/**
83+
* setter method for a new room on the map object
84+
* @param xStart
85+
* @param xLen
86+
* @param yStart
87+
* @param yLen
88+
*/
89+
public void setNewRoom(int xStart, int xLen, int yStart, int yLen) {
5790
for (int x = xStart; x < xStart + xLen; x++) {
5891
for (int y = yStart; y < yStart + yLen; y++) {
59-
map[x][y].set(ROOM);
92+
map[x][y].setGround(ROOM);
6093
}
6194
}
6295
}

0 commit comments

Comments
 (0)