63 lines
2.1 KiB
C#
63 lines
2.1 KiB
C#
using GameCore.ECS;
|
|
using GameCore.ECS.Interfaces;
|
|
using GameCore.Events;
|
|
|
|
namespace GameCore.Logic;
|
|
|
|
public class LogicSequenceSystem : ISystem
|
|
{
|
|
private readonly World _world;
|
|
|
|
public LogicSequenceSystem(World world)
|
|
{
|
|
_world = world;
|
|
world.Subscribe<ButtonPressedEvent>(OnButtonPressed);
|
|
}
|
|
|
|
public void Update(World world, float deltaTime)
|
|
{
|
|
}
|
|
|
|
private void OnButtonPressed(ButtonPressedEvent e)
|
|
{
|
|
var logicEntities = _world.GetEntitiesWith<LogicSequenceComponent>();
|
|
|
|
foreach (var entity in logicEntities)
|
|
{
|
|
var logic = _world.GetComponent<LogicSequenceComponent>(entity);
|
|
if (logic == null) continue;
|
|
|
|
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 (logic.IsOneTimeTrigger && logic.HasTriggered) continue;
|
|
|
|
_world.Logger.Info("[LogicSequenceSystem] All channels activated. Executing OnActivateActions.");
|
|
foreach (var action in logic.OnActivateActions) action.Execute(_world);
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
} |