Skip to content

Commit

Permalink
feat: added Dungeon Generator, closes #1130
Browse files Browse the repository at this point in the history
  • Loading branch information
keziah480 authored Mar 20, 2024
1 parent a9c8e04 commit 858fa5c
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 0 deletions.
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);
}
}
}
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;
}
}

0 comments on commit 858fa5c

Please sign in to comment.