From 1a4c4cf4b12ec7da16f2d0e05a6c63222bd7fea0 Mon Sep 17 00:00:00 2001 From: Gabriel Kaszewski Date: Thu, 30 Oct 2025 02:35:28 +0100 Subject: [PATCH] Add OpenDoorAction to handle unlocking and opening doors with event publishing --- GameCore/Interaction/InteractionSystem.cs | 2 +- GameCore/Logic/OpenDoorAction.cs | 34 +++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 GameCore/Logic/OpenDoorAction.cs diff --git a/GameCore/Interaction/InteractionSystem.cs b/GameCore/Interaction/InteractionSystem.cs index d6e86f8..b8659c0 100644 --- a/GameCore/Interaction/InteractionSystem.cs +++ b/GameCore/Interaction/InteractionSystem.cs @@ -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; diff --git a/GameCore/Logic/OpenDoorAction.cs b/GameCore/Logic/OpenDoorAction.cs new file mode 100644 index 0000000..c0779a5 --- /dev/null +++ b/GameCore/Logic/OpenDoorAction.cs @@ -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(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)); + } + } +} \ No newline at end of file