cirnogodot/Scripts/Controllers/RogueliteRoomManager.cs

586 lines
20 KiB
C#
Raw Permalink Normal View History

2025-04-15 16:22:30 +02:00
using System;
using System.Collections.Generic;
2025-04-11 18:39:39 +02:00
using System.Linq;
2025-04-29 18:14:09 +02:00
using Cirno.Scripts.Actors;
2025-04-11 18:39:39 +02:00
using Cirno.Scripts.Enums;
2025-04-24 16:40:51 +02:00
using Cirno.Scripts.Resources;
2025-04-10 19:04:06 +02:00
using Cirno.Scripts.Resources.Roguelite;
2025-04-15 15:19:36 +02:00
using Cirno.Scripts.Utils;
2025-04-10 19:04:06 +02:00
using Godot;
using Godot.Collections;
2025-04-22 18:21:53 +02:00
using Range = System.Range;
2025-04-10 19:04:06 +02:00
namespace Cirno.Scripts.Controllers;
public partial class RogueliteRoomManager : Node2D
{
2025-04-24 16:40:51 +02:00
//[Export] public Array<RogueliteRoomResource> Rooms { get; set; }
2025-04-30 10:30:43 +02:00
2025-04-30 16:11:25 +02:00
[Export] public Array<RogueliteMapTheme> MapThemes { get; set; }
2025-04-14 16:50:58 +02:00
2025-04-30 16:11:25 +02:00
private int CurrentLevel => GlobalState.Instance.SessionSettings.LevelNumber;
// TODO : Get based on level number
RogueliteMapTheme MapTheme => MapThemes[0];
// TODO: overflow on next theme
private RogueliteFloorResource CurrentFloorData => MapTheme.Floors[CurrentLevel % MapTheme.Floors.Count];
2025-04-14 16:50:58 +02:00
//private Godot.Collections.Dictionary<Vector2I, RogueliteRoomResource> _grid = new();
private Godot.Collections.Dictionary<Vector2I, RogueliteRoom> _roomGrid = new();
2025-04-15 12:04:22 +02:00
public Godot.Collections.Dictionary<Vector2I, RogueliteRoom> RoomGrid => _roomGrid;
2025-04-16 10:21:01 +02:00
2025-04-15 12:04:22 +02:00
public List<RogueliteRoom> SpawnedRooms { get; private set; } = [];
2025-04-14 16:50:58 +02:00
[Export] public Vector2I SpawnOrigin { get; private set; } = Vector2I.Zero;
2025-04-30 16:11:25 +02:00
//[Export] public int DungeonLength { get; set; } = 10;
// [Export] public int MaxBranches { get; set; } = 3;
// [Export] public int MaxBranchLength { get; set; } = 3;
//
// [Export] public int DungeonWidth = 10;
// [Export] public int DungeonHeight = 10;
// [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;
2025-04-29 18:14:09 +02:00
[Export] public string ManualSeed { get; private set; }
2025-04-10 19:04:06 +02:00
2025-04-30 10:30:43 +02:00
private ulong _seed;
2025-04-11 18:39:39 +02:00
[Export] public Vector2I TileSize { get; set; } = new Vector2I(16, 16);
[Export] public Vector2I RoomSizeInTiles { get; set; } = new Vector2I(20, 10);
2025-04-16 10:21:01 +02:00
[Signal]
public delegate void MapCreatedEventHandler();
2025-04-14 16:50:58 +02:00
2025-04-10 19:04:06 +02:00
public override void _Ready()
{
// Spawn first room
}
2025-04-11 18:39:39 +02:00
public void InitSpawning()
{
2025-04-30 15:09:59 +02:00
GlobalState.Instance.SessionSettings.GameMode = GameMode.Roguelite;
2025-04-30 16:11:25 +02:00
GlobalState.Instance.SessionSettings.AllowSaving = false;
// if (GlobalState.Instance.SessionSettings.LevelNumber < 0)
// {
// GlobalState.Instance.SessionSettings.LevelNumber = 0;
// }
2025-04-14 23:04:41 +02:00
GenerateStraightLineDungeon();
2025-04-11 18:39:39 +02:00
}
2025-04-30 16:11:25 +02:00
2025-04-24 16:40:51 +02:00
private IEnumerable<RogueliteRoomResource> StarterRooms => MapTheme.Rooms.Where(x => x.Type is RoomType.Starter);
2025-04-21 18:40:14 +02:00
2025-04-24 16:40:51 +02:00
private IEnumerable<RogueliteRoomResource> RegularRooms => MapTheme.Rooms.Where(x =>
2025-04-21 18:40:14 +02:00
x.Type is RoomType.Regular && x.HasDoors(DoorDirections.North | DoorDirections.South));
2025-04-24 16:40:51 +02:00
private IEnumerable<RogueliteRoomResource> OffshootRooms => MapTheme.Rooms.Where(x =>
2025-04-21 18:40:14 +02:00
x.Type is RoomType.Regular && x.HasDoors(DoorDirections.East | DoorDirections.West));
2025-04-24 16:40:51 +02:00
private IEnumerable<RogueliteRoomResource> BossRooms => MapTheme.Rooms.Where(x => x.Type is RoomType.Boss);
2025-04-22 18:21:53 +02:00
2025-04-14 16:50:58 +02:00
private List<RoomConnection> _connections = new();
2025-04-16 10:21:01 +02:00
2025-04-15 16:51:13 +02:00
public List<RoomConnection> Connections => _connections;
2025-04-14 16:50:58 +02:00
2025-04-29 18:14:09 +02:00
private void SetSeed()
2025-04-14 23:04:41 +02:00
{
2025-04-29 18:14:09 +02:00
if (!string.IsNullOrWhiteSpace(ManualSeed) && ulong.TryParse(ManualSeed, out var seed))
{
_seed = seed;
}
else
2025-04-16 10:21:01 +02:00
{
2025-04-29 18:14:09 +02:00
var rng = new RandomNumberGenerator();
_seed = rng.GetSeed();
rng.Dispose();
2025-04-16 10:21:01 +02:00
}
2025-04-30 10:30:43 +02:00
2025-04-30 15:09:59 +02:00
GD.Seed(_seed);
2025-04-29 18:14:09 +02:00
GD.Print($"Seed: {_seed}");
}
2025-04-30 10:30:43 +02:00
2025-04-29 18:14:09 +02:00
private void GenerateStraightLineDungeon()
{
SetSeed();
2025-04-30 10:30:43 +02:00
2025-04-30 16:11:25 +02:00
GD.Print($"Floor: {CurrentLevel}");
2025-04-29 18:14:09 +02:00
MapTheme.MakeChestLootQueue();
MapTheme.TeleportersList = [];
2025-04-16 10:21:01 +02:00
2025-04-14 23:04:41 +02:00
Vector2I origin = Vector2I.Zero;
2025-04-24 16:40:51 +02:00
var starterRoom = MapTheme.Rooms.Where(r => r.Type == RoomType.Starter).PickRandom();
2025-04-16 18:18:52 +02:00
SpawnRoom(starterRoom, origin, out var spawnedBeginRoom);
2025-04-30 10:30:43 +02:00
var randomRoomsList = RegularRooms.Shuffle().ToList();
var regularRoomsQueue = new Queue<RogueliteRoomResource>().EnqueueRange(randomRoomsList);
var randomOffshootRoomsList = OffshootRooms.Shuffle().ToList();
2025-04-16 10:21:01 +02:00
2025-04-30 10:30:43 +02:00
var offshootRoomsQueue = new Queue<RogueliteRoomResource>().EnqueueRange(randomOffshootRoomsList);
2025-04-21 18:40:14 +02:00
2025-04-22 13:50:26 +02:00
var currentPos = spawnedBeginRoom.RandomBottomExit();
2025-04-16 10:21:01 +02:00
_connections.Add(new RoomConnection(origin, currentPos + new Vector2I(0, 1)));
2025-04-14 23:04:41 +02:00
Vector2I nextPos;
2025-04-16 10:21:01 +02:00
2025-04-22 18:21:53 +02:00
var offshoots = new List<RoomType>();
2025-04-30 16:11:25 +02:00
AddRandomOffshootType(offshoots, CurrentFloorData.MinKeys, CurrentFloorData.MaxKeys, RoomType.Key);
AddRandomOffshootType(offshoots, CurrentFloorData.MinShops, CurrentFloorData.MaxShops, RoomType.Shop);
AddRandomOffshootType(offshoots, CurrentFloorData.MinSecrets, CurrentFloorData.MaxSecrets, RoomType.Secret);
AddRandomOffshootType(offshoots, CurrentFloorData.MinTreasures, CurrentFloorData.MaxTreasures, RoomType.Treasure);
2025-04-22 18:21:53 +02:00
2025-05-02 11:52:06 +02:00
// Always add guaranteed treasure first
var shuffledOffshoots = offshoots.Shuffle();
2025-04-30 10:30:43 +02:00
var offshootsQueue = new Queue<RoomType>();
2025-05-02 11:52:06 +02:00
offshootsQueue.Enqueue(RoomType.Treasure); // Always add a guaranteed treasure first
2025-04-30 10:30:43 +02:00
offshootsQueue.EnqueueRange(shuffledOffshoots);
2025-04-18 17:32:12 +02:00
int currentOffshoot = 0;
2025-04-22 18:21:53 +02:00
RogueliteRoom lastRoom = spawnedBeginRoom;
2025-04-24 10:19:25 +02:00
bool lockNext = false;
2025-04-30 10:30:43 +02:00
2025-04-30 16:11:25 +02:00
for (int i = 0; i < CurrentFloorData.DungeonLength; i++)
2025-04-14 23:04:41 +02:00
{
2025-04-21 17:46:26 +02:00
GD.Print($"Dungeon room {i}");
2025-04-16 10:21:01 +02:00
2025-04-30 10:30:43 +02:00
// if (!regularRoomsQueue.TryDequeue(out var roomToSpawn))
// {
// GD.Print("Ran out of regular rooms, add more");
// return;
// }
2025-04-22 18:21:53 +02:00
2025-04-30 10:30:43 +02:00
// 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);
var spawnedRoom = TrySpawnRoom(regularRoomsQueue,
2025-04-22 18:21:53 +02:00
lastRoom, Direction.Down);
2025-04-16 10:21:01 +02:00
2025-04-22 13:50:26 +02:00
if (spawnedRoom is null)
2025-04-15 16:22:30 +02:00
{
2025-04-30 10:30:43 +02:00
GD.Print("Abort creation");
return;
2025-04-16 10:21:01 +02:00
}
2025-04-21 18:40:14 +02:00
2025-04-24 10:19:25 +02:00
if (lockNext)
2025-04-21 17:46:26 +02:00
{
2025-04-24 10:19:25 +02:00
_connections.Last().IsLocked = true;
lockNext = false;
2025-04-21 17:46:26 +02:00
}
2025-04-21 18:40:14 +02:00
2025-04-24 10:19:25 +02:00
lastRoom = spawnedRoom;
2025-04-30 10:30:43 +02:00
2025-04-24 10:19:25 +02:00
// Spawn offshoot here
2025-04-30 10:30:43 +02:00
2025-04-21 12:12:27 +02:00
// Roll whether to go left or right, if direction is full go the other, if both are full do not spawn
2025-04-21 18:40:14 +02:00
2025-04-22 18:21:53 +02:00
var directions = new List<Direction>();
2025-04-21 18:40:14 +02:00
2025-04-22 18:21:53 +02:00
if (lastRoom.RoomResource.DoorDirections.HasFlag(DoorDirections.East)) directions.Add(Direction.Right);
if (lastRoom.RoomResource.DoorDirections.HasFlag(DoorDirections.West)) directions.Add(Direction.Left);
2025-04-21 12:12:27 +02:00
2025-04-22 18:21:53 +02:00
directions = directions.Shuffle().ToList();
2025-04-30 10:30:43 +02:00
2025-04-22 18:21:53 +02:00
foreach (var direction in directions)
{
2025-05-02 11:17:09 +02:00
GD.Print($"Offshoot trying {direction}");
2025-04-30 10:30:43 +02:00
if (!offshootsQueue.TryDequeue(out var offshootTypeToSpawn))
{
2025-04-30 15:09:59 +02:00
GD.Print("Ran out of offshoot types");
2025-04-30 10:30:43 +02:00
break;
}
2025-04-22 18:21:53 +02:00
int roomsInOffshot = offshootTypeToSpawn is RoomType.Secret or RoomType.Shop
? 0
2025-04-30 16:11:25 +02:00
: GD.RandRange(0, CurrentFloorData.MaxBranchLength);
2025-04-21 18:40:14 +02:00
2025-04-30 10:30:43 +02:00
// var roomsForOffshoot = randomOffshootRoomsList
// .Take(new Range(randOffshootStartIndex, randomOffshootRoomsList.Count - 1)).ToList();
// This one reshuffles improperly, good for now
var endRooms =
new Queue<RogueliteRoomResource>().EnqueueRange(MapTheme.Rooms
.Where(x => x.Type == offshootTypeToSpawn).ToList());
2025-04-22 18:21:53 +02:00
2025-05-02 11:17:09 +02:00
GD.Print($"Trying to spawn {roomsInOffshot} rooms ending in {endRooms.Peek()}");
2025-04-30 10:30:43 +02:00
var res = SpawnOffshoot(lastRoom, direction, offshootTypeToSpawn, roomsInOffshot, offshootRoomsQueue, endRooms);
2025-04-20 17:47:57 +02:00
2025-04-22 18:21:53 +02:00
if (res)
{
2025-04-21 17:46:26 +02:00
currentOffshoot++;
2025-04-24 10:19:25 +02:00
// Try to spawn one the other direction too
2025-04-30 10:30:43 +02:00
2025-04-24 10:19:25 +02:00
// If key, lock the last connection
if (offshootTypeToSpawn is RoomType.Key)
{
//lastConnection.IsLocked = true;
// Need to lock the next connection
lockNext = true;
}
2025-04-30 10:30:43 +02:00
2025-04-30 15:09:59 +02:00
if (offshootTypeToSpawn is RoomType.Key)
2025-04-24 10:19:25 +02:00
{
2025-04-30 15:09:59 +02:00
bool hasNextRoom = offshootsQueue.TryPeek(out var nextRoom);
if (!hasNextRoom || nextRoom is RoomType.Key)
2025-04-24 10:19:25 +02:00
// Stop if next room is a key
break;
}
//break;
2025-04-21 12:12:27 +02:00
}
2025-04-30 15:09:59 +02:00
else
{
offshootsQueue.Enqueue(offshootTypeToSpawn);
}
2025-04-18 17:32:12 +02:00
}
2025-04-21 18:40:14 +02:00
2025-04-22 18:21:53 +02:00
// Add more dungeon if not enough rooms are generated
2025-04-30 16:11:25 +02:00
if (i == CurrentFloorData.DungeonLength - 1 && CurrentFloorData.DungeonLength < CurrentFloorData.MaxRooms && currentOffshoot < CurrentFloorData.DungeonLength)
2025-04-22 18:21:53 +02:00
{
2025-04-30 16:11:25 +02:00
CurrentFloorData.DungeonLength++;
2025-04-22 18:21:53 +02:00
}
2025-04-14 23:04:41 +02:00
}
2025-04-16 10:21:01 +02:00
2025-04-14 23:04:41 +02:00
var bossRoom = BossRooms.PickRandom();
2025-04-16 10:21:01 +02:00
2025-04-30 10:30:43 +02:00
var bossQueue = new Queue<RogueliteRoomResource>().EnqueueRange(BossRooms.Shuffle());
var spawnedBossRoom = TrySpawnRoom(bossQueue, lastRoom, Direction.Down);
2025-04-21 18:40:14 +02:00
2025-04-22 18:21:53 +02:00
if (spawnedBossRoom is null)
2025-04-15 16:22:30 +02:00
{
2025-04-22 18:21:53 +02:00
GD.PrintErr($"Could not spawn boss room {bossRoom}");
2025-04-15 16:22:30 +02:00
}
2025-04-30 10:30:43 +02:00
2025-04-24 10:19:25 +02:00
if (lockNext)
{
_connections.Last().IsLocked = true;
lockNext = false;
}
2025-04-16 10:21:01 +02:00
2025-04-17 16:51:20 +02:00
foreach (var room in SpawnedRooms)
2025-04-14 23:04:41 +02:00
{
2025-04-16 15:11:29 +02:00
room.HandleDoors((doorEdge, pos) =>
2025-04-14 23:04:41 +02:00
{
2025-04-16 15:11:29 +02:00
//var neighborPos = room.GridPosition + pos;
2025-04-16 18:18:52 +02:00
return _connections.FirstOrDefault(x =>
(x.From == doorEdge && x.To == pos) || (x.From == pos && x.To == doorEdge));
2025-04-16 15:11:29 +02:00
//return _roomGrid.ContainsKey(neighborPos);
2025-04-14 23:04:41 +02:00
});
}
2025-04-30 10:30:43 +02:00
2025-04-29 18:14:09 +02:00
// Debug teleporter matching
MatchBossTeleporter();
2025-04-16 10:21:01 +02:00
2025-04-15 12:04:22 +02:00
EmitSignalMapCreated();
2025-04-21 18:40:14 +02:00
2025-04-16 18:18:52 +02:00
//CallDeferred(MethodName.OpenStartDoorsDeferred, spawnedBeginRoom);
2025-04-14 23:04:41 +02:00
}
2025-04-16 10:21:01 +02:00
2025-04-29 18:14:09 +02:00
private void MatchBossTeleporter()
{
var bossTeleporters = MapTheme.TeleportersList.Where(x => x.Type is TeleporterMarkerType.Boss);
2025-04-30 10:30:43 +02:00
2025-04-29 18:14:09 +02:00
foreach (var teleporterMarker in bossTeleporters)
{
var bossRoom = SpawnedRooms.FirstOrDefault(x => x.RoomResource.Type is RoomType.Boss);
if (bossRoom is null) return;
2025-04-30 10:30:43 +02:00
2025-04-29 18:14:09 +02:00
var teleporter = bossRoom.Teleporters.FirstOrDefault(x =>
x.Type is TeleporterMarkerType.Receiver or TeleporterMarkerType.InvisibleReceiver);
if (teleporter is null) return;
teleporterMarker.SpawnedTeleporter.Target = teleporter.SpawnedTeleporter;
}
}
2025-04-30 10:30:43 +02:00
private RogueliteRoom TrySpawnRoom(Queue<RogueliteRoomResource> roomsList, RogueliteRoom lastRoom,
2025-04-22 18:21:53 +02:00
Direction direction)
2025-04-22 13:50:26 +02:00
{
2025-04-22 18:21:53 +02:00
//var nextPos = originPos + new Vector2I(0, 1);
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
};
2025-04-30 10:30:43 +02:00
//var rooms = roomsList; //.Shuffle().ToList();
int tries = 0;
while (roomsList.Count > 0 && tries < 10)
2025-04-22 13:50:26 +02:00
{
2025-04-30 10:30:43 +02:00
if (!roomsList.TryDequeue(out var roomToSpawn))
{
GD.Print("Ran out of regular rooms, add more");
return null;
}
2025-05-02 11:17:09 +02:00
// Shift position by the size
var posToSpawn = direction switch
{
Direction.Up => nextPos - new Vector2I(0, roomToSpawn.Size.Y -1),
Direction.Down => nextPos,
Direction.Left => nextPos - new Vector2I(roomToSpawn.Size.X -1, 0),
Direction.Right => nextPos,
_ => nextPos
};
2025-04-22 18:21:53 +02:00
var spawnedRoom = TrySpawnRoom(roomToSpawn, nextPos, direction);
2025-04-22 13:50:26 +02:00
if (spawnedRoom is null)
{
2025-04-30 10:30:43 +02:00
roomsList.Enqueue(roomToSpawn);
tries++;
2025-04-22 13:50:26 +02:00
continue;
}
2025-04-22 18:21:53 +02:00
_connections.Add(new RoomConnection(exitPosition, nextPos));
2025-04-22 13:50:26 +02:00
return spawnedRoom;
}
2025-04-22 18:21:53 +02:00
2025-04-22 13:50:26 +02:00
GD.PrintErr($"Could not spawn room at all at {nextPos}");
return null;
}
2025-04-22 18:21:53 +02:00
private readonly System.Collections.Generic.Dictionary<Direction, Vector2I> dirDict = new()
2025-04-22 13:50:26 +02:00
{
2025-04-22 18:21:53 +02:00
{ 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));
2025-04-22 13:50:26 +02:00
foreach (var offset in offsets)
{
if (!SpawnRoom(roomToSpawn, offset, out var spawnedRoom))
{
GD.PrintErr($"Could not spawn room {roomToSpawn} at {nextPos}");
//DungeonLength += 1;
}
else
{
return spawnedRoom;
}
}
return null;
}
2025-04-14 16:50:58 +02:00
private Vector2I GetNextPosition(Vector2I current, Vector2I target)
{
var possibleDirs = new List<Vector2I>();
2025-04-11 18:39:39 +02:00
2025-04-14 16:50:58 +02:00
if (target.X > current.X)
possibleDirs.Add(Vector2I.Right);
else if (target.X < current.X)
possibleDirs.Add(Vector2I.Left);
2025-04-11 18:39:39 +02:00
2025-04-14 16:50:58 +02:00
if (target.Y > current.Y)
possibleDirs.Add(Vector2I.Down);
else if (target.Y < current.Y)
possibleDirs.Add(Vector2I.Up);
2025-04-11 18:39:39 +02:00
2025-04-14 16:50:58 +02:00
// Add random noise to allow slightly winding paths
possibleDirs.AddRange(new[] { Vector2I.Right, Vector2I.Left, Vector2I.Up, Vector2I.Down });
possibleDirs = possibleDirs.Distinct().OrderBy(_ => GD.Randi()).ToList();
foreach (var dir in possibleDirs)
{
var nextPos = current + dir;
if (!_roomGrid.ContainsKey(nextPos))
return nextPos;
2025-04-11 18:39:39 +02:00
}
2025-04-14 16:50:58 +02:00
return current; // No valid next position found
2025-04-10 19:04:06 +02:00
}
private void RebakeNavigationDeferred()
{
GameManager.Instance.RebakeNavigation();
}
2025-04-22 18:21:53 +02:00
private bool SpawnOffshoot(RogueliteRoom lastRoom, Direction direction, RoomType offshootTypeToSpawn,
2025-04-30 10:30:43 +02:00
int roomsInOffshot, Queue<RogueliteRoomResource> randomOffshootRoomsList, Queue<RogueliteRoomResource> endRoomsQueue)
2025-04-22 18:21:53 +02:00
{
RogueliteRoom lastSpawnedOffshootRoom = lastRoom;
2025-04-30 10:30:43 +02:00
int tries = 0;
int spawned = 0;
while (randomOffshootRoomsList.Count > 0 && tries < 10 && spawned < roomsInOffshot)
{
var spawnedRoom = TrySpawnRoom(randomOffshootRoomsList, lastSpawnedOffshootRoom, direction);
if (spawnedRoom is null)
2025-04-22 18:21:53 +02:00
{
2025-04-30 10:30:43 +02:00
// GD.Print($"Could not place offshoot {shuffledOffshoot}");
// Try next in list
tries++;
continue;
2025-04-22 18:21:53 +02:00
}
2025-04-30 10:30:43 +02:00
lastSpawnedOffshootRoom = spawnedRoom;
//roomsInOffshot++;
spawned++;
2025-04-22 18:21:53 +02:00
}
2025-04-30 10:30:43 +02:00
if (lastSpawnedOffshootRoom is null)
{
return false;
}
// 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;
// }
// }
2025-04-22 18:21:53 +02:00
// Offshoot over
// Spawn final room
// var finalRoomToSpawn = Rooms.Where(x => x.Type == offshootTypeToSpawn).PickRandom();
2025-04-30 10:30:43 +02:00
var spawnedFinalRoom = TrySpawnRoom(endRoomsQueue,
2025-04-22 18:21:53 +02:00
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;
}
2025-04-16 18:18:52 +02:00
private bool SpawnRoom(RogueliteRoomResource room, Vector2I gridPos, out RogueliteRoom spawnedRoom)
2025-04-10 19:04:06 +02:00
{
2025-04-14 16:50:58 +02:00
if (!CanPlaceRoom(gridPos, room.Size))
2025-04-16 18:18:52 +02:00
{
spawnedRoom = null;
2025-04-21 17:46:26 +02:00
GD.Print($"{gridPos} size {room.Size} is occupied");
2025-04-14 16:50:58 +02:00
return false;
2025-04-16 18:18:52 +02:00
}
2025-04-16 10:21:01 +02:00
2025-04-14 16:50:58 +02:00
var position = gridPos * TileSize * RoomSizeInTiles;
2025-04-11 18:39:39 +02:00
var roomScene = GD.Load<PackedScene>(room.ScenePath);
2025-04-10 19:04:06 +02:00
var spawnedScene = this.CreateChild<RogueliteRoom>(roomScene, position);
2025-04-14 16:50:58 +02:00
2025-04-24 16:40:51 +02:00
spawnedScene.MapTheme = MapTheme;
2025-04-14 16:50:58 +02:00
spawnedScene.GridPosition = gridPos;
2025-04-21 17:46:26 +02:00
spawnedScene.Name = room.RoomName.ToString().Replace(" ", "_");
2025-04-21 18:40:14 +02:00
2025-04-15 12:04:22 +02:00
SpawnedRooms.Add(spawnedScene);
2025-04-16 10:21:01 +02:00
2025-04-21 17:46:26 +02:00
GD.Print($"Spawned room: {spawnedScene}");
2025-04-21 18:40:14 +02:00
2025-04-11 18:39:39 +02:00
// for reference
//SpawnRoom(room, origin + (room.Size * new Vector2(i, j) * tileSize));
2025-04-14 16:50:58 +02:00
spawnedScene.Spawn();
2025-04-15 15:19:36 +02:00
MarkRoom(gridPos, room.Size, spawnedScene);
2025-04-16 18:18:52 +02:00
spawnedRoom = spawnedScene;
2025-04-14 16:50:58 +02:00
return true;
2025-04-11 18:39:39 +02:00
}
2025-04-14 16:50:58 +02:00
// private bool CheckConnection(Vector2I pos)
// {
// return _grid.ContainsKey(pos); // <- this is your dungeon grid dictionary
// }
//
2025-04-11 18:39:39 +02:00
private bool CanPlaceRoom(Vector2I origin, Vector2I size)
{
for (int x = 0; x < size.X; x++)
{
for (int y = 0; y < size.Y; y++)
{
var pos = origin + new Vector2I(x, y);
2025-04-14 16:50:58 +02:00
if (_roomGrid.ContainsKey(pos)) return false;
2025-04-11 18:39:39 +02:00
}
}
2025-04-16 10:21:01 +02:00
2025-04-11 18:39:39 +02:00
return true;
}
2025-04-14 16:50:58 +02:00
private void MarkRoom(Vector2I origin, Vector2I size, RogueliteRoom room)
2025-04-11 18:39:39 +02:00
{
for (int x = 0; x < size.X; x++)
{
for (int y = 0; y < size.Y; y++)
{
var pos = origin + new Vector2I(x, y);
2025-04-14 16:50:58 +02:00
_roomGrid[pos] = room;
2025-04-11 18:39:39 +02:00
}
}
2025-04-10 19:04:06 +02:00
}
2025-04-22 18:21:53 +02:00
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);
}
}
2025-04-10 19:04:06 +02:00
}