Dungeon generation

This commit is contained in:
Marco 2025-04-22 18:21:53 +02:00
commit c3107fbce9
9 changed files with 494 additions and 130 deletions

View file

@ -13,6 +13,7 @@
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AGameManager_005FScriptMethods_002Egenerated_002Ecs_002Fl_003AC_0021_003FUsers_003FMaddo_003FAppData_003FLocal_003FJetBrains_003FShared_003FvAny_003FSourcesCache_003F642294f86639b2f8d4cb4d14e791d697e55369_003FGameManager_005FScriptMethods_002Egenerated_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AGameManager_005FScriptMethods_002Egenerated_002Ecs_002Fl_003AC_0021_003FUsers_003FMaddo_003FAppData_003FLocal_003FJetBrains_003FShared_003FvAny_003FSourcesCache_003F642294f86639b2f8d4cb4d14e791d697e55369_003FGameManager_005FScriptMethods_002Egenerated_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AInputEvent_002Ecs_002Fl_003AC_0021_003FUsers_003FMaddo_003FAppData_003FLocal_003FJetBrains_003FShared_003FvAny_003FSourcesCache_003F10e1ba49df151bbaf6853f9834601cecebdeade8465a9adb366b4a7b7c9524b0_003FInputEvent_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AInputEvent_002Ecs_002Fl_003AC_0021_003FUsers_003FMaddo_003FAppData_003FLocal_003FJetBrains_003FShared_003FvAny_003FSourcesCache_003F10e1ba49df151bbaf6853f9834601cecebdeade8465a9adb366b4a7b7c9524b0_003FInputEvent_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AInput_002Ecs_002Fl_003AC_0021_003FUsers_003FMaddo_003FAppData_003FLocal_003FJetBrains_003FShared_003FvAny_003FSourcesCache_003F3e43a674e8a96aa8b4abdf97a31f18732060401d8285a17ee7109827a69cd18b_003FInput_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AInput_002Ecs_002Fl_003AC_0021_003FUsers_003FMaddo_003FAppData_003FLocal_003FJetBrains_003FShared_003FvAny_003FSourcesCache_003F3e43a674e8a96aa8b4abdf97a31f18732060401d8285a17ee7109827a69cd18b_003FInput_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AList_002Ecs_002Fl_003AC_0021_003FUsers_003FMaddo_003FAppData_003FLocal_003FJetBrains_003FShared_003FvAny_003FSourcesCache_003F1b81cb3be224213a6a73519b6e340a628d9a1fb8629c351a186a26f6376669_003FList_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ANativeCalls_002Ecs_002Fl_003AC_0021_003FUsers_003FMaddo_003FAppData_003FLocal_003FJetBrains_003FShared_003FvAny_003FSourcesCache_003F4178b6f96a2e273c05bd023e0a7f4ff5c747b218af487b1ac979f58bafb30d_003FNativeCalls_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ANativeCalls_002Ecs_002Fl_003AC_0021_003FUsers_003FMaddo_003FAppData_003FLocal_003FJetBrains_003FShared_003FvAny_003FSourcesCache_003F4178b6f96a2e273c05bd023e0a7f4ff5c747b218af487b1ac979f58bafb30d_003FNativeCalls_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ANode2D_002Ecs_002Fl_003AC_0021_003FUsers_003FMaddo_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Fe37dc1faf08a4d5ea030ad59bdf77522523400_003F49_003F8f2ede17_003FNode2D_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ANode2D_002Ecs_002Fl_003AC_0021_003FUsers_003FMaddo_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Fe37dc1faf08a4d5ea030ad59bdf77522523400_003F49_003F8f2ede17_003FNode2D_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ANode_002Ecs_002Fl_003AC_0021_003FUsers_003FMaddo_003FAppData_003FLocal_003FJetBrains_003FShared_003FvAny_003FSourcesCache_003Fbb1b701f3c7411227a9d2e09f965d857ff3e771557650c4f513e427d77c_003FNode_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ANode_002Ecs_002Fl_003AC_0021_003FUsers_003FMaddo_003FAppData_003FLocal_003FJetBrains_003FShared_003FvAny_003FSourcesCache_003Fbb1b701f3c7411227a9d2e09f965d857ff3e771557650c4f513e427d77c_003FNode_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>

View file

