Add OpenDoorAction to handle unlocking and opening doors with event publishing

This commit is contained in:
2025-10-30 02:35:28 +01:00
parent 3fcb31d92f
commit 1a4c4cf4b1
2 changed files with 35 additions and 1 deletions

View File

@@ -52,7 +52,7 @@ public class InteractionSystem(float interactionRange) : ISystem
switch (door.CurrentState)
{
case DoorComponent.DoorState.Locked:
if (CheckRequirements(world, interactor, door.Requirements))
if (door.Requirements.Count > 0 && CheckRequirements(world, interactor, door.Requirements))
{
world.Logger.Info($"Door {doorEntity.Id} requirements met. Unlocking door.");
door.CurrentState = DoorComponent.DoorState.Opening;

View File

@@ -0,0 +1,34 @@
using GameCore.ECS;
using GameCore.Events;
using GameCore.Interaction;
using GameCore.Logic.Interfaces;
namespace GameCore.Logic;
public class OpenDoorAction(string targetWorldId) : ITriggerAction
{
public void Execute(World world)
{
var doorEntity = world.FindEntityByWorldId(targetWorldId);
if (doorEntity == null)
{
world.Logger.Warn($"[OpenDoorAction] Could not find entity with WorldId: {targetWorldId}");
return;
}
var door = world.GetComponent<DoorComponent>(doorEntity.Value);
if (door == null)
{
world.Logger.Warn($"[OpenDoorAction] Entity '{targetWorldId}' does not have a DoorComponent.");
return;
}
if (door.CurrentState == DoorComponent.DoorState.Locked || door.CurrentState == DoorComponent.DoorState.Closed)
{
door.Requirements.Clear();
door.CurrentState = DoorComponent.DoorState.Opening;
world.Logger.Info($"[OpenDoorAction] Unlocking and opening door: {targetWorldId}");
world.PublishEvent(new DoorStateChangedEvent(doorEntity.Value, door.CurrentState));
}
}
}