Spawn doors on walls

This commit is contained in:
Marco 2025-04-23 14:27:30 +02:00
commit eab572ff2e
2 changed files with 41 additions and 65 deletions

View file

@ -51,7 +51,6 @@ MaxBranchLength = 2
MaxKeys = 2 MaxKeys = 2
MaxSecrets = 1 MaxSecrets = 1
MaxTreasures = 1 MaxTreasures = 1
Seed = 1
[node name="CameraController" type="Camera2D" parent="."] [node name="CameraController" type="Camera2D" parent="."]
process_mode = 1 process_mode = 1

View file

@ -20,12 +20,12 @@ public partial class RogueliteRoom : Node2D
public Vector2I BottomLeft => GridPosition + new Vector2I(0, RoomResource.Size.Y - 1); public Vector2I BottomLeft => GridPosition + new Vector2I(0, RoomResource.Size.Y - 1);
private Vector2 BaseRoomSize => new Vector2(320, 160); private Vector2 BaseRoomSize => new Vector2(320, 160);
public Vector2I RandomBottomExit() public Vector2I RandomBottomExit()
{ {
return BottomLeft + new Vector2I(GD.RandRange(0, RoomResource.Size.X - 1), 0); return BottomLeft + new Vector2I(GD.RandRange(0, RoomResource.Size.X - 1), 0);
} }
public Vector2I RandomExit(Direction direction) public Vector2I RandomExit(Direction direction)
{ {
return direction switch return direction switch
@ -37,10 +37,10 @@ public partial class RogueliteRoom : Node2D
new Vector2I(0, GD.RandRange(0, RoomResource.Size.Y - 1)), new Vector2I(0, GD.RandRange(0, RoomResource.Size.Y - 1)),
_ => throw new ArgumentOutOfRangeException(nameof(direction), direction, null) _ => throw new ArgumentOutOfRangeException(nameof(direction), direction, null)
}; };
} }
[Export] public PackedScene DoorPrefab { get; private set; } [Export] public PackedScene DoorPrefab { get; private set; }
[Export] public PackedScene VerticalDoorPrefab { get; private set; } [Export] public PackedScene VerticalDoorPrefab { get; private set; }
[Export] public PackedScene WallPrefab { get; private set; } [Export] public PackedScene WallPrefab { get; private set; }
@ -84,11 +84,11 @@ public partial class RogueliteRoom : Node2D
private List<DoorMarker> GenerateDoors() private List<DoorMarker> GenerateDoors()
{ {
List<DoorMarker> doors = []; List<DoorMarker> doors = [];
var doorsContainer = new Node2D(); var doorsContainer = new Node2D();
this.AddChild(doorsContainer); this.AddChild(doorsContainer);
doorsContainer.Name = "Doors"; doorsContainer.Name = "Doors";
// North // North
for (int i = 0; i < RoomResource.Size.X; i++) for (int i = 0; i < RoomResource.Size.X; i++)
{ {
@ -96,7 +96,7 @@ public partial class RogueliteRoom : Node2D
{ {
doors.Add(MakeDoorMarker(DoorDirections.North, i)); doors.Add(MakeDoorMarker(DoorDirections.North, i));
} }
if (RoomResource.HasDoors(DoorDirections.South)) if (RoomResource.HasDoors(DoorDirections.South))
{ {
doors.Add(MakeDoorMarker(DoorDirections.South, i)); doors.Add(MakeDoorMarker(DoorDirections.South, i));
@ -122,7 +122,7 @@ public partial class RogueliteRoom : Node2D
var doorMarker = new DoorMarker(); var doorMarker = new DoorMarker();
doorMarker.Direction = direction; doorMarker.Direction = direction;
doorMarker.WallIndex = wallIndex; doorMarker.WallIndex = wallIndex;
doorMarker.Position = GetDoorPosition(direction, wallIndex); doorMarker.Position = GetDoorPosition(direction, wallIndex);
return doorMarker; return doorMarker;
@ -131,7 +131,7 @@ public partial class RogueliteRoom : Node2D
public void HandleDoors(Func<Vector2I, Vector2I, RoomConnection> connectionChecker) public void HandleDoors(Func<Vector2I, Vector2I, RoomConnection> connectionChecker)
{ {
var doors = GenerateDoors(); var doors = GenerateDoors();
foreach (DoorMarker marker in doors) foreach (DoorMarker marker in doors)
{ {
var baseDir = marker.GetWorldDirection(); var baseDir = marker.GetWorldDirection();
@ -154,19 +154,19 @@ public partial class RogueliteRoom : Node2D
var connected = connection is not null; var connected = connection is not null;
var door = this.CreateChildOf<Door>(marker, marker.Direction switch
{
DoorDirections.North => DoorPrefab,
DoorDirections.South => DoorPrefab,
DoorDirections.East => VerticalDoorPrefab,
DoorDirections.West => VerticalDoorPrefab,
_ => throw new ArgumentOutOfRangeException()
}, marker.GlobalPosition);
door.State = DoorState.Closed;
if (connected) if (connected)
{ {
var door = this.CreateChildOf<Door>(marker, marker.Direction switch
{
DoorDirections.North => DoorPrefab,
DoorDirections.South => DoorPrefab,
DoorDirections.East => VerticalDoorPrefab,
DoorDirections.West => VerticalDoorPrefab,
_ => throw new ArgumentOutOfRangeException()
}, marker.GlobalPosition);
door.State = DoorState.Closed;
_doors.Add(door); _doors.Add(door);
if (doorEdge == connection.From) if (doorEdge == connection.From)
@ -179,41 +179,17 @@ public partial class RogueliteRoom : Node2D
} }
else else
{ {
GD.Print($"Door {door} connection was full: {connection.From} {connection.FromDoor} to {connection.To} {connection.ToDoor}"); GD.Print(
$"Door {door} connection was full: {connection.From} {connection.FromDoor} to {connection.To} {connection.ToDoor}");
} }
// if (connection.FromDoor is null && connection.ToDoor != door)
// {
// connection.FromDoor = door;
// }
// else if (connection.ToDoor is null && connection.FromDoor != door)
// {
// connection.ToDoor = door;
// }
// else
// {
// GD.Print($"Door {door} connection was full: {connection.From} {connection.FromDoor} to {connection.To} {connection.ToDoor}");
// }
_connections.Add(connection);
// var label = new Label(); _connections.Add(connection);
// label.Text = $"Door Edge: {doorEdge}, {connection}";
// label.ZIndex = 10;
//
// door.AddChild(label);
} }
else else
{ {
var wall = this.CreateChild<Node2D>(WallPrefab, marker.GlobalPosition); // var wall = this.CreateChild<Node2D>(WallPrefab, marker.GlobalPosition);
// var label = new Label();
// label.Text = $"Door Edge: {doorEdge}, Neighbor: {neighborPos}";
// label.ZIndex = 10;
//
// marker.AddChild(label);
} }
} }
} }
@ -231,9 +207,9 @@ public partial class RogueliteRoom : Node2D
var enemyScene = GD.Load<PackedScene>(e.PrefabPath); var enemyScene = GD.Load<PackedScene>(e.PrefabPath);
var spawnedEnemy = spawner.CreateChild<EnemyFSMProxy>(enemyScene); var spawnedEnemy = spawner.CreateChild<EnemyFSMProxy>(enemyScene);
_enemies.Add(spawnedEnemy); _enemies.Add(spawnedEnemy);
spawnedEnemy.Death += SpawnedEnemyOnDeath; spawnedEnemy.Death += SpawnedEnemyOnDeath;
} }
} }
@ -270,7 +246,7 @@ public partial class RogueliteRoom : Node2D
private void OnRoomEntered(Area2D area) private void OnRoomEntered(Area2D area)
{ {
if (area is not InteractionController player) return; if (area is not InteractionController player) return;
if (_enemies.Count <= 0) if (_enemies.Count <= 0)
{ {
OpenDoors(); OpenDoors();
@ -280,12 +256,12 @@ public partial class RogueliteRoom : Node2D
CloseDoors(); CloseDoors();
} }
} }
private void OnRoomExited(Area2D area) private void OnRoomExited(Area2D area)
{ {
if (area is not InteractionController player) return; if (area is not InteractionController player) return;
} }
public override string ToString() public override string ToString()
{ {
return $"{GridPosition} {RoomResource}"; return $"{GridPosition} {RoomResource}";
@ -297,21 +273,22 @@ public partial class RogueliteRoom : Node2D
{ {
DoorDirections.North => new Vector2((BaseRoomSize.X / 2) + (BaseRoomSize.X * wallIndex), 32), DoorDirections.North => new Vector2((BaseRoomSize.X / 2) + (BaseRoomSize.X * wallIndex), 32),
DoorDirections.South => new Vector2((BaseRoomSize.X / 2) + (BaseRoomSize.X * wallIndex), DoorDirections.South => new Vector2((BaseRoomSize.X / 2) + (BaseRoomSize.X * wallIndex),
((BaseRoomSize.Y) * RoomResource.Size.Y) +2 ), ((BaseRoomSize.Y) * RoomResource.Size.Y) + 2),
DoorDirections.East => new Vector2((BaseRoomSize.X * RoomResource.Size.X) - 12, (BaseRoomSize.Y / 2) + (BaseRoomSize.Y * wallIndex) - 8), DoorDirections.East => new Vector2((BaseRoomSize.X * RoomResource.Size.X) - 12,
(BaseRoomSize.Y / 2) + (BaseRoomSize.Y * wallIndex) - 8),
DoorDirections.West => new Vector2(12, (BaseRoomSize.Y / 2) + (BaseRoomSize.Y * wallIndex) - 8), DoorDirections.West => new Vector2(12, (BaseRoomSize.Y / 2) + (BaseRoomSize.Y * wallIndex) - 8),
_ => Vector2.Zero _ => Vector2.Zero
}; };
} }
// [ExportToolButton("Arrange Doors")] public Callable ArrangeDoorsButton => Callable.From(ArrangeDoors); // [ExportToolButton("Arrange Doors")] public Callable ArrangeDoorsButton => Callable.From(ArrangeDoors);
public void ArrangeDoors() public void ArrangeDoors()
{ {
var doorNode = this.GetNode("Doors"); var doorNode = this.GetNode("Doors");
var doors = doorNode.GetChildren(); var doors = doorNode.GetChildren();
foreach (var node in doors) foreach (var node in doors)
{ {
if (node is DoorMarker doorMarker) if (node is DoorMarker doorMarker)
@ -322,9 +299,10 @@ public partial class RogueliteRoom : Node2D
Vector2 doorPosition = doorMarker.Direction switch Vector2 doorPosition = doorMarker.Direction switch
{ {
DoorDirections.North => new Vector2((baseGridSize.X / 2) + (baseGridSize.X * doorMarker.WallIndex), 32), DoorDirections.North => new Vector2((baseGridSize.X / 2) + (baseGridSize.X * doorMarker.WallIndex),
32),
DoorDirections.South => new Vector2((baseGridSize.X / 2) + (baseGridSize.X * doorMarker.WallIndex), DoorDirections.South => new Vector2((baseGridSize.X / 2) + (baseGridSize.X * doorMarker.WallIndex),
((baseGridSize.Y) * RoomResource.Size.Y) +2 ), ((baseGridSize.Y) * RoomResource.Size.Y) + 2),
DoorDirections.East => doorMarker.Position, DoorDirections.East => doorMarker.Position,
DoorDirections.West => doorMarker.Position, DoorDirections.West => doorMarker.Position,
_ => doorMarker.Position _ => doorMarker.Position
@ -337,6 +315,5 @@ public partial class RogueliteRoom : Node2D
GD.Print($"Node was something else: {node}"); GD.Print($"Node was something else: {node}");
} }
} }
} }
} }