@ -0,0 +1,18 @@
[gd_resource type="Resource" script_class="RogueliteRoomResource" load_steps=6 format=3 uid="uid://char0yv78vtty"]
[ext_resource type="Resource" uid="uid://cocl3qontm3be" path="res://Resources/Enemies/Base_Fairy.tres" id="1_w1ns8"]
[ext_resource type="Resource" uid="uid://cqfyuurvqb8m6" path="res://Resources/Enemies/Base_Fairy_Special.tres" id="2_gohkc"]
[ext_resource type="Resource" uid="uid://qbo6avc7x64b" path="res://Resources/Enemies/Fairy_Guard.tres" id="3_x42ya"]
[ext_resource type="Resource" uid="uid://cfdvg162u65sr" path="res://Resources/Enemies/Thermathron.tres" id="4_qo81h"]
[ext_resource type="Script" uid="uid://bl2ne8w12e3a" path="res://Scripts/Resources/Roguelite/RogueliteRoomResource.cs" id="5_570h4"]
[resource]
script = ExtResource("5_570h4")
RoomName = &"BigTest"
Type = 1
ScenePath = &"uid://cglddtyl6o4fn"
Size = Vector2i(2, 2)
DoorGridPositions = Array[Vector2i]([Vector2i(0, -1), Vector2i(0, 1), Vector2i(1, 0), Vector2i(-1, 0)])
SpawnableEnemies = Array[Object]([ExtResource("1_w1ns8"), ExtResource("2_gohkc"), ExtResource("3_x42ya"), ExtResource("4_qo81h")])
DoorDirections = 30
metadata/_custom_type_script = "uid://bl2ne8w12e3a"

View file

@ -0,0 +1,18 @@
[gd_resource type="Resource" script_class="RogueliteRoomResource" load_steps=6 format=3 uid="uid://cb7gk278lmicd"]
[ext_resource type="Resource" uid="uid://cocl3qontm3be" path="res://Resources/Enemies/Base_Fairy.tres" id="1_85eyw"]
[ext_resource type="Resource" uid="uid://cqfyuurvqb8m6" path="res://Resources/Enemies/Base_Fairy_Special.tres" id="2_dduxp"]
[ext_resource type="Resource" uid="uid://qbo6avc7x64b" path="res://Resources/Enemies/Fairy_Guard.tres" id="3_7y8b1"]
[ext_resource type="Resource" uid="uid://cfdvg162u65sr" path="res://Resources/Enemies/Thermathron.tres" id="4_wwjbl"]
[ext_resource type="Script" uid="uid://bl2ne8w12e3a" path="res://Scripts/Resources/Roguelite/RogueliteRoomResource.cs" id="5_qpk7q"]
[resource]
script = ExtResource("5_qpk7q")
RoomName = &"TestMapLab 2x1"
Type = 1
ScenePath = &"uid://bon3ay27r54tw"
Size = Vector2i(2, 1)
DoorGridPositions = Array[Vector2i]([Vector2i(0, -1), Vector2i(0, 1), Vector2i(1, 0), Vector2i(-1, 0)])
SpawnableEnemies = Array[Object]([ExtResource("1_85eyw"), ExtResource("2_dduxp"), ExtResource("3_7y8b1"), ExtResource("4_wwjbl")])
DoorDirections = 30
metadata/_custom_type_script = "uid://bl2ne8w12e3a"

View file

