ManiaMap.Unity
Procedural generation of metroidvania style maps for Unity.
RoomDatabase.cs
1using MPewsey.ManiaMap;
2using MPewsey.ManiaMap.Exceptions;
3using System.Collections.Generic;
4using System.Linq;
5using UnityEngine;
6
8{
12 [CreateAssetMenu(menuName = "Mania Map/Room Database")]
13 public class RoomDatabase : ScriptableObject
14 {
15 [SerializeField]
16 private List<RoomComponent> _rooms = new List<RoomComponent>();
20 public List<RoomComponent> Rooms { get => _rooms; set => _rooms = value; }
21
25 private Dictionary<int, RoomComponent> RoomsByTemplateId { get; } = new Dictionary<int, RoomComponent>();
26
30 public bool IsDirty { get; private set; } = true;
31
32 private void Awake()
33 {
34 IsDirty = true;
35 }
36
37 private void OnValidate()
38 {
39 IsDirty = true;
40 }
41
45 public IReadOnlyDictionary<int, RoomComponent> GetRoomsByTemplateId()
46 {
48 return RoomsByTemplateId;
49 }
50
54 public void MarkDirty()
55 {
56 IsDirty = true;
57 }
58
62 private void PopulateIfDirty()
63 {
64 if (IsDirty)
65 {
67 IsDirty = false;
68 }
69 }
70
76 {
77 RoomsByTemplateId.Clear();
78
79 foreach (var room in Rooms)
80 {
81 var id = room.RoomTemplate.Id;
82
83 if (!RoomsByTemplateId.TryGetValue(id, out var storedRoom))
84 RoomsByTemplateId.Add(id, room);
85 else if (room != storedRoom)
86 throw new DuplicateIdException($"Duplicate room template ID: (ID = {id}, Room1 = {storedRoom}, Room2 = {room}).");
87 }
88 }
89
95 {
97 return RoomsByTemplateId[id];
98 }
99
105 public RoomComponent GetRoomPrefab(Uid id, LayoutPack layoutPack)
106 {
107 var room = layoutPack.Layout.Rooms[id];
108 return GetRoomPrefab(room.Template.Id);
109 }
110
116 public List<RoomComponent> InstantiateAllRooms(LayoutPack layoutPack, Transform parent = null)
117 {
118 var result = new List<RoomComponent>(layoutPack.Layout.Rooms.Count);
119
120 foreach (var room in layoutPack.Layout.Rooms.Values)
121 {
122 var prefab = GetRoomPrefab(room.Template.Id).gameObject;
123 var roomInstance = RoomComponent.InstantiateRoom(room.Id, layoutPack, prefab, parent, true);
124 result.Add(roomInstance.GetComponent<RoomComponent>());
125 }
126
127 return result;
128 }
129
136 public List<RoomComponent> InstantiateRooms(LayoutPack layoutPack, int? z = null, Transform parent = null)
137 {
138 z ??= layoutPack.Layout.Rooms.Values.Select(x => x.Position.Z).First();
139 var result = new List<RoomComponent>();
140
141 foreach (var room in layoutPack.Layout.Rooms.Values)
142 {
143 if (room.Position.Z == z)
144 {
145 var prefab = GetRoomPrefab(room.Template.Id).gameObject;
146 var roomInstance = RoomComponent.InstantiateRoom(room.Id, layoutPack, prefab, parent, true);
147 result.Add(roomInstance.GetComponent<RoomComponent>());
148 }
149 }
150
151 return result;
152 }
153
161 public RoomComponent InstantiateRoom(Uid id, LayoutPack layoutPack, Transform parent = null, bool assignLayoutPosition = false)
162 {
163 var prefab = GetRoomPrefab(id, layoutPack).gameObject;
164 var roomInstance = RoomComponent.InstantiateRoom(id, layoutPack, prefab, parent, assignLayoutPosition);
165 return roomInstance.GetComponent<RoomComponent>();
166 }
167 }
168}
A manager for maintaining the current map data and state.
Definition: LayoutPack.cs:12
A component for creating a room.
static RoomComponent InstantiateRoom(Uid id, LayoutPack layoutPack, GameObject prefab, Transform parent=null, bool assignLayoutPosition=false)
Instantiates and initializes a room and returns it.
A database of room prefabs.
Definition: RoomDatabase.cs:14
List< RoomComponent > Rooms
A list of room prefabs.
Definition: RoomDatabase.cs:20
RoomComponent InstantiateRoom(Uid id, LayoutPack layoutPack, Transform parent=null, bool assignLayoutPosition=false)
Instantiates the room with the specified ID.
List< RoomComponent > InstantiateRooms(LayoutPack layoutPack, int? z=null, Transform parent=null)
Instantiates the rooms in a layer of a layout and returns a list of them.
void PopulateRoomsByTemplateId()
Populates the room dictionary.
Definition: RoomDatabase.cs:75
void PopulateIfDirty()
If the object is dirty, populates the room dictionary.
Definition: RoomDatabase.cs:62
Dictionary< int, RoomComponent > RoomsByTemplateId
A dictionary of room prefabs by room template ID.
Definition: RoomDatabase.cs:25
RoomComponent GetRoomPrefab(int id)
Returns the room prefab with the specified template ID.
Definition: RoomDatabase.cs:94
IReadOnlyDictionary< int, RoomComponent > GetRoomsByTemplateId()
Returns the dictionary of rooms by room template ID.
Definition: RoomDatabase.cs:45
List< RoomComponent > InstantiateAllRooms(LayoutPack layoutPack, Transform parent=null)
Instantiates all rooms in the layout and returns a list of them.
void MarkDirty()
Sets the object as dirty.
Definition: RoomDatabase.cs:54
RoomComponent GetRoomPrefab(Uid id, LayoutPack layoutPack)
Returns the room prefab with the specified room ID.
bool IsDirty
If true, the database is dirty and requires population.
Definition: RoomDatabase.cs:30