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 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 HasTriggered { get; set; } = false;
|
||||
public bool IsCurrentlyActive { get; set; } = false;
|
||||
}
|
||||
@@ -25,27 +25,38 @@ public class LogicSequenceSystem : ISystem
|
||||
foreach (var entity in logicEntities)
|
||||
{
|
||||
var logic = _world.GetComponent<LogicSequenceComponent>(entity);
|
||||
if (logic == null || (logic.IsOneTimeTrigger && logic.HasTriggered)) continue;
|
||||
if (logic == null) continue;
|
||||
|
||||
if (logic.RequiredChannels.Contains(e.ChannelId))
|
||||
if (!logic.RequiredChannels.Contains(e.ChannelId)) continue;
|
||||
|
||||
if (e.NewState)
|
||||
logic.ActivatedChannels.Add(e.ChannelId);
|
||||
else
|
||||
logic.ActivatedChannels.Remove(e.ChannelId);
|
||||
|
||||
_world.Logger.Info(
|
||||
$"[LogicSequenceSystem] Channel '{e.ChannelId}' activated. Total {logic.ActivatedChannels.Count}/{logic.RequiredChannels.Count}");
|
||||
|
||||
var allConditionsMet = logic.ActivatedChannels.Count == logic.RequiredChannels.Count;
|
||||
|
||||
if (allConditionsMet && !logic.IsCurrentlyActive)
|
||||
{
|
||||
if (e.NewState)
|
||||
logic.ActivatedChannels.Add(e.ChannelId);
|
||||
else
|
||||
logic.ActivatedChannels.Remove(e.ChannelId);
|
||||
if (logic.IsOneTimeTrigger && logic.HasTriggered) continue;
|
||||
|
||||
_world.Logger.Info(
|
||||
$"[LogicSequenceSystem] Channel '{e.ChannelId}' activated. Total {logic.ActivatedChannels.Count}/{logic.RequiredChannels.Count}");
|
||||
_world.Logger.Info("[LogicSequenceSystem] All channels activated. Executing OnActivateActions.");
|
||||
foreach (var action in logic.OnActivateActions) action.Execute(_world);
|
||||
|
||||
if (logic.ActivatedChannels.Count == logic.RequiredChannels.Count)
|
||||
{
|
||||
_world.Logger.Info("[LogicSequenceSystem] All channels activated. Executing actions.");
|
||||
foreach (var action in logic.OnCompleteActions) action.Execute(_world);
|
||||
logic.IsCurrentlyActive = true;
|
||||
logic.HasTriggered = true;
|
||||
}
|
||||
else if (!allConditionsMet && logic.IsCurrentlyActive)
|
||||
{
|
||||
if (logic.IsOneTimeTrigger) continue;
|
||||
|
||||
if (logic.IsOneTimeTrigger) logic.HasTriggered = true;
|
||||
_world.Logger.Info("[LogicSequenceSystem] Channels deactivated. Executing OnDeactivateActions.");
|
||||
foreach (var action in logic.OnDeactivateActions) action.Execute(_world);
|
||||
|
||||
logic.ActivatedChannels.Clear();
|
||||
}
|
||||
logic.IsCurrentlyActive = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user