@ -1,4 +1,4 @@
[gd_scene load_steps=28 format=3 uid="uid://bf1kqr3o6r6d4"] [gd_scene load_steps=30 format=3 uid="uid://bf1kqr3o6r6d4"]
[ext_resource type="Script" uid="uid://doxmbokehw8ci" path="res://Scripts/GameManager.cs" id="1_wbqvu"] [ext_resource type="Script" uid="uid://doxmbokehw8ci" path="res://Scripts/GameManager.cs" id="1_wbqvu"]
[ext_resource type="PackedScene" uid="uid://c4pr2707hbeph" path="res://Scenes/Actors/fsm_player.tscn" id="2_3fyis"] [ext_resource type="PackedScene" uid="uid://c4pr2707hbeph" path="res://Scenes/Actors/fsm_player.tscn" id="2_3fyis"]
@ -27,6 +27,8 @@
[ext_resource type="Resource" uid="uid://cwtma7mxged8a" path="res://Resources/RogueliteMaps/ShopRoom1.tres" id="18_2lxq3"] [ext_resource type="Resource" uid="uid://cwtma7mxged8a" path="res://Resources/RogueliteMaps/ShopRoom1.tres" id="18_2lxq3"]
[ext_resource type="Resource" uid="uid://crqgvauqarfaq" path="res://Resources/RogueliteMaps/TreasureRoom1.tres" id="19_6ahuq"] [ext_resource type="Resource" uid="uid://crqgvauqarfaq" path="res://Resources/RogueliteMaps/TreasureRoom1.tres" id="19_6ahuq"]
[ext_resource type="Resource" uid="uid://bo4efv7rwowuh" path="res://Resources/RogueliteMaps/SecretRoom1.tres" id="20_xrp0h"] [ext_resource type="Resource" uid="uid://bo4efv7rwowuh" path="res://Resources/RogueliteMaps/SecretRoom1.tres" id="20_xrp0h"]
[ext_resource type="Resource" uid="uid://cb7gk278lmicd" path="res://Resources/RogueliteMaps/Lab2x1_1.tres" id="21_2lxq3"]
[ext_resource type="Resource" uid="uid://char0yv78vtty" path="res://Resources/RogueliteMaps/BigTest2.tres" id="22_6ahuq"]
[node name="GameScene" type="Node2D"] [node name="GameScene" type="Node2D"]
process_mode = 3 process_mode = 3
@ -41,10 +43,10 @@ SpawnMarkers = Dictionary[int, NodePath]({
process_mode = 1 process_mode = 1
y_sort_enabled = true y_sort_enabled = true
script = ExtResource("4_jtlua") script = ExtResource("4_jtlua")
Rooms = Array[Object]([ExtResource("5_gwtv6"), ExtResource("6_gwtv6"), ExtResource("7_wbqvu"), ExtResource("8_3fyis"), ExtResource("9_go1yg"), ExtResource("5_pfafs"), ExtResource("11_68lig"), ExtResource("12_83bvc"), ExtResource("13_y651a"), ExtResource("14_vhvs2"), ExtResource("15_6gk3e"), ExtResource("16_4gy5m"), ExtResource("17_td7hx"), ExtResource("18_2lxq3"), ExtResource("19_6ahuq"), ExtResource("20_xrp0h")]) Rooms = Array[Object]([ExtResource("5_gwtv6"), ExtResource("6_gwtv6"), ExtResource("7_wbqvu"), ExtResource("8_3fyis"), ExtResource("9_go1yg"), ExtResource("5_pfafs"), ExtResource("11_68lig"), ExtResource("12_83bvc"), ExtResource("13_y651a"), ExtResource("14_vhvs2"), ExtResource("15_6gk3e"), ExtResource("16_4gy5m"), ExtResource("17_td7hx"), ExtResource("18_2lxq3"), ExtResource("19_6ahuq"), ExtResource("20_xrp0h"), ExtResource("21_2lxq3"), ExtResource("22_6ahuq")])
DungeonLength = 6 DungeonLength = 4
MaxBranchLength = 2 MaxBranchLength = 2
Seed = 4 Seed = 1
[node name="CameraController" type="Camera2D" parent="."] [node name="CameraController" type="Camera2D" parent="."]
process_mode = 1 process_mode = 1

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Cirno.Scripts.Components.FSM.Enemy; using Cirno.Scripts.Components.FSM.Enemy;
using Cirno.Scripts.Enums;
using Cirno.Scripts.Resources; using Cirno.Scripts.Resources;
using Cirno.Scripts.Resources.Roguelite; using Cirno.Scripts.Resources.Roguelite;
using Godot; using Godot;
@ -22,6 +23,19 @@ public partial class RogueliteRoom : Node2D
return BottomLeft + new Vector2I(GD.RandRange(0, RoomResource.Size.X - 1), 0); return BottomLeft + new Vector2I(GD.RandRange(0, RoomResource.Size.X - 1), 0);
} }
public Vector2I RandomExit(Direction direction)
{
return direction switch
{
Direction.Up => GridPosition + new Vector2I(GD.RandRange(0, RoomResource.Size.X - 1), 0),
Direction.Down => BottomLeft + new Vector2I(GD.RandRange(0, RoomResource.Size.X - 1), 0),
Direction.Left => GridPosition + new Vector2I(0, GD.RandRange(0, RoomResource.Size.Y - 1)),
Direction.Right => GridPosition + new Vector2I(RoomResource.Size.X - 1, 0) +
new Vector2I(0, GD.RandRange(0, RoomResource.Size.Y - 1)),
_ => throw new ArgumentOutOfRangeException(nameof(direction), direction, null)
};
}
[Export] public PackedScene DoorPrefab { get; private set; } [Export] public PackedScene DoorPrefab { get; private set; }
[Export] public PackedScene WallPrefab { get; private set; } [Export] public PackedScene WallPrefab { get; private set; }

