From 21d16cf78a04bdb83cc5344e7646219cfa0640da Mon Sep 17 00:00:00 2001 From: keziah480 Date: Sun, 3 Mar 2024 23:09:51 +0000 Subject: [PATCH 1/2] + new DungeonCell class, extending Cell with dungeon specific data + new Dungeon class, capable of generating simple connected dungeons with a variety of circular and rectangular rooms --- .../fxgl/core/collection/grid/Dungeon.java | 90 +++++++++++++++++++ .../core/collection/grid/DungeonCell.java | 16 ++++ 2 files changed, 106 insertions(+) create mode 100644 fxgl-core/src/main/java/com/almasb/fxgl/core/collection/grid/Dungeon.java create mode 100644 fxgl-core/src/main/java/com/almasb/fxgl/core/collection/grid/DungeonCell.java diff --git a/fxgl-core/src/main/java/com/almasb/fxgl/core/collection/grid/Dungeon.java b/fxgl-core/src/main/java/com/almasb/fxgl/core/collection/grid/Dungeon.java new file mode 100644 index 000000000..a7bdb3c7f --- /dev/null +++ b/fxgl-core/src/main/java/com/almasb/fxgl/core/collection/grid/Dungeon.java @@ -0,0 +1,90 @@ +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); + } + } +} \ No newline at end of file diff --git a/fxgl-core/src/main/java/com/almasb/fxgl/core/collection/grid/DungeonCell.java b/fxgl-core/src/main/java/com/almasb/fxgl/core/collection/grid/DungeonCell.java new file mode 100644 index 000000000..9d1a17cd4 --- /dev/null +++ b/fxgl-core/src/main/java/com/almasb/fxgl/core/collection/grid/DungeonCell.java @@ -0,0 +1,16 @@ +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; + } +} \ No newline at end of file From 8e43da11ab74a90b5e4101874131060910a0f058 Mon Sep 17 00:00:00 2001 From: keziah480 Date: Mon, 18 Mar 2024 07:39:02 +0000 Subject: [PATCH 2/2] + new DungeonCell class, extending Cell with dungeon specific data + new Dungeon class, capable of generating simple connected dungeons with a variety of circular and rectangular rooms --- .../com/almasb/fxgl/core/collection/grid/Dungeon.java | 8 ++++++++ .../com/almasb/fxgl/core/collection/grid/DungeonCell.java | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/fxgl-core/src/main/java/com/almasb/fxgl/core/collection/grid/Dungeon.java b/fxgl-core/src/main/java/com/almasb/fxgl/core/collection/grid/Dungeon.java index a7bdb3c7f..99aa4c976 100644 --- a/fxgl-core/src/main/java/com/almasb/fxgl/core/collection/grid/Dungeon.java +++ b/fxgl-core/src/main/java/com/almasb/fxgl/core/collection/grid/Dungeon.java @@ -1,3 +1,11 @@ +/* + * 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; diff --git a/fxgl-core/src/main/java/com/almasb/fxgl/core/collection/grid/DungeonCell.java b/fxgl-core/src/main/java/com/almasb/fxgl/core/collection/grid/DungeonCell.java index 9d1a17cd4..566f59e9d 100644 --- a/fxgl-core/src/main/java/com/almasb/fxgl/core/collection/grid/DungeonCell.java +++ b/fxgl-core/src/main/java/com/almasb/fxgl/core/collection/grid/DungeonCell.java @@ -1,3 +1,11 @@ +/* + * 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;