diff options
Diffstat (limited to 'DungeonSystem/scripts/Room.cs')
| -rw-r--r-- | DungeonSystem/scripts/Room.cs | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/DungeonSystem/scripts/Room.cs b/DungeonSystem/scripts/Room.cs new file mode 100644 index 0000000..cbd07b9 --- /dev/null +++ b/DungeonSystem/scripts/Room.cs @@ -0,0 +1,70 @@ +using Godot; +using Godot.Collections; + +namespace DungeonSystem; + +[GlobalClass] +public partial class Room : Node2D +{ + public enum RoomType { Start, Combat, Shop, Treasure, Boss } + + [Export] public RoomType Type { get; set; } = RoomType.Combat; + + public Area2D? RoomBounds => GetNodeOrNull<Area2D>("RoomBounds"); + public CollisionShape2D? CollisionShape => GetNodeOrNull<CollisionShape2D>("RoomBounds/CollisionShape2D"); + public Node2D? DoorsContainer => GetNodeOrNull<Node2D>("Doors"); + + public Array<Marker2D> GetOpenDoors() + { + var openDoors = new Array<Marker2D>(); + var doors = DoorsContainer; + + if (doors == null) + { + GD.PrintErr($"[Room.cs] Missing 'Doors' child node in room scene: '{Name}' ({SceneFilePath})!"); + return openDoors; + } + + foreach (Node child in doors.GetChildren()) + { + // NEW: Only add the door if it is VISIBLE (Eye icon is open) + if (child is Marker2D door && door.Visible) + { + bool isConnected = door.HasMeta("connected") && door.GetMeta("connected").AsBool(); + if (!isConnected) + { + openDoors.Add(door); + } + } + } + + return openDoors; + } + + /// <summary> + /// Dynamically calculates the room's global bounding rectangle directly from its TileMapLayer. + /// </summary> + public Rect2 GetRoomRect() + { + var tileMapLayer = GetNodeOrNull<TileMapLayer>("TileMapLayer"); + if (tileMapLayer == null || tileMapLayer.TileSet == null) + { + GD.PrintErr($"[Room.cs] Missing 'TileMapLayer' or 'TileSet' in scene: '{Name}'!"); + return new Rect2(GlobalPosition, Vector2.Zero); + } + + // Get the used rectangle in tile cell coordinates (e.g., Position = (-5, -5), Size = (10, 10)) + Rect2I usedCellsRect = tileMapLayer.GetUsedRect(); + Vector2 tileSize = tileMapLayer.TileSet.TileSize; + + // Find the top-left pixel position of the top-left tile + Vector2 topLeftCellCenter = tileMapLayer.MapToLocal(usedCellsRect.Position); + Vector2 topLeftPixelLocal = topLeftCellCenter - (tileSize / 2.0f); + + // Convert local pixel bounds to global world coordinates + Vector2 globalPos = tileMapLayer.ToGlobal(topLeftPixelLocal); + Vector2 globalSize = usedCellsRect.Size * tileSize; + + return new Rect2(globalPos, globalSize); + } +}
\ No newline at end of file |
