diff options
Diffstat (limited to 'DungeonSystem/scripts')
| -rw-r--r-- | DungeonSystem/scripts/Door.cs | 55 | ||||
| -rw-r--r-- | DungeonSystem/scripts/Door.cs.uid | 1 | ||||
| -rw-r--r-- | DungeonSystem/scripts/DungeonGenerator.cs | 168 | ||||
| -rw-r--r-- | DungeonSystem/scripts/DungeonGenerator.cs.uid | 1 | ||||
| -rw-r--r-- | DungeonSystem/scripts/Room.cs | 70 | ||||
| -rw-r--r-- | DungeonSystem/scripts/Room.cs.uid | 1 |
6 files changed, 296 insertions, 0 deletions
diff --git a/DungeonSystem/scripts/Door.cs b/DungeonSystem/scripts/Door.cs new file mode 100644 index 0000000..0a970b3 --- /dev/null +++ b/DungeonSystem/scripts/Door.cs @@ -0,0 +1,55 @@ +using Godot; + +namespace DungeonSystem; + +[GlobalClass] +public partial class Door : Node2D +{ + [Signal] + public delegate void PlayerEnteredDoorwayEventHandler(Door door); + + public enum DoorState { Open, Locked } + public DoorState CurrentState { get; private set; } = DoorState.Open; + + private CollisionShape2D _barrierCollision = null!; + private Area2D _triggerArea = null!; + private AnimationPlayer _animPlayer = null!; + + public override void _Ready() + { + _barrierCollision = GetNode<CollisionShape2D>("Barrier/CollisionShape2D"); + _triggerArea = GetNode<Area2D>("TriggerArea"); + _animPlayer = GetNode<AnimationPlayer>("AnimationPlayer"); + + _triggerArea.BodyEntered += OnBodyEntered; + UnlockDoor(); + } + + public void LockDoor() + { + if (CurrentState == DoorState.Locked) return; + + CurrentState = DoorState.Locked; + _barrierCollision.SetDeferred(CollisionShape2D.PropertyName.Disabled, false); + + if (_animPlayer.HasAnimation("lock")) + _animPlayer.Play("lock"); + } + + public void UnlockDoor() + { + CurrentState = DoorState.Open; + _barrierCollision.SetDeferred(CollisionShape2D.PropertyName.Disabled, true); + + if (_animPlayer.HasAnimation("unlock")) + _animPlayer.Play("unlock"); + } + + private void OnBodyEntered(Node2D body) + { + if (CurrentState == DoorState.Open && body.IsInGroup("player")) + { + EmitSignal(SignalName.PlayerEnteredDoorway, this); + } + } +}
\ No newline at end of file diff --git a/DungeonSystem/scripts/Door.cs.uid b/DungeonSystem/scripts/Door.cs.uid new file mode 100644 index 0000000..41e8e2e --- /dev/null +++ b/DungeonSystem/scripts/Door.cs.uid @@ -0,0 +1 @@ +uid://csm4y4okldumw diff --git a/DungeonSystem/scripts/DungeonGenerator.cs b/DungeonSystem/scripts/DungeonGenerator.cs new file mode 100644 index 0000000..dd59698 --- /dev/null +++ b/DungeonSystem/scripts/DungeonGenerator.cs @@ -0,0 +1,168 @@ +using Godot; +using Godot.Collections; + +namespace DungeonSystem; + +public partial class DungeonGenerator : Node2D +{ + [Export] public PackedScene StartRoomScene { get; set; } = null!; + [Export] public Array<PackedScene> CombatRoomScenes { get; set; } = new(); + [Export] public PackedScene ShopRoomScene { get; set; } = null!; + [Export] public PackedScene TreasureRoomScene { get; set; } = null!; + [Export] public PackedScene BossRoomScene { get; set; } = null!; + [Export] public PackedScene DoorScene { get; set; } = null!; + + [Export] public int TargetRoomCount { get; set; } = 10; + [Export] public int MaxAttempts { get; set; } = 100; + + // --- ADD THESE TWO LINES --- + private readonly Array<Room> _placedRooms = new(); + private readonly Array<Marker2D> _openDoors = new(); + // --------------------------- + + public override void _Ready() + { + GenerateDungeon(); + } + + public void GenerateDungeon() + { + // 1. Instantiate Start Room + if (StartRoomScene.Instantiate() is not Room startRoom) return; + + AddChild(startRoom); + startRoom.GlobalPosition = Vector2.Zero; + _placedRooms.Add(startRoom); + _openDoors.AddRange(startRoom.GetOpenDoors()); + + // 2. Main Generation Loop + int attempts = 0; + while (_openDoors.Count > 0 && _placedRooms.Count < TargetRoomCount && attempts < MaxAttempts) + { + attempts++; + Marker2D targetDoor = _openDoors[(int)(GD.Randi() % (uint)_openDoors.Count)]; + _TryAttachRoom(targetDoor); + } + + // 3. Post-Process Doors + SpawnDoorsAndSealUnconnected(); + } + + private bool _TryAttachRoom(Marker2D targetDoor) + { + if (CombatRoomScenes.Count == 0) return false; + + PackedScene candidateScene = CombatRoomScenes[(int)(GD.Randi() % (uint)CombatRoomScenes.Count)]; + if (candidateScene.Instantiate() is not Room candidate) return false; + + AddChild(candidate); + + Array<Marker2D> candidateDoors = candidate.GetOpenDoors(); + if (candidateDoors.Count == 0) + { + RemoveChild(candidate); + candidate.QueueFree(); + return false; + } + + Marker2D entryDoor = candidateDoors[(int)(GD.Randi() % (uint)candidateDoors.Count)]; + + // Align candidate entry door directly to target door position + Vector2 offset = targetDoor.GlobalPosition - entryDoor.GlobalPosition; + candidate.GlobalPosition += offset; + + if (CheckOverlap(candidate)) + { + RemoveChild(candidate); + candidate.QueueFree(); + return false; + } + + _placedRooms.Add(candidate); + + targetDoor.SetMeta("connected", true); + entryDoor.SetMeta("connected", true); + + _openDoors.Remove(targetDoor); + _openDoors.AddRange(candidate.GetOpenDoors()); + + return true; + } + private bool CheckOverlap(Room candidate) + { + Rect2 candidateRect = candidate.GetRoomRect().Grow(-2.0f); + + foreach (Room room in _placedRooms) + { + if (candidateRect.Intersects(room.GetRoomRect().Grow(-2.0f))) + { + return true; + } + } + + return false; + } + + private void SpawnDoorsAndSealUnconnected() + { + foreach (Room room in _placedRooms) + { + var doorsContainer = room.DoorsContainer; + if (doorsContainer == null) continue; + + foreach (Node child in doorsContainer.GetChildren()) + { + if (child is not Marker2D marker) continue; + if (!marker.Visible) continue; + bool isConnected = marker.HasMeta("connected") && marker.GetMeta("connected").AsBool(); + + if (isConnected) + { + if (marker.HasMeta("door_spawned")) continue; + _InstantiateDoorAtMarker(room, marker); + } + else + { + _SealDoorwayWithWall(room, marker); + } + } + } + } + + private void _InstantiateDoorAtMarker(Room room, Marker2D marker) + { + if (DoorScene == null) return; + + Door doorInstance = DoorScene.Instantiate<Door>(); + + // Add to DungeonGenerator parent directly so it isn't affected by individual room deletion + AddChild(doorInstance); + doorInstance.GlobalPosition = marker.GlobalPosition; + doorInstance.ZIndex = 1; // Ensure it renders above tilemaps + + if (marker.HasMeta("direction")) + { + Vector2 dir = marker.GetMeta("direction").AsVector2(); + if (dir.X != 0) + { + doorInstance.RotationDegrees = 90; + } + } + + marker.SetMeta("door_spawned", true); + } + + private void _SealDoorwayWithWall(Room room, Marker2D marker) + { + var tileMapLayer = room.GetNodeOrNull<TileMapLayer>("TileMapLayer"); + if (tileMapLayer == null) return; + + // Use Vector2I (Capital I) + Vector2I cellPos = tileMapLayer.LocalToMap(tileMapLayer.ToLocal(marker.GlobalPosition)); + + int sourceId = 0; + Vector2I wallAtlasCoords = new Vector2I(1, 0); + + tileMapLayer.SetCell(cellPos, sourceId, wallAtlasCoords); + } +}
\ No newline at end of file diff --git a/DungeonSystem/scripts/DungeonGenerator.cs.uid b/DungeonSystem/scripts/DungeonGenerator.cs.uid new file mode 100644 index 0000000..d84e3ea --- /dev/null +++ b/DungeonSystem/scripts/DungeonGenerator.cs.uid @@ -0,0 +1 @@ +uid://njkfvxsw6cji 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 diff --git a/DungeonSystem/scripts/Room.cs.uid b/DungeonSystem/scripts/Room.cs.uid new file mode 100644 index 0000000..a44005c --- /dev/null +++ b/DungeonSystem/scripts/Room.cs.uid @@ -0,0 +1 @@ +uid://b80pkquwjm7is |