View file

@ -6,6 +6,7 @@ using Cirno.Scripts.Resources.Roguelite;
using Cirno.Scripts.Utils; using Cirno.Scripts.Utils;
using Godot; using Godot;
using Godot.Collections; using Godot.Collections;
using Range = System.Range;
namespace Cirno.Scripts.Controllers; namespace Cirno.Scripts.Controllers;
@ -31,6 +32,14 @@ public partial class RogueliteRoomManager : Node2D
[Export] public int DungeonWidth = 10; [Export] public int DungeonWidth = 10;
[Export] public int DungeonHeight = 10; [Export] public int DungeonHeight = 10;
[Export] public int MaxRooms = 12; [Export] public int MaxRooms = 12;
[Export] public int MinKeys = 0;
[Export] public int MaxKeys = 3;
[Export] public int MinSecrets = 1;
[Export] public int MaxSecrets = 2;
[Export] public int MinTreasures = 1;
[Export] public int MaxTreasures = 2;
[Export] public int MinShops = 1;
[Export] public int MaxShops = 1;
[Export] public ulong Seed = 0; [Export] public ulong Seed = 0;
[Export] public Vector2I TileSize { get; set; } = new Vector2I(16, 16); [Export] public Vector2I TileSize { get; set; } = new Vector2I(16, 16);
@ -94,82 +103,42 @@ public partial class RogueliteRoomManager : Node2D
var starterRoom = Rooms.Where(r => r.Type == RoomType.Starter).PickRandom(); var starterRoom = Rooms.Where(r => r.Type == RoomType.Starter).PickRandom();
SpawnRoom(starterRoom, origin, out var spawnedBeginRoom); SpawnRoom(starterRoom, origin, out var spawnedBeginRoom);
var randomRoomsList = RegularRooms.Shuffle(DungeonLength).ToList(); var randomRoomsList = RegularRooms.Shuffle(MaxRooms * 4).ToList();
var randomOffshootRoomsList = OffshootRooms.Shuffle(MaxBranchLength).ToList(); var randomOffshootRoomsList = OffshootRooms.Shuffle(DungeonLength * 4).ToList();
var currentPos = spawnedBeginRoom.RandomBottomExit(); var currentPos = spawnedBeginRoom.RandomBottomExit();
_connections.Add(new RoomConnection(origin, currentPos + new Vector2I(0, 1))); _connections.Add(new RoomConnection(origin, currentPos + new Vector2I(0, 1)));
Vector2I nextPos; Vector2I nextPos;
var offshoots = new List<RoomType>() var offshoots = new List<RoomType>();
{
RoomType.Treasure, AddRandomOffshootType(offshoots, MinKeys, MaxKeys, RoomType.Key);
RoomType.Secret, AddRandomOffshootType(offshoots, MinShops, MaxShops, RoomType.Shop);
RoomType.Shop, AddRandomOffshootType(offshoots, MinSecrets, MaxSecrets, RoomType.Secret);
RoomType.Key, AddRandomOffshootType(offshoots, MinTreasures, MaxTreasures, RoomType.Treasure);
RoomType.Regular
};
var shuffledOffshoots = offshoots.Shuffle().ToList(); var shuffledOffshoots = offshoots.Shuffle().ToList();
int currentOffshoot = 0; int currentOffshoot = 0;
RogueliteRoom lastRoom = spawnedBeginRoom;
for (int i = 0; i < DungeonLength; i++) for (int i = 0; i < DungeonLength; i++)
{ {
GD.Print($"Dungeon room {i}"); GD.Print($"Dungeon room {i}");
var spawnedRoom = TrySpawnRoom(randomRoomsList, currentPos); var randRoomStartIndex = SpawnedRooms.Count(x => x.RoomResource.Type is RoomType.Regular && x.RoomResource.HasDoors(DoorDirections.North | DoorDirections.South));
var spawnedRoom = TrySpawnRoom(randomRoomsList.Take(new Range(randRoomStartIndex, randomRoomsList.Count - 1)).ToList(),
lastRoom, Direction.Down);
if (spawnedRoom is null) if (spawnedRoom is null)
{ {
continue; continue;
} }
// lastRoom = spawnedRoom;
// nextPos = currentPos + new Vector2I(0, 1);
//
// var roomToSpawn = randomRoomsList[randRoomIndex % randomRoomsList.Count];
//
// var spawnedRoom = TrySpawnRoom(roomToSpawn, nextPos);
// if (spawnedRoom is null)
// {
// // Failed, try another room
// DungeonLength += 1;
// randRoomIndex++;
// continue;
// }
//
//
// var offset = 0;
//
// // Place it at a random X position
// if (roomToSpawn.Size.X > 1)
// {
// offset -= GD.RandRange(0, roomToSpawn.Size.X - 1);
// }
//
// nextPos += new Vector2I(offset, 0);
//
// //var posWithOffset = nextPos + new Vector2I(offset, 0);
//
// GD.Print($"Spawning room at {nextPos}");
// if (!SpawnRoom(roomToSpawn, nextPos, out var spawnedRoom))
// {
// GD.PrintErr($"Could not spawn room {roomToSpawn} at {nextPos}");
// //DungeonLength += 1;
// continue;
// }
nextPos = spawnedRoom.RandomBottomExit();
GD.Print($"Next pos is now: {nextPos}");
// nextPos is now the end of the room at the current exit
_connections.Add(new RoomConnection(nextPos, nextPos + new Vector2I(0, 1)));
currentPos = nextPos;
// Spawn offshoot here // Spawn offshoot here
@ -181,78 +150,50 @@ public partial class RogueliteRoomManager : Node2D
continue; continue;
} }
int roomsInOffshot = GD.RandRange(0, MaxBranchLength);
var leftPosition = spawnedRoom.GridPosition;
// Roll whether to go left or right, if direction is full go the other, if both are full do not spawn // Roll whether to go left or right, if direction is full go the other, if both are full do not spawn
for (int j = 0; j < roomsInOffshot; j++) var directions = new List<Direction>();
if (lastRoom.RoomResource.DoorDirections.HasFlag(DoorDirections.East)) directions.Add(Direction.Right);
if (lastRoom.RoomResource.DoorDirections.HasFlag(DoorDirections.West)) directions.Add(Direction.Left);
directions = directions.Shuffle().ToList();
var randOffshootStartIndex = SpawnedRooms.Count(x => x.RoomResource.Type is RoomType.Regular && x.RoomResource.HasDoors(DoorDirections.West | DoorDirections.East));
foreach (var direction in directions)
{ {
var shuffledOffshootRoomsList = randomOffshootRoomsList.Shuffle().ToList(); int roomsInOffshot = offshootTypeToSpawn is RoomType.Secret or RoomType.Shop
? 0
: GD.RandRange(0, MaxBranchLength);
var directions = new List<Vector2I>() { Vector2I.Left, Vector2I.Right }.Shuffle(); var roomsForOffshoot = randomOffshootRoomsList
.Take(new Range(randOffshootStartIndex, randomOffshootRoomsList.Count - 1)).ToList();
foreach (var shuffledOffshoot in shuffledOffshootRoomsList) var res = SpawnOffshoot(lastRoom, direction, offshootTypeToSpawn, roomsInOffshot, roomsForOffshoot);
if (res)
{ {
var offshootCoord = leftPosition - new Vector2I(shuffledOffshoot.Size.X, 0);
GD.Print("Spawning side room");
var spawned = SpawnRoom(shuffledOffshoot, offshootCoord, out var spawnedOffshoot);
if (!spawned)
{
GD.Print($"Could not place room {shuffledOffshoot} at {offshootCoord}");
// Try next in list
continue;
}
_connections.Add(new RoomConnection(leftPosition,
offshootCoord + new Vector2I(shuffledOffshoot.Size.X - 1, 0)));
leftPosition = offshootCoord;
// Stop because we spawned the room we needed to
currentOffshoot++; currentOffshoot++;
break; break;
} }
} }
// Offshoot over // Add more dungeon if not enough rooms are generated
if (i == DungeonLength - 1 && DungeonLength < MaxRooms && currentOffshoot < DungeonLength)
// Spawn final room {
var finalRoomToSpawn = Rooms.Where(x => x.Type == offshootTypeToSpawn).PickRandom(); DungeonLength++;
}
var finalRoomCoord = leftPosition - new Vector2I(finalRoomToSpawn.Size.X, 0);
SpawnRoom(finalRoomToSpawn, finalRoomCoord, out var spawnedFinalRoom);
_connections.Add(new RoomConnection(leftPosition,
finalRoomCoord + new Vector2I(finalRoomToSpawn.Size.X - 1, 0)));
leftPosition = finalRoomCoord;
// Done with last offshoot room
} }
nextPos = currentPos + new Vector2I(0, 1);
GD.Print($"Final nextpos is {nextPos}");
var bossRoom = BossRooms.PickRandom(); var bossRoom = BossRooms.PickRandom();
if (SpawnRoom(bossRoom, nextPos, out var spawnedBossRoom)) var spawnedBossRoom = TrySpawnRoom(BossRooms.ToList(), lastRoom, Direction.Down);
{
if (bossRoom.Size.Y > 1) if (spawnedBossRoom is null)
{
nextPos += new Vector2I(0, bossRoom.Size.Y - 1);
}
_connections.Add(new RoomConnection(currentPos, nextPos));
}
else
{ {
GD.PrintErr($"Could not spawn boss room {bossRoom} at {nextPos}"); GD.PrintErr($"Could not spawn boss room {bossRoom}");
} }
foreach (var room in SpawnedRooms) foreach (var room in SpawnedRooms)
@ -271,30 +212,57 @@ public partial class RogueliteRoomManager : Node2D
//CallDeferred(MethodName.OpenStartDoorsDeferred, spawnedBeginRoom); //CallDeferred(MethodName.OpenStartDoorsDeferred, spawnedBeginRoom);
} }
private RogueliteRoom TrySpawnRoom(List<RogueliteRoomResource> roomsList, Vector2I originPos) private RogueliteRoom TrySpawnRoom(List<RogueliteRoomResource> roomsList, RogueliteRoom lastRoom,
Direction direction)
{ {
var nextPos = originPos + new Vector2I(0, 1); //var nextPos = originPos + new Vector2I(0, 1);
var rooms = roomsList.Shuffle().ToList();
Vector2I exitPosition = lastRoom.RandomExit(direction);
Vector2I nextPos = direction switch
{
Direction.Up => exitPosition + Vector2I.Up,
Direction.Down => exitPosition + Vector2I.Down,
Direction.Left => exitPosition + Vector2I.Left,
Direction.Right => exitPosition + Vector2I.Right,
_ => exitPosition
};
var rooms = roomsList; //.Shuffle().ToList();
for (int i = 0; i < rooms.Count(); i++) for (int i = 0; i < rooms.Count(); i++)
{ {
var roomToSpawn = rooms[i % rooms.Count()]; var roomToSpawn = rooms[i % rooms.Count()];
var spawnedRoom = TrySpawnRoom(roomToSpawn, nextPos); var spawnedRoom = TrySpawnRoom(roomToSpawn, nextPos, direction);
if (spawnedRoom is null) if (spawnedRoom is null)
{ {
GD.PrintErr($"Could not spawn room {roomToSpawn} at {nextPos}"); GD.PrintErr($"Could not spawn room {roomToSpawn} at {nextPos}");
continue; continue;
} }
_connections.Add(new RoomConnection(exitPosition, nextPos));
return spawnedRoom; return spawnedRoom;
} }
GD.PrintErr($"Could not spawn room at all at {nextPos}"); GD.PrintErr($"Could not spawn room at all at {nextPos}");
return null; return null;
} }
private RogueliteRoom TrySpawnRoom(RogueliteRoomResource roomToSpawn, Vector2I nextPos) private readonly System.Collections.Generic.Dictionary<Direction, Vector2I> dirDict = new()
{ {
var offsets = roomToSpawn.GetTopRoomOffsets(nextPos).Shuffle(); { Direction.Up, Vector2I.Up },
{ Direction.Down, Vector2I.Down },
{ Direction.Left, Vector2I.Left },
{ Direction.Right, Vector2I.Right },
};
private RogueliteRoom TrySpawnRoom(RogueliteRoomResource roomToSpawn, Vector2I nextPos, Direction direction)
{
List<Vector2I> offsets = [];
offsets.AddRange(roomToSpawn.GetRoomOffsets(nextPos, direction));
foreach (var offset in offsets) foreach (var offset in offsets)
{ {
@ -346,6 +314,61 @@ public partial class RogueliteRoomManager : Node2D
GameManager.Instance.RebakeNavigation(); GameManager.Instance.RebakeNavigation();
} }
private bool SpawnOffshoot(RogueliteRoom lastRoom, Direction direction, RoomType offshootTypeToSpawn,
int roomsInOffshot, List<RogueliteRoomResource> randomOffshootRoomsList)
{
RogueliteRoom lastSpawnedOffshootRoom = lastRoom;
for (int j = 0; j < roomsInOffshot; j++)
{
var shuffledOffshootRoomsList = randomOffshootRoomsList;//.Shuffle().ToList();
foreach (var shuffledOffshoot in shuffledOffshootRoomsList)
{
var spawnedRoom = TrySpawnRoom(shuffledOffshootRoomsList, lastSpawnedOffshootRoom, direction);
if (spawnedRoom is null)
{
GD.Print($"Could not place offshoot {shuffledOffshoot}");
// Try next in list
continue;
}
lastSpawnedOffshootRoom = spawnedRoom;
break;
}
// Nope no offshoot
if (lastSpawnedOffshootRoom is null)
{
return false;
}
}
// Offshoot over
// Spawn final room
// var finalRoomToSpawn = Rooms.Where(x => x.Type == offshootTypeToSpawn).PickRandom();
var spawnedFinalRoom = TrySpawnRoom(Rooms.Where(x => x.Type == offshootTypeToSpawn).ToList(),
lastSpawnedOffshootRoom, direction);
if (spawnedFinalRoom is null)
{
GD.Print($"Could not place offshoot final room {lastSpawnedOffshootRoom}");
// Try next in list
return false;
}
// var finalRoomCoord = leftPosition - new Vector2I(finalRoomToSpawn.Size.X, 0);
//
// SpawnRoom(finalRoomToSpawn, finalRoomCoord, out var spawnedFinalRoom);
//
// _connections.Add(new RoomConnection(leftPosition,
// finalRoomCoord + new Vector2I(finalRoomToSpawn.Size.X - 1, 0)));
return true;
}
private bool SpawnRoom(RogueliteRoomResource room, Vector2I gridPos, out RogueliteRoom spawnedRoom) private bool SpawnRoom(RogueliteRoomResource room, Vector2I gridPos, out RogueliteRoom spawnedRoom)
{ {
if (!CanPlaceRoom(gridPos, room.Size)) if (!CanPlaceRoom(gridPos, room.Size))
@ -406,4 +429,14 @@ public partial class RogueliteRoomManager : Node2D
} }
} }
} }
private void AddRandomOffshootType(List<RoomType> roomTypes, int minAmount, int maxAmount, RoomType type)
{
var itemsToAdd = GD.RandRange(minAmount, maxAmount);
GD.Print($"Adding {itemsToAdd} {type}");
for (int k = 0; k < itemsToAdd; k++)
{
roomTypes.Add(type);
}
}
} }

