Door connections

This commit is contained in:
Marco 2025-04-16 18:18:52 +02:00
commit 700be1e207
11 changed files with 127 additions and 149 deletions

View file

@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Cirno.Scripts.Components.FSM.Enemy;
using Cirno.Scripts.Resources;
using Cirno.Scripts.Resources.Roguelite;
using Godot;
@ -16,7 +18,7 @@ public partial class RogueliteRoom : Node2D
[Export] public PackedScene DoorPrefab { get; private set; }
[Export] public PackedScene WallPrefab { get; private set; }
private static readonly Dictionary<string, Vector2I> DirectionMap = new()
private static readonly Godot.Collections.Dictionary<string, Vector2I> DirectionMap = new()
{
{ "North", new Vector2I(0, -1) },
{ "South", new Vector2I(0, 1) },
@ -24,6 +26,10 @@ public partial class RogueliteRoom : Node2D
{ "West", new Vector2I(-1, 0) },
};
private List<Door> _doors = [];
private List<RoomConnection> _connections = [];
private List<EnemyFSMProxy> _enemies = [];
private Array<EnemyResource> SpawnableEnemies => RoomResource.SpawnableEnemies;
public RogueliteRoom Spawn()
@ -84,6 +90,15 @@ public partial class RogueliteRoom : Node2D
door.State = DoorState.Closed;
_doors.Add(door);
if (connection.FromDoor is null) connection.FromDoor = door;
else if (connection.ToDoor is null) connection.ToDoor = door;
else
GD.Print("Door connection was full");
_connections.Add(connection);
// var label = new Label();
// label.Text = $"Door Edge: {doorEdge}, {connection}";
// label.ZIndex = 10;
@ -100,15 +115,7 @@ public partial class RogueliteRoom : Node2D
//
// marker.AddChild(label);
}
// PackedScene prefab = hasConnection
// ? GD.Load<PackedScene>("res://Prefabs/Door.tscn")
// : GD.Load<PackedScene>("res://Prefabs/Wall.tscn");
//
// var instance = prefab.Instantiate<Node2D>();
// AddChild(instance);
// instance.GlobalPosition = ((Node2D)child).GlobalPosition;
// instance.Rotation = ((Node2D)child).GlobalRotation;
}
}
@ -125,7 +132,59 @@ public partial class RogueliteRoom : Node2D
var e = SpawnableEnemies[index];
var enemyScene = GD.Load<PackedScene>(e.PrefabPath);
var spawnedEnemy = spawner.CreateChild<Node2D>(enemyScene);
var spawnedEnemy = spawner.CreateChild<EnemyFSMProxy>(enemyScene);
_enemies.Add(spawnedEnemy);
spawnedEnemy.Death += SpawnedEnemyOnDeath;
}
}
private void SpawnedEnemyOnDeath(EnemyFSMProxy enemy)
{
enemy.Death -= SpawnedEnemyOnDeath;
_enemies.Remove(enemy);
if (_enemies.Count == 0)
{
OpenDoors();
}
}
public void OpenDoors()
{
foreach (var connection in _connections)
{
connection.FromDoor?.Activate(ActivationType.Open);
connection.ToDoor?.Activate(ActivationType.Open);
}
}
public void CloseDoors()
{
foreach (var connection in _connections)
{
connection.FromDoor?.Activate(ActivationType.Close);
connection.ToDoor?.Activate(ActivationType.Close);
}
}
private void OnRoomEntered(Area2D area)
{
if (area is not InteractionController player) return;
if (_enemies.Count <= 0)
{
OpenDoors();
}
else
{
CloseDoors();
}
}
private void OnRoomExited(Area2D area)
{
if (area is not InteractionController player) return;
}
}