ManiaMap.Unity
Procedural generation of metroidvania style maps for Unity.
LayoutPack.cs
1using MPewsey.Common.Mathematics;
2using MPewsey.ManiaMap;
3using System.Collections.Generic;
4using UnityEngine;
5
7{
11 public class LayoutPack
12 {
16 public Layout Layout { get; }
17
21 public LayoutState LayoutState { get; }
22
26 public ManiaMapSettings Settings { get; }
27
31 private Dictionary<Uid, List<DoorConnection>> RoomConnections { get; } = new Dictionary<Uid, List<DoorConnection>>();
32
36 private Dictionary<int, List<Room>> RoomsByLayer { get; } = new Dictionary<int, List<Room>>();
37
41 private Dictionary<Uid, List<DoorPosition>> RoomDoors { get; } = new Dictionary<Uid, List<DoorPosition>>();
42
46 public RectangleInt LayoutBounds { get; }
47
55 public LayoutPack(Layout layout, LayoutState layoutState, ManiaMapSettings settings = null)
56 {
57 if (layout == null)
58 throw new System.ArgumentException("Layout cannot be null.");
59 if (layoutState == null)
60 throw new System.ArgumentException("Layout state cannot be null.");
61 if (layout.Id != layoutState.Id)
62 throw new System.ArgumentException($"Layout and layout state ID's do not match: (Layout ID = {layout.Id}, Layout State ID = {layoutState.Id})");
63
64 Layout = layout;
65 LayoutState = layoutState;
66 Settings = settings != null ? settings : ScriptableObject.CreateInstance<ManiaMapSettings>();
67 RoomConnections = layout.GetRoomConnections();
68 RoomsByLayer = layout.GetRoomsByLayer();
69 RoomDoors = layout.GetRoomDoors();
70 LayoutBounds = layout.GetBounds();
71 }
72
78 public IReadOnlyList<DoorConnection> GetDoorConnections(Uid id)
79 {
80 if (RoomConnections.TryGetValue(id, out var connections))
81 return connections;
82 return System.Array.Empty<DoorConnection>();
83 }
84
90 public IReadOnlyList<Room> GetRoomsInLayer(int z)
91 {
92 if (RoomsByLayer.TryGetValue(z, out var rooms))
93 return rooms;
94 return System.Array.Empty<Room>();
95 }
96
102 public IReadOnlyList<DoorPosition> GetRoomDoors(Uid id)
103 {
104 if (RoomDoors.TryGetValue(id, out var doors))
105 return doors;
106 return System.Array.Empty<DoorPosition>();
107 }
108
115 public bool DoorExists(Uid id, Vector2DInt position, DoorDirection direction)
116 {
117 foreach (var door in GetRoomDoors(id))
118 {
119 if (door.Matches(position, direction))
120 return true;
121 }
122
123 return false;
124 }
125
130 public IEnumerable<int> GetLayerCoordinates()
131 {
132 return RoomsByLayer.Keys;
133 }
134
142 public DoorConnection FindDoorConnection(Uid id, Vector2DInt position, DoorDirection direction)
143 {
144 foreach (var connection in GetDoorConnections(id))
145 {
146 if (connection.ContainsDoor(id, position, direction))
147 return connection;
148 }
149
150 return null;
151 }
152 }
153}
A manager for maintaining the current map data and state.
Definition: LayoutPack.cs:12
Dictionary< int, List< Room > > RoomsByLayer
A dictionary of rooms by layer coordinate.
Definition: LayoutPack.cs:36
LayoutPack(Layout layout, LayoutState layoutState, ManiaMapSettings settings=null)
Initializes a new object.
Definition: LayoutPack.cs:55
LayoutState LayoutState
The layout state.
Definition: LayoutPack.cs:21
RectangleInt LayoutBounds
The bounds of the layout.
Definition: LayoutPack.cs:46
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:142
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:102
Dictionary< Uid, List< DoorConnection > > RoomConnections
A dictionary of door connections by room ID.
Definition: LayoutPack.cs:31
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:78
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:90
bool DoorExists(Uid id, Vector2DInt position, DoorDirection direction)
Returns true if the door exists for the specified cell index and direction.
Definition: LayoutPack.cs:115
ManiaMapSettings Settings
The applied settings.
Definition: LayoutPack.cs:26
IEnumerable< int > GetLayerCoordinates()
Returns an enumerable of unique layer (z) coordinates for the layout. The coordinates are not in any ...
Definition: LayoutPack.cs:130
Dictionary< Uid, List< DoorPosition > > RoomDoors
A dictionary of door positions by room ID.
Definition: LayoutPack.cs:41
Contains settings used by Mania Map components.