ManiaMap.Godot
Procedural generation of metroidvania style maps for Godot .NET.
LayoutPack.cs
1using Godot;
2using MPewsey.Common.Mathematics;
3using MPewsey.ManiaMap;
4using System;
5using System.Collections.Generic;
6
8{
12 public class LayoutPack
13 {
17 public Layout Layout { get; }
18
22 public LayoutState LayoutState { get; }
23
27 public ManiaMapSettings Settings { get; }
28
32 private Dictionary<Uid, List<DoorConnection>> RoomConnections { get; } = new Dictionary<Uid, List<DoorConnection>>();
33
37 private Dictionary<int, List<Room>> RoomsByLayer { get; } = new Dictionary<int, List<Room>>();
38
42 private Dictionary<Uid, List<DoorPosition>> RoomDoors { get; } = new Dictionary<Uid, List<DoorPosition>>();
43
50 public LayoutPack(Layout layout, LayoutState state, string settingsPath)
51 : this(layout, state, ResourceLoader.Load<ManiaMapSettings>(settingsPath))
52 {
53
54 }
55
64 public LayoutPack(Layout layout, LayoutState state, ManiaMapSettings settings = null)
65 {
66 ArgumentNullException.ThrowIfNull(layout, nameof(layout));
67 ArgumentNullException.ThrowIfNull(state, nameof(state));
68
69 if (layout.Id != state.Id)
70 throw new ArgumentException($"Layout and layout state ID's do not match: (Layout ID = {layout.Id}, Layout State ID = {state.Id})");
71
72 Layout = layout;
73 LayoutState = state;
74 Settings = settings;
75 RoomConnections = layout.GetRoomConnections();
76 RoomsByLayer = layout.GetRoomsByLayer();
77 RoomDoors = layout.GetRoomDoors();
78 }
79
85 public IReadOnlyList<DoorConnection> GetDoorConnections(Uid id)
86 {
87 if (RoomConnections.TryGetValue(id, out var connections))
88 return connections;
89 return Array.Empty<DoorConnection>();
90 }
91
97 public IReadOnlyList<Room> GetRoomsInLayer(int z)
98 {
99 if (RoomsByLayer.TryGetValue(z, out var rooms))
100 return rooms;
101 return Array.Empty<Room>();
102 }
103
109 public IReadOnlyList<DoorPosition> GetRoomDoors(Uid id)
110 {
111 if (RoomDoors.TryGetValue(id, out var doors))
112 return doors;
113 return Array.Empty<DoorPosition>();
114 }
115
122 public bool DoorExists(Uid id, Vector2DInt position, DoorDirection direction)
123 {
124 foreach (var door in GetRoomDoors(id))
125 {
126 if (door.Matches(position, direction))
127 return true;
128 }
129
130 return false;
131 }
132
137 public IEnumerable<int> GetLayerCoordinates()
138 {
139 return RoomsByLayer.Keys;
140 }
141
149 public DoorConnection FindDoorConnection(Uid id, Vector2DInt position, DoorDirection direction)
150 {
151 foreach (var connection in GetDoorConnections(id))
152 {
153 if (connection.ContainsDoor(id, position, direction))
154 return connection;
155 }
156
157 return null;
158 }
159 }
160}
Holds the current Layout and LayoutState.
Definition: LayoutPack.cs:13
Dictionary< int, List< Room > > RoomsByLayer
A dictionary of rooms by layer coordinate.
Definition: LayoutPack.cs:37
LayoutState LayoutState
The layout state.
Definition: LayoutPack.cs:22
LayoutPack(Layout layout, LayoutState state, string settingsPath)
Initializes a new layout pack.
Definition: LayoutPack.cs:50
DoorConnection FindDoorConnection(Uid id, Vector2DInt position, DoorDirection direction)
Returns the door connection with the given room ID, position, and direction if it exists....
Definition: LayoutPack.cs:149
IReadOnlyList< DoorPosition > GetRoomDoors(Uid id)
Returns a list of door positions for the specified room ID. If the room ID does not exist,...
Definition: LayoutPack.cs:109
Dictionary< Uid, List< DoorConnection > > RoomConnections
A dictionary of door connections by room ID.
Definition: LayoutPack.cs:32
IReadOnlyList< DoorConnection > GetDoorConnections(Uid id)
Returns a list of door connections with a room for the specified room ID. If the room ID does not exi...
Definition: LayoutPack.cs:85
IReadOnlyList< Room > GetRoomsInLayer(int z)
Returns a list of rooms corresponding to the specified layer (z) coordinate. If the layer does not ex...
Definition: LayoutPack.cs:97
bool DoorExists(Uid id, Vector2DInt position, DoorDirection direction)
Returns true if the door exists for the specified cell index and direction.
Definition: LayoutPack.cs:122
ManiaMapSettings Settings
The settings.
Definition: LayoutPack.cs:27
LayoutPack(Layout layout, LayoutState state, ManiaMapSettings settings=null)
Initializes a new layout pack.
Definition: LayoutPack.cs:64
IEnumerable< int > GetLayerCoordinates()
Returns an enumerable of unique layer (z) coordinates for the layout. The coordinates are not in any ...
Definition: LayoutPack.cs:137
Dictionary< Uid, List< DoorPosition > > RoomDoors
A dictionary of door positions by room ID.
Definition: LayoutPack.cs:42
Contains various runtime settings.