ManiaMap.Unity
Procedural generation of metroidvania style maps for Unity.
DoorComponent.cs
1using MPewsey.Common.Mathematics;
2using MPewsey.ManiaMap;
3using System.Collections.Generic;
4using UnityEngine;
5
7{
11 public class DoorComponent : CellChild
12 {
16 private static Dictionary<Uid, LinkedList<DoorComponent>> ActiveDoorsByRoom { get; } = new Dictionary<Uid, LinkedList<DoorComponent>>();
17
18 [Header("Door:")]
19 [SerializeField]
20 private bool _autoAssignDirection = true;
25 public bool AutoAssignDirection { get => _autoAssignDirection; set => _autoAssignDirection = value; }
26
27 [SerializeField]
28 private DoorDirection _direction;
32 public DoorDirection Direction { get => _direction; set => _direction = value; }
33
34 [SerializeField]
35 private DoorType _type;
39 public DoorType Type { get => _type; set => _type = value; }
40
41 [SerializeField]
42 private DoorCode _code;
46 public DoorCode Code { get => _code; set => _code = value; }
47
51 public DoorConnection Connection { get; private set; }
52
56 public bool DoorExists()
57 {
58 return Connection != null;
59 }
60
64 public Uid ToRoomId()
65 {
66 if (!DoorExists())
67 return new Uid(-1, -1, -1);
68
69 return Connection.GetConnectingRoom(Room.RoomLayout.Id);
70 }
71
72 protected override void OnDestroy()
73 {
74 base.OnDestroy();
76 }
77
79 protected override void Initialize()
80 {
81 if (!IsInitialized)
82 {
83 IsInitialized = true;
86 OnInitialize.Invoke();
87 }
88 }
89
96 public static DoorComponent FindDoor(Uid roomId, DoorConnection connection)
97 {
98 if (connection != null && ActiveDoorsByRoom.TryGetValue(roomId, out var doors))
99 {
100 foreach (var door in doors)
101 {
102 if (door.Connection == connection)
103 return door;
104 }
105 }
106
107 return null;
108 }
109
113 private void AddToActiveDoors()
114 {
115 var id = Room.RoomLayout.Id;
116
117 if (!ActiveDoorsByRoom.TryGetValue(id, out var doors))
118 {
119 doors = new LinkedList<DoorComponent>();
120 ActiveDoorsByRoom.Add(id, doors);
121 }
122
123 doors.AddLast(this);
124 }
125
130 {
131 if (Room.IsInitialized && ActiveDoorsByRoom.TryGetValue(Room.RoomLayout.Id, out var doors))
132 doors.Remove(this);
133 }
134
139 private DoorConnection FindDoorConnection()
140 {
141 var roomId = Room.RoomLayout.Id;
142 var position = new Vector2DInt(Row, Column);
143 return Room.LayoutPack.FindDoorConnection(roomId, position, Direction);
144 }
145
149 public override void AutoAssign(RoomComponent room)
150 {
151 base.AutoAssign(room);
152
154 Direction = room.FindClosestDoorDirection(Row, Column, transform.position);
155 }
156
160 public Door GetMMDoor()
161 {
162 return new Door(Type, Code);
163 }
164 }
165}
An object tied to the cell index of a RoomComponent.
Definition: CellChild.cs:10
UnityEvent OnInitialize
The event invoked after the object is initialized.
Definition: CellChild.cs:43
RoomComponent Room
The containing room.
Definition: CellChild.cs:37
int Column
The column index.
Definition: CellChild.cs:53
bool IsInitialized
True if the object has been initialized.
Definition: CellChild.cs:23
A component representing a possible door location.
bool DoorExists()
True if the door exists in the layout.
Uid ToRoomId()
The room ID of the room this door connects to.
bool AutoAssignDirection
If true, the door direction will be automatically assigned to the cell when update and save operation...
Door GetMMDoor()
Returns the Mania Map door used by the procedural generator.
DoorConnection FindDoorConnection()
Returns the door connection in the layout associated with the door. Returns null if the door connecti...
void RemoveFromActiveDoors()
Removes the door from the doors dictionary.
static DoorComponent FindDoor(Uid roomId, DoorConnection connection)
Finds the door with the specified room ID and door connection. Returns null if the door is not found.
override void AutoAssign(RoomComponent room)
Auto assigns elements to the door.
DoorConnection Connection
The associated door connection in the layout.
DoorDirection Direction
The door direction.
override void Initialize()
Initializes the object.
void AddToActiveDoors()
Adds the door to the doors dictionary.
static Dictionary< Uid, LinkedList< DoorComponent > > ActiveDoorsByRoom
A dictionary of door components in the scene by their room ID.
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
A component for creating a room.
LayoutPack LayoutPack
The layout pack.
DoorDirection FindClosestDoorDirection(int row, int column, Vector3 position)
Returns the closest door direction based on the specified global position.
bool IsInitialized
True if the room has been initialized.