-
-
Notifications
You must be signed in to change notification settings - Fork 559
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added Dungeon Generator, closes #1130
- Loading branch information
Showing
2 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
98 changes: 98 additions & 0 deletions
98
fxgl-core/src/main/java/com/almasb/fxgl/core/collection/grid/Dungeon.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* FXGL - JavaFX Game Library. The MIT License (MIT). | ||
* Copyright (c) AlmasB (almaslvl@gmail.com). | ||
* See LICENSE for details. | ||
*/ | ||
|
||
package com.almasb.fxgl.core.collection.grid; | ||
|
||
import java.util.Random; | ||
import static java.lang.Math.abs; | ||
import static java.lang.Math.sqrt; | ||
|
||
|
||
public class Dungeon { | ||
|
||
// Create tilemap of given height and width | ||
private DungeonCell[][] tileMap; | ||
|
||
private int[][] roomPositions = new int[20][2]; | ||
|
||
public void GenerateDungeon(int dungeonWidth, int dungeonHeight){ | ||
|
||
Random rand = new Random(); | ||
tileMap = new DungeonCell[dungeonWidth][dungeonHeight]; | ||
|
||
// Setup empty dungeon | ||
for (int i = 0; i < tileMap.length; i++){ | ||
for (int j = 0; j < tileMap[0].length; j++){ | ||
tileMap[i][j] = new DungeonCell(i, j); | ||
} | ||
} | ||
|
||
// Pick initial room positions | ||
for (int i = 0; i < roomPositions.length; i++){ | ||
roomPositions[i][0] = rand.nextInt(dungeonWidth); | ||
roomPositions[i][1] = rand.nextInt(dungeonHeight); | ||
} | ||
|
||
// Clear out rooms | ||
for (int i = 0; i < roomPositions.length; i++){ | ||
int subRooms = rand.nextInt(1) + 1; | ||
|
||
// Clear Circle | ||
if (rand.nextInt(3) == 0) { | ||
ClearCircle(roomPositions[i][0], roomPositions[i][1], rand.nextInt(3) + 1); | ||
} | ||
|
||
// Clear Rect | ||
else { | ||
ClearRect(roomPositions[i][0], roomPositions[i][1], rand.nextInt(3) + 4, rand.nextInt(3) + 4); | ||
} | ||
|
||
// Connect Rooms | ||
int connectRoom = rand.nextInt(roomPositions.length); | ||
|
||
ClearPath(roomPositions[i][0], roomPositions[i][1], roomPositions[connectRoom][0], roomPositions[connectRoom][1]); | ||
|
||
} | ||
} | ||
|
||
void ClearRect(int xPos, int yPos, int width, int height){ | ||
for (int i = 0; i < tileMap.length; i++){ | ||
for (int j = 0; j < tileMap[0].length; j++){ | ||
int xDis = abs(i - xPos); | ||
int yDis = abs(j - yPos); | ||
|
||
if (xDis <= width/2 && yDis <= height/2) { tileMap[i][j].SetType(0); } | ||
} | ||
} | ||
} | ||
|
||
void ClearCircle(int xPos, int yPos, int radius){ | ||
for (int i = 0; i < tileMap.length; i++){ | ||
for (int j = 0; j < tileMap[0].length; j++){ | ||
int xDis = abs(i - xPos); | ||
int yDis = abs(j - yPos); | ||
|
||
double tileDis = sqrt(xDis*xDis + yDis*yDis); | ||
if (tileDis <= radius) { tileMap[i][j].SetType(0); } | ||
} | ||
} | ||
} | ||
|
||
void ClearPath(int xStart, int yStart, int xEnd, int yEnd){ | ||
int[] clearPos = new int[2]; | ||
clearPos[0] = xStart; | ||
clearPos[1] = yStart; | ||
|
||
while (clearPos[0] != xEnd || clearPos[1] != yEnd){ | ||
if (clearPos[0] < xEnd) clearPos[0]++; | ||
else if (clearPos[0] > xEnd) clearPos[0]--; | ||
else if (clearPos[1] < yEnd) clearPos[1]++; | ||
else if (clearPos[1] > yEnd) clearPos[1]--; | ||
|
||
tileMap[clearPos[0]][clearPos[1]].SetType(0); | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
fxgl-core/src/main/java/com/almasb/fxgl/core/collection/grid/DungeonCell.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* FXGL - JavaFX Game Library. The MIT License (MIT). | ||
* Copyright (c) AlmasB (almaslvl@gmail.com). | ||
* See LICENSE for details. | ||
*/ | ||
|
||
package com.almasb.fxgl.core.collection.grid; | ||
|
||
public class DungeonCell extends Cell { | ||
private int cellType; | ||
|
||
public DungeonCell(int x, int y) { | ||
super(x, y); | ||
cellType = 1; | ||
} | ||
|
||
public void SetType(int type){ | ||
cellType = type; | ||
} | ||
|
||
public int GetType(){ | ||
return cellType; | ||
} | ||
} |