ManiaMap.Godot
Procedural generation of metroidvania style maps for Godot .NET.
DoorNode2D.cs
1using Godot;
2using MPewsey.ManiaMap;
3using System.Collections.Generic;
4
6{
12 [Tool]
13 [GlobalClass]
14 [Icon(ManiaMapResources.Icons.DoorNode2DIcon)]
15 public partial class DoorNode2D : CellChild2D, IDoorNode
16 {
20 private static Dictionary<Uid, LinkedList<DoorNode2D>> ActiveRoomDoors { get; } = new Dictionary<Uid, LinkedList<DoorNode2D>>();
21
23 [Export] public bool AutoAssignDirection { get; set; } = true;
24
26 [Export] public DoorDirection DoorDirection { get; set; }
27
29 [Export] public DoorType DoorType { get; set; }
30
32 [ExportGroup("Door Code")]
33 [Export(PropertyHint.Flags, ManiaMapResources.Enums.DoorCodeFlags)] public int DoorCode { get; set; }
34
36 public DoorConnection DoorConnection { get; private set; }
37
38 public override void _Ready()
39 {
40 base._Ready();
41
42 if (!Engine.IsEditorHint() && Room.IsInitialized)
43 DoorConnection = this.FindDoorConnection();
44 }
45
46 public override void _EnterTree()
47 {
48 base._EnterTree();
49
50 if (!Engine.IsEditorHint())
52 }
53
54 public override void _ExitTree()
55 {
56 base._ExitTree();
57
58 if (!Engine.IsEditorHint())
60 }
61
63 public override void AutoAssign(RoomNode2D room)
64 {
65 base.AutoAssign(room);
66
68 DoorDirection = room.FindClosestDoorDirection(Row, Column, GlobalPosition);
69 }
70
74 private void AddToActiveRoomDoors()
75 {
77 {
78 var roomId = Room.RoomLayout.Id;
79
80 if (!ActiveRoomDoors.TryGetValue(roomId, out var doors))
81 {
82 doors = new LinkedList<DoorNode2D>();
83 ActiveRoomDoors.Add(roomId, doors);
84 }
85
86 doors.AddLast(this);
87 }
88 }
89
94 {
96 {
97 var roomId = Room.RoomLayout.Id;
98
99 if (ActiveRoomDoors.TryGetValue(roomId, out var doors))
100 {
101 doors.Remove(this);
102
103 if (doors.Count == 0)
104 ActiveRoomDoors.Remove(roomId);
105 }
106 }
107 }
108
114 public static DoorNode2D FindActiveDoor(Uid roomId, DoorConnection doorConnection)
115 {
116 if (doorConnection != null && ActiveRoomDoors.TryGetValue(roomId, out var doors))
117 {
118 foreach (var door in doors)
119 {
120 if (door.DoorConnection == doorConnection)
121 return door;
122 }
123 }
124
125 return null;
126 }
127 }
128}
The base class for elements tied to a RoomNode2D's cell index.
Definition: CellChild2D.cs:11
RoomNode2D Room
The contained room.
Definition: CellChild2D.cs:15
A possible door location connecting two RoomNode2D.
Definition: DoorNode2D.cs:16
void AddToActiveRoomDoors()
Adds the door to the active room doors dictionary.
Definition: DoorNode2D.cs:74
override void AutoAssign(RoomNode2D room)
Assigns the room and any auto assigned values to the object.
Definition: DoorNode2D.cs:63
void RemoveFromActiveRoomDoors()
Removes the door from the active room doors dictionary.
Definition: DoorNode2D.cs:93
static DoorNode2D FindActiveDoor(Uid roomId, DoorConnection doorConnection)
Returns the door in the scene with the matching room ID and door connection.
Definition: DoorNode2D.cs:114
static Dictionary< Uid, LinkedList< DoorNode2D > > ActiveRoomDoors
A dictionary of doors in the scene by room ID.
Definition: DoorNode2D.cs:20
const string DoorCodeFlags
The door code enum string.
Contains resource references for the project.
A node serving as the top level of a 2D room.
Definition: RoomNode2D.cs:17
DoorDirection FindClosestDoorDirection(int row, int column, Vector2 position)
Returns the closest door direction based on the cell index and specified global position....
Definition: RoomNode2D.cs:283
A possible door location connecting two IRoomNode.
Definition: IDoorNode.cs:9