Add button interaction system with event publishing and requirements handling
This commit is contained in:
12
GameCore/Logic/DebugMessageAction.cs
Normal file
12
GameCore/Logic/DebugMessageAction.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using GameCore.ECS;
|
||||
using GameCore.Logic.Interfaces;
|
||||
|
||||
namespace GameCore.Logic;
|
||||
|
||||
public class DebugMessageAction(string message) : ITriggerAction
|
||||
{
|
||||
public void Execute(World world)
|
||||
{
|
||||
world.Logger.Debug($"[DebugMessageAction] {message}");
|
||||
}
|
||||
}
|
||||
8
GameCore/Logic/Interfaces/ITriggerAction.cs
Normal file
8
GameCore/Logic/Interfaces/ITriggerAction.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
using GameCore.ECS;
|
||||
|
||||
namespace GameCore.Logic.Interfaces;
|
||||
|
||||
public interface ITriggerAction
|
||||
{
|
||||
void Execute(World world);
|
||||
}
|
||||
13
GameCore/Logic/LogicSequenceComponent.cs
Normal file
13
GameCore/Logic/LogicSequenceComponent.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using GameCore.ECS.Interfaces;
|
||||
using GameCore.Logic.Interfaces;
|
||||
|
||||
namespace GameCore.Logic;
|
||||
|
||||
public class LogicSequenceComponent : IComponent
|
||||
{
|
||||
public List<string> RequiredChannels { get; set; } = [];
|
||||
public HashSet<string> ActivatedChannels { get; set; } = [];
|
||||
public List<ITriggerAction> OnCompleteActions { get; set; } = [];
|
||||
public bool IsOneTimeTrigger { get; set; } = false;
|
||||
public bool HasTriggered { get; set; } = false;
|
||||
}
|
||||
52
GameCore/Logic/LogicSequenceSystem.cs
Normal file
52
GameCore/Logic/LogicSequenceSystem.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
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 || (logic.IsOneTimeTrigger && logic.HasTriggered)) continue;
|
||||
|
||||
if (logic.RequiredChannels.Contains(e.ChannelId))
|
||||
{
|
||||
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}");
|
||||
|
||||
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);
|
||||
|
||||
if (logic.IsOneTimeTrigger) logic.HasTriggered = true;
|
||||
|
||||
logic.ActivatedChannels.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
35
GameCore/Logic/SpawnEntityAction.cs
Normal file
35
GameCore/Logic/SpawnEntityAction.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using GameCore.ECS;
|
||||
using GameCore.Events;
|
||||
using GameCore.Logic.Interfaces;
|
||||
using GameCore.Math;
|
||||
using GameCore.Physics;
|
||||
|
||||
namespace GameCore.Logic;
|
||||
|
||||
public class SpawnEntityAction(string archetypeId, string spawnerWorldId) : ITriggerAction
|
||||
{
|
||||
public void Execute(World world)
|
||||
{
|
||||
var spawnerEntity = world.FindEntityByWorldId(spawnerWorldId);
|
||||
if (spawnerEntity == null)
|
||||
{
|
||||
world.Logger.Warn($"[SpawnEntityAction] Could not find spawner with WorldId: {spawnerWorldId}");
|
||||
return;
|
||||
}
|
||||
|
||||
var position = world.GetComponent<PositionComponent>(spawnerEntity.Value);
|
||||
if (position == null)
|
||||
{
|
||||
world.Logger.Warn($"[SpawnEntityAction] Spawner '{spawnerWorldId}' does not have a PositionComponent.");
|
||||
return;
|
||||
}
|
||||
|
||||
world.PublishEvent(new SpawnEntityEvent(
|
||||
archetypeId,
|
||||
position.Position,
|
||||
Vector3.Zero,
|
||||
default));
|
||||
|
||||
world.Logger.Info($"[SpawnEntityAction] Spawning '{archetypeId}' at '{spawnerWorldId}'.");
|
||||
}
|
||||
}
|
||||
32
GameCore/Logic/UnlockDoorAction.cs
Normal file
32
GameCore/Logic/UnlockDoorAction.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using GameCore.ECS;
|
||||
using GameCore.Interaction;
|
||||
using GameCore.Logic.Interfaces;
|
||||
|
||||
namespace GameCore.Logic;
|
||||
|
||||
public class UnlockDoorAction(string targetWorldId) : ITriggerAction
|
||||
{
|
||||
public void Execute(World world)
|
||||
{
|
||||
var doorEntity = world.FindEntityByWorldId(targetWorldId);
|
||||
if (doorEntity == null)
|
||||
{
|
||||
world.Logger.Warn($"[UnlockDoorAction] Could not find entity with WorldId: {targetWorldId}");
|
||||
return;
|
||||
}
|
||||
|
||||
var door = world.GetComponent<DoorComponent>(doorEntity.Value);
|
||||
if (door == null)
|
||||
{
|
||||
world.Logger.Warn($"[UnlockDoorAction] Entity '{targetWorldId}' does not have a DoorComponent.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (door.CurrentState == DoorComponent.DoorState.Locked)
|
||||
{
|
||||
door.CurrentState = DoorComponent.DoorState.Closed;
|
||||
door.Requirements.Clear();
|
||||
world.Logger.Info($"[UnlockDoorAction] Unlocked door: {targetWorldId}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user