View file

@ -26,13 +26,31 @@ public partial class RogueliteRoomResource : Resource
return $"{RoomName} {Type} {Size.X}x{Size.Y}"; return $"{RoomName} {Type} {Size.X}x{Size.Y}";
} }
public List<Vector2I> GetTopRoomOffsets(Vector2I gridPosition) public List<Vector2I> GetRoomOffsets(Vector2I gridPosition, Direction side)
{ {
List<Vector2I> offsets = []; List<Vector2I> offsets = [];
for (int i = 0; i < Size.X; i++) switch (side)
{ {
offsets.Add(new Vector2I(gridPosition.X - i, gridPosition.Y)); case Direction.Down:
for (int i = 0; i < Size.X; i++)
offsets.Add(new Vector2I(gridPosition.X - i, gridPosition.Y));
break;
case Direction.Up:
for (int i = 0; i < Size.X; i++)
offsets.Add(new Vector2I(gridPosition.X - i, gridPosition.Y + Size.Y - 1));
break;
case Direction.Right:
for (int i = 0; i < Size.Y; i++)
offsets.Add(new Vector2I(gridPosition.X, gridPosition.Y - i));
break;
case Direction.Left:
for (int i = 0; i < Size.Y; i++)
offsets.Add(new Vector2I(gridPosition.X + Size.X - 1, gridPosition.Y - i));
break;
} }
return offsets; return offsets;