diff options
| author | alex <[email protected]> | 2026-10-15 10:07:47 +0200 |
|---|---|---|
| committer | alex <[email protected]> | 2026-10-15 10:07:47 +0200 |
| commit | be7856168c790ef9dc6478cc429633b032ca09b5 (patch) | |
| tree | e446bd577e4948fec711a17d395c734ad47b4d2f /DungeonSystem/scripts/DungeonGenerator.cs | |
| parent | f0c870bb9dcffd79d0ad4e361d9f9f2a2ff807f8 (diff) | |
| download | godot-testing-be7856168c790ef9dc6478cc429633b032ca09b5.tar.xz godot-testing-be7856168c790ef9dc6478cc429633b032ca09b5.zip | |
zas vela veci
Diffstat (limited to 'DungeonSystem/scripts/DungeonGenerator.cs')
| -rw-r--r-- | DungeonSystem/scripts/DungeonGenerator.cs | 168 |
1 files changed, 168 insertions, 0 deletions
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 |
