summaryrefslogtreecommitdiff
path: root/DungeonSystem/scripts/Room.cs
blob: a00af285fd668dbaf5696076828a15bca7f4674a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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;
    [Export] public RoomEncounterConfig? EncounterConfig { get; set; }

    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);
    }
}