61 lines
1.8 KiB
C#
61 lines
1.8 KiB
C#
using Chickensoft.AutoInject;
|
|
using Chickensoft.Introspection;
|
|
using Godot;
|
|
using System.Collections.Generic;
|
|
using System.Collections.Immutable;
|
|
using System.Linq;
|
|
|
|
namespace Zennysoft.Game.Ma;
|
|
|
|
[Meta(typeof(IAutoNode))]
|
|
public partial class DungeonFloor : Node3D, IDungeonFloor
|
|
{
|
|
public override void _Notification(int what) => this.Notify(what);
|
|
|
|
private Transform3D _playerSpawnPoint;
|
|
|
|
public ImmutableList<IDungeonRoom> Rooms { get; private set; }
|
|
|
|
public bool FloorIsLoaded { get; set; }
|
|
|
|
public void InitializeDungeon()
|
|
{
|
|
Rooms = [];
|
|
Rooms = FindAllDungeonRooms([.. GetChildren()], Rooms);
|
|
_playerSpawnPoint = RandomizePlayerSpawnPoint();
|
|
}
|
|
|
|
public void SpawnEnemies(Godot.Collections.Dictionary<EnemyType, float> enemyInfo)
|
|
{
|
|
var monsterRooms = Rooms.OfType<MonsterRoom>();
|
|
foreach (var room in monsterRooms)
|
|
room.SpawnEnemies(enemyInfo);
|
|
}
|
|
|
|
public Transform3D GetPlayerSpawnPoint() => new Transform3D(_playerSpawnPoint.Basis, new Vector3(_playerSpawnPoint.Origin.X, 0f, _playerSpawnPoint.Origin.Z));
|
|
|
|
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);
|
|
}
|
|
|
|
return FindAllDungeonRooms([.. nodesToSearch.SelectMany(x => x.GetChildren())], roomsFound);
|
|
}
|
|
}
|