cirnogodot/Scripts/Controllers/RogueliteRoomManager.cs

344 lines
11 KiB
C#
Raw 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;
using Cirno.Scripts.Enums;
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;
namespace Cirno.Scripts.Controllers;
public partial class RogueliteRoomManager : Node2D
{
[Export] public Array<RogueliteRoomResource> Rooms { get; set; }
2025-04-14 16:50:58 +02:00
2025-04-10 19:04:06 +02:00
[Export] public Node2D SceneContainer { get; set; }
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;
[Export] public int DungeonLength { get; set; } = 10;
[Export] public int MaxBranches { get; set; } = 3;
[Export] public int MaxBranchLength { get; set; } = 3;
2025-04-11 18:39:39 +02:00
[Export] public int DungeonWidth = 10;
[Export] public int DungeonHeight = 10;
[Export] public int MaxRooms = 12;
2025-04-16 10:21:01 +02:00
[Export] public ulong Seed = 0;
2025-04-10 19:04:06 +02:00
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-14 23:04:41 +02:00
GenerateStraightLineDungeon();
2025-04-11 18:39:39 +02:00
}
2025-04-14 16:50:58 +02:00
2025-04-10 19:04:06 +02:00
private void SpawnRoomsGrid()
{
2025-04-11 15:53:59 +02:00
//var firstRoom = Rooms.FirstOrDefault();
2025-04-10 19:04:06 +02:00
var origin = Vector2.Zero;
var tileSize = new Vector2(16, 16);
2025-04-14 16:50:58 +02:00
2025-04-10 19:04:06 +02:00
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
2025-04-11 15:53:59 +02:00
var roomIndex = GD.RandRange(0, Rooms.Count - 1);
var room = Rooms[roomIndex];
2025-04-11 18:39:39 +02:00
//SpawnRoom(room, origin + (room.Size * new Vector2(i, j) * tileSize));
2025-04-10 19:04:06 +02:00
}
}
2025-04-11 15:53:59 +02:00
//CallDeferred(MethodName.RebakeNavigationDeferred);
2025-04-11 18:39:39 +02:00
}
2025-04-10 19:04:06 +02:00
2025-04-14 16:50:58 +02:00
private IEnumerable<RogueliteRoomResource> StarterRooms => Rooms.Where(x => x.Type is RoomType.Starter);
2025-04-20 17:47:57 +02:00
private IEnumerable<RogueliteRoomResource> RegularRooms => Rooms.Where(x => x.Type is RoomType.Regular && x.HasDoors(DoorDirections.North | DoorDirections.South));
private IEnumerable<RogueliteRoomResource> OffshootRooms => Rooms.Where(x => x.Type is RoomType.Regular && x.HasDoors(DoorDirections.East | DoorDirections.West));
2025-04-14 16:50:58 +02:00
private IEnumerable<RogueliteRoomResource> BossRooms => Rooms.Where(x => x.Type is RoomType.Boss);
private List<Vector2I> _mainPath = new();
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-14 23:04:41 +02:00
private void GenerateStraightLineDungeon()
{
2025-04-16 10:21:01 +02:00
if (Seed > 0)
{
GD.Seed(Seed);
}
2025-04-14 23:04:41 +02:00
Vector2I origin = Vector2I.Zero;
var starterRoom = Rooms.Where(r => r.Type == RoomType.Starter).PickRandom();
2025-04-16 18:18:52 +02:00
SpawnRoom(starterRoom, origin, out var spawnedBeginRoom);
2025-04-14 23:04:41 +02:00
_mainPath.Add(origin);
2025-04-16 18:18:52 +02:00
2025-04-16 10:21:01 +02:00
var randomRoomsList = RegularRooms.Shuffle(DungeonLength).ToList();
2025-04-20 17:47:57 +02:00
var randomOffshootRoomsList = OffshootRooms.Shuffle(MaxBranchLength).ToList();
2025-04-14 23:04:41 +02:00
var currentPos = origin;
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-18 17:32:12 +02:00
var offshoots = new List<OffshootType>()
{
OffshootType.Item,
OffshootType.Secret,
OffshootType.Shop,
OffshootType.Key
};
var shuffledOffshoots = offshoots.Shuffle().ToList();
int currentOffshoot = 0;
2025-04-14 23:04:41 +02:00
for (int i = 0; i < DungeonLength; i++)
{
nextPos = currentPos + new Vector2I(0, 1);
2025-04-15 15:19:36 +02:00
//var roomToSpawn = Rooms.Where(x => x.Type == RoomType.Regular).PickRandom();
var roomToSpawn = randomRoomsList[i];
2025-04-18 17:32:12 +02:00
2025-04-16 10:21:01 +02:00
// We're already in the new room position, we do not care about previous anymore
2025-04-15 16:22:30 +02:00
var offset = 0;
2025-04-16 10:21:01 +02:00
2025-04-15 16:22:30 +02:00
// Place it at a random X position
if (roomToSpawn.Size.X > 1)
{
offset -= GD.RandRange(0, roomToSpawn.Size.X - 1);
}
2025-04-14 23:04:41 +02:00
2025-04-16 10:21:01 +02:00
nextPos += new Vector2I(offset, 0);
//var posWithOffset = nextPos + new Vector2I(offset, 0);
2025-04-16 18:18:52 +02:00
if (!SpawnRoom(roomToSpawn, nextPos, out var spawnedRoom))
2025-04-15 16:22:30 +02:00
{
GD.PrintErr("Could not spawn room");
break;
2025-04-16 10:21:01 +02:00
}
2025-04-15 16:22:30 +02:00
2025-04-16 10:21:01 +02:00
_mainPath.Add(nextPos);
// Place cursor at bottom of room
2025-04-14 23:04:41 +02:00
if (roomToSpawn.Size.Y > 1)
{
2025-04-16 10:21:01 +02:00
nextPos += new Vector2I(0, roomToSpawn.Size.Y - 1);
2025-04-14 23:04:41 +02:00
}
2025-04-16 10:21:01 +02:00
2025-04-15 16:22:30 +02:00
// Pick a random exit to continue
2025-04-16 10:21:01 +02:00
var exit = GD.RandRange(0, roomToSpawn.Size.X - 1);
if (exit < 0) exit = 0;
nextPos += new Vector2I(exit, 0);
// nextPos is now the end of the room at the current exit
_connections.Add(new RoomConnection(nextPos, nextPos + new Vector2I(0, 1)));
2025-04-18 17:32:12 +02:00
// Spawn offshoot here
2025-04-20 17:47:57 +02:00
var offshootTypeToSpawn = shuffledOffshoots[currentOffshoot % shuffledOffshoots.Count()];
2025-04-18 17:32:12 +02:00
int roomsInOffshot = GD.RandRange(0, MaxBranchLength);
2025-04-20 17:47:57 +02:00
var leftPosition = spawnedRoom.GridPosition;
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-18 17:32:12 +02:00
for (int j = 0; j < roomsInOffshot; j++)
{
2025-04-21 12:12:27 +02:00
var shuffledOffshootRoomsList = randomOffshootRoomsList.Shuffle().ToList();
foreach (var shuffledOffshoot in shuffledOffshootRoomsList)
{
var offShootCoord = leftPosition - new Vector2I(shuffledOffshoot.Size.X, 0);
if (!CanPlaceRoom(offShootCoord, shuffledOffshoot.Size))
{
GD.Print($"Could not place room {shuffledOffshoot.RoomName} {shuffledOffshoot.Size}");
// Try next in list
continue;
}
var offshootCoord = leftPosition - new Vector2I(shuffledOffshoot.Size.X, 0);
SpawnRoom(shuffledOffshoot, offshootCoord, out var spawnedOffshoot);
_connections.Add(new RoomConnection(leftPosition, offshootCoord + new Vector2I(shuffledOffshoot.Size.X -1, 0)));
leftPosition = offshootCoord;
2025-04-20 17:47:57 +02:00
2025-04-21 12:12:27 +02:00
// Stop because we spawned the room we needed to
break;
}
2025-04-20 17:47:57 +02:00
2025-04-21 12:12:27 +02:00
//var room = randomOffshootRoomsList.Shuffle().FirstOrDefault();
2025-04-20 17:47:57 +02:00
// Get a random room with the required doors
//var leftToRightRooms =
2025-04-18 17:32:12 +02:00
// Get a random door on right or left side
// spawn
// Add path
// Move cursor
// if last room generate final room
// Continue
}
// Offshoot over
2025-04-14 23:04:41 +02:00
currentPos = nextPos;
}
2025-04-16 10:21:01 +02:00
2025-04-14 23:04:41 +02:00
nextPos = currentPos + new Vector2I(0, 1);
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-16 18:18:52 +02:00
if (SpawnRoom(bossRoom, nextPos, out var spawnedBossRoom))
2025-04-14 23:04:41 +02:00
{
_mainPath.Add(nextPos);
2025-04-16 10:21:01 +02:00
2025-04-14 23:04:41 +02:00
if (bossRoom.Size.Y > 1)
{
2025-04-16 10:21:01 +02:00
nextPos += new Vector2I(0, bossRoom.Size.Y - 1);
2025-04-14 23:04:41 +02:00
}
2025-04-16 10:21:01 +02:00
2025-04-14 23:04:41 +02:00
_connections.Add(new RoomConnection(currentPos, nextPos));
}
2025-04-15 16:22:30 +02:00
else
{
GD.PrintErr("Could not spawn boss room");
}
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-16 10:21:01 +02:00
2025-04-15 12:04:22 +02:00
EmitSignalMapCreated();
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-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-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-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
spawnedScene.GridPosition = gridPos;
2025-04-17 16:51:20 +02:00
spawnedScene.Name = room.RoomName;
2025-04-15 12:04:22 +02:00
SpawnedRooms.Add(spawnedScene);
2025-04-16 10:21:01 +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
}
}