64 lines
1.9 KiB
C#
64 lines
1.9 KiB
C#
using Chickensoft.AutoInject;
|
|
using Chickensoft.Introspection;
|
|
using Godot;
|
|
using System.Collections.Generic;
|
|
using System.Collections.Immutable;
|
|
using System.Linq;
|
|
|
|
namespace GameJamDungeon
|
|
{
|
|
[Meta(typeof(IAutoNode))]
|
|
public partial class DungeonFloor : Node3D, IDungeonFloor
|
|
{
|
|
public override void _Notification(int what) => this.Notify(what);
|
|
|
|
[Node] public GodotObject DungeonGenerator { get; set; } = default!;
|
|
|
|
[Node] public EnemyDatabase EnemyDatabase { get; set; } = default!;
|
|
|
|
private Transform3D _playerSpawnPoint;
|
|
|
|
public ImmutableList<IDungeonRoom> Rooms { get; private set; }
|
|
|
|
public void InitializeDungeon()
|
|
{
|
|
Rooms = [];
|
|
Rooms = FindAllDungeonRooms([.. GetChildren()], Rooms);
|
|
_playerSpawnPoint = RandomizePlayerSpawnPoint();
|
|
var monsterRooms = Rooms.OfType<MonsterRoom>();
|
|
foreach (var room in monsterRooms)
|
|
room.SpawnEnemies(EnemyDatabase);
|
|
DungeonGenerator.EmitSignal("done_generating");
|
|
}
|
|
|
|
public Transform3D GetPlayerSpawnPoint() => _playerSpawnPoint;
|
|
|
|
private Transform3D RandomizePlayerSpawnPoint()
|
|
{
|
|
var randomSpawnLocations = Rooms
|
|
.OfType<MonsterRoom>()
|
|
.Select(x => x.PlayerSpawn);
|
|
var godotCollection = new Godot.Collections.Array<Marker3D>(randomSpawnLocations);
|
|
var result = godotCollection.PickRandom();
|
|
return result.GlobalTransform;
|
|
}
|
|
|
|
private static ImmutableList<IDungeonRoom> FindAllDungeonRooms(List<Node> nodesToSearch, ImmutableList<IDungeonRoom> roomsFound)
|
|
{
|
|
if (nodesToSearch.Count == 0)
|
|
return roomsFound;
|
|
|
|
foreach (var node in nodesToSearch)
|
|
{
|
|
if (node is IDungeonRoom dungeonRoom)
|
|
roomsFound = roomsFound.Add(dungeonRoom);
|
|
|
|
if (node.HasSignal("dungeon_done_generating"))
|
|
node.EmitSignal("dungeon_done_generating");
|
|
}
|
|
|
|
return FindAllDungeonRooms(nodesToSearch.SelectMany(x => x.GetChildren()).ToList(), roomsFound);
|
|
}
|
|
}
|
|
}
|