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

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