1
0

Dungeon.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. namespace Content.Shared.Procedural;
  2. /// <summary>
  3. /// Procedurally generated dungeon data.
  4. /// </summary>
  5. public sealed class Dungeon
  6. {
  7. public static Dungeon Empty = new Dungeon();
  8. private List<DungeonRoom> _rooms;
  9. private HashSet<Vector2i> _allTiles = new();
  10. public IReadOnlyList<DungeonRoom> Rooms => _rooms;
  11. /// <summary>
  12. /// Hashset of the tiles across all rooms.
  13. /// </summary>
  14. public readonly HashSet<Vector2i> RoomTiles = new();
  15. public readonly HashSet<Vector2i> RoomExteriorTiles = new();
  16. public readonly HashSet<Vector2i> CorridorTiles = new();
  17. public readonly HashSet<Vector2i> CorridorExteriorTiles = new();
  18. public readonly HashSet<Vector2i> Entrances = new();
  19. public IReadOnlySet<Vector2i> AllTiles => _allTiles;
  20. public Dungeon() : this(new List<DungeonRoom>())
  21. {
  22. }
  23. public Dungeon(List<DungeonRoom> rooms)
  24. {
  25. // This reftype is mine now.
  26. _rooms = rooms;
  27. foreach (var room in _rooms)
  28. {
  29. InternalAddRoom(room);
  30. }
  31. RefreshAllTiles();
  32. }
  33. public void RefreshAllTiles()
  34. {
  35. _allTiles.Clear();
  36. _allTiles.UnionWith(RoomTiles);
  37. _allTiles.UnionWith(RoomExteriorTiles);
  38. _allTiles.UnionWith(CorridorTiles);
  39. _allTiles.UnionWith(CorridorExteriorTiles);
  40. _allTiles.UnionWith(Entrances);
  41. }
  42. public void Rebuild()
  43. {
  44. _allTiles.Clear();
  45. RoomTiles.Clear();
  46. RoomExteriorTiles.Clear();
  47. Entrances.Clear();
  48. foreach (var room in _rooms)
  49. {
  50. InternalAddRoom(room, false);
  51. }
  52. RefreshAllTiles();
  53. }
  54. public void AddRoom(DungeonRoom room)
  55. {
  56. _rooms.Add(room);
  57. InternalAddRoom(room);
  58. }
  59. private void InternalAddRoom(DungeonRoom room, bool refreshAll = true)
  60. {
  61. Entrances.UnionWith(room.Entrances);
  62. RoomTiles.UnionWith(room.Tiles);
  63. RoomExteriorTiles.UnionWith(room.Exterior);
  64. if (refreshAll)
  65. RefreshAllTiles();
  66. }
  67. }