2using MPewsey.ManiaMap.Exceptions;
3using System.Collections.Generic;
5using System.Threading.Tasks;
7using UnityEngine.ResourceManagement.AsyncOperations;
14 [CreateAssetMenu(menuName =
"Mania Map/Room Template Database")]
18 private List<TemplateGroup> _templateGroups =
new List<TemplateGroup>();
22 public List<TemplateGroup>
TemplateGroups {
get => _templateGroups;
set => _templateGroups = value; }
27 private Dictionary<int, RoomTemplateResource>
RoomTemplates {
get; } =
new Dictionary<int, RoomTemplateResource>();
32 public bool IsDirty {
get;
private set; } =
true;
39 private void OnValidate()
83 foreach (var entry
in group.Entries)
85 var
id = entry.Template.Id;
89 else if (
template != entry.Template)
90 throw new DuplicateIdException($
"Duplicate room template ID: (ID = {id}, Template1 = {template}, Template2 = {entry.Template}).");
112 var room = layoutPack.
Layout.Rooms[id];
123 var result =
new List<RoomComponent>(layoutPack.
Layout.Rooms.Count);
125 foreach (var room
in layoutPack.Layout.Rooms.Values)
144 var result =
new List<RoomComponent>(layoutPack.
Layout.Rooms.Count);
146 foreach (var room
in layoutPack.Layout.Rooms.Values)
150 var roomInstance = handle.WaitForCompletion();
165 z ??= layoutPack.
Layout.Rooms.Values.Select(x => x.Position.Z).First();
166 var result =
new List<RoomComponent>();
168 foreach (var room
in layoutPack.Layout.Rooms.Values)
170 if (room.Position.Z == z)
189 foreach (var room
in rooms)
191 room.gameObject.SetActive(
true);
203 room.gameObject.SetActive(
false);
215 z ??= layoutPack.
Layout.Rooms.Values.Select(x => x.Position.Z).First();
216 var result =
new List<RoomComponent>();
218 foreach (var room
in layoutPack.Layout.Rooms.Values)
220 if (room.Position.Z == z)
224 var roomInstance = handle.WaitForCompletion();
254 var result =
InstantiateRoomAsync(
id, layoutPack, parent, assignLayoutPosition).WaitForCompletion();
A manager for maintaining the current map data and state.
A component for creating a room.
static AsyncOperationHandle< GameObject > InstantiateRoomAsync(Uid id, LayoutPack layoutPack, AssetReferenceGameObject prefab, Transform parent=null, bool assignLayoutPosition=false)
Instantiates and initializes a room asynchronously. Returns the operation handle.
A database of room templates whose room can be instantiated via Addressables.
RoomComponent InstantiateRoom(Uid id, LayoutPack layoutPack, Transform parent=null, bool assignLayoutPosition=false)
Instantiates the specified room and returns it.
List< RoomComponent > InstantiateRooms(LayoutPack layoutPack, Transform parent=null, int? z=null)
Instantiates the rooms in the specified layer of the layout.
IReadOnlyDictionary< int, RoomTemplateResource > GetRoomTemplates()
Returns the dictionary of room templates by ID.
void PopulateIfDirty()
If the object is dirty, populates the room templates dictionary.
Dictionary< int, RoomTemplateResource > RoomTemplates
A dictionary of room templates by ID.
AsyncOperationHandle< GameObject > InstantiateRoomAsync(Uid id, LayoutPack layoutPack, Transform parent=null, bool assignLayoutPosition=false)
Instantiates the specified room asynchronously. Returns an operation handle with the result.
RoomTemplateResource GetRoomTemplate(Uid id, LayoutPack layoutPack)
Returns the room template for the specified room ID.
async Task< List< RoomComponent > > InstantiateRoomsAsync(LayoutPack layoutPack, Transform parent=null, int? z=null)
Instantiates the rooms in a specified layer of the layout asynchronously. Returns the rooms as a resu...
static void ActivateRooms(List< RoomComponent > rooms)
Activates the rooms in the specified list.
List< TemplateGroup > TemplateGroups
A list of template groups.
RoomTemplateResource GetRoomTemplate(int id)
Returns the room template with the specified ID.
void PopulateRoomTemplates()
Populates the room templates dictionary.
async Task< List< RoomComponent > > InstantiateAllRoomsAsync(LayoutPack layoutPack, Transform parent=null)
Instantiates all rooms in the layout asynchronously and returns the rooms as a result of the task.
static void OnInstantiationComplete(AsyncOperationHandle< GameObject > handle, List< RoomComponent > results)
Adds the room from the operation handle result to the results list and deactivates it.
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.
bool IsDirty
If true, the object is dirty and requires population.
A container for storing a serialized room template.
AssetReferenceGameObject GetAssetReference()
Returns the asset reference based on the assigned prefab GUID.