Add CloseDoorAction and enhance LogicSequenceComponent for activation handling
This commit is contained in:
33
GameCore/Logic/CloseDoorAction.cs
Normal file
33
GameCore/Logic/CloseDoorAction.cs
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
using GameCore.ECS;
|
||||||
|
using GameCore.Events;
|
||||||
|
using GameCore.Interaction;
|
||||||
|
using GameCore.Logic.Interfaces;
|
||||||
|
|
||||||
|
namespace GameCore.Logic;
|
||||||
|
|
||||||
|
public class CloseDoorAction(string targetWorldId) : ITriggerAction
|
||||||
|
{
|
||||||
|
public void Execute(World world)
|
||||||
|
{
|
||||||
|
var doorEntity = world.FindEntityByWorldId(targetWorldId);
|
||||||
|
if (doorEntity == null)
|
||||||
|
{
|
||||||
|
world.Logger.Warn($"[CloseDoorAction] Could not find entity with WorldId: {targetWorldId}");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var door = world.GetComponent<DoorComponent>(doorEntity.Value);
|
||||||
|
if (door == null)
|
||||||
|
{
|
||||||
|
world.Logger.Warn($"[CloseDoorAction] Entity '{targetWorldId}' does not have a DoorComponent.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (door.CurrentState == DoorComponent.DoorState.Open || door.CurrentState == DoorComponent.DoorState.Opening)
|
||||||
|
{
|
||||||
|
door.CurrentState = DoorComponent.DoorState.Closing;
|
||||||
|
world.Logger.Info($"[CloseDoorAction] Closing door: {targetWorldId}");
|
||||||
|
world.PublishEvent(new DoorStateChangedEvent(doorEntity.Value, door.CurrentState));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,7 +7,9 @@ public class LogicSequenceComponent : IComponent
|
|||||||
{
|
{
|
||||||
public List<string> RequiredChannels { get; set; } = [];
|
public List<string> RequiredChannels { get; set; } = [];
|
||||||
public HashSet<string> ActivatedChannels { get; set; } = [];
|
public HashSet<string> ActivatedChannels { get; set; } = [];
|
||||||
public List<ITriggerAction> OnCompleteActions { get; set; } = [];
|
public List<ITriggerAction> OnActivateActions { get; set; } = [];
|
||||||
|
public List<ITriggerAction> OnDeactivateActions { get; set; } = [];
|
||||||
public bool IsOneTimeTrigger { get; set; } = false;
|
public bool IsOneTimeTrigger { get; set; } = false;
|
||||||
public bool HasTriggered { get; set; } = false;
|
public bool HasTriggered { get; set; } = false;
|
||||||
|
public bool IsCurrentlyActive { get; set; } = false;
|
||||||
}
|
}
|
||||||
@@ -25,10 +25,10 @@ public class LogicSequenceSystem : ISystem
|
|||||||
foreach (var entity in logicEntities)
|
foreach (var entity in logicEntities)
|
||||||
{
|
{
|
||||||
var logic = _world.GetComponent<LogicSequenceComponent>(entity);
|
var logic = _world.GetComponent<LogicSequenceComponent>(entity);
|
||||||
if (logic == null || (logic.IsOneTimeTrigger && logic.HasTriggered)) continue;
|
if (logic == null) continue;
|
||||||
|
|
||||||
|
if (!logic.RequiredChannels.Contains(e.ChannelId)) continue;
|
||||||
|
|
||||||
if (logic.RequiredChannels.Contains(e.ChannelId))
|
|
||||||
{
|
|
||||||
if (e.NewState)
|
if (e.NewState)
|
||||||
logic.ActivatedChannels.Add(e.ChannelId);
|
logic.ActivatedChannels.Add(e.ChannelId);
|
||||||
else
|
else
|
||||||
@@ -37,15 +37,26 @@ public class LogicSequenceSystem : ISystem
|
|||||||
_world.Logger.Info(
|
_world.Logger.Info(
|
||||||
$"[LogicSequenceSystem] Channel '{e.ChannelId}' activated. Total {logic.ActivatedChannels.Count}/{logic.RequiredChannels.Count}");
|
$"[LogicSequenceSystem] Channel '{e.ChannelId}' activated. Total {logic.ActivatedChannels.Count}/{logic.RequiredChannels.Count}");
|
||||||
|
|
||||||
if (logic.ActivatedChannels.Count == logic.RequiredChannels.Count)
|
var allConditionsMet = logic.ActivatedChannels.Count == logic.RequiredChannels.Count;
|
||||||
|
|
||||||
|
if (allConditionsMet && !logic.IsCurrentlyActive)
|
||||||
{
|
{
|
||||||
_world.Logger.Info("[LogicSequenceSystem] All channels activated. Executing actions.");
|
if (logic.IsOneTimeTrigger && logic.HasTriggered) continue;
|
||||||
foreach (var action in logic.OnCompleteActions) action.Execute(_world);
|
|
||||||
|
|
||||||
if (logic.IsOneTimeTrigger) logic.HasTriggered = true;
|
_world.Logger.Info("[LogicSequenceSystem] All channels activated. Executing OnActivateActions.");
|
||||||
|
foreach (var action in logic.OnActivateActions) action.Execute(_world);
|
||||||
|
|
||||||
logic.ActivatedChannels.Clear();
|
logic.IsCurrentlyActive = true;
|
||||||
|
logic.HasTriggered = true;
|
||||||
}
|
}
|
||||||
|
else if (!allConditionsMet && logic.IsCurrentlyActive)
|
||||||
|
{
|
||||||
|
if (logic.IsOneTimeTrigger) continue;
|
||||||
|
|
||||||
|
_world.Logger.Info("[LogicSequenceSystem] Channels deactivated. Executing OnDeactivateActions.");
|
||||||
|
foreach (var action in logic.OnDeactivateActions) action.Execute(_world);
|
||||||
|
|
||||||
|
logic.IsCurrentlyActive = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user