Add button interaction system with event publishing and requirements handling

This commit is contained in:
2025-10-30 02:21:33 +01:00
parent 86afb57809
commit 3fcb31d92f
11 changed files with 240 additions and 5 deletions

View File

@@ -0,0 +1,14 @@
using GameCore.ECS.Interfaces;
using GameCore.Interaction.Interfaces;
namespace GameCore.Interaction;
public class ButtonComponent : IComponent
{
public string ChannelId { get; set; } = "default_channel";
public bool IsToggle { get; set; } = false;
public bool IsOneTimeUse { get; set; } = false;
public bool IsPressed { get; set; } = false;
public bool HasBeenUsed { get; set; } = false;
public List<IInteractionRequirement> Requirements { get; set; } = [];
}

View File

@@ -2,6 +2,7 @@ using GameCore.ECS;
using GameCore.ECS.Interfaces;
using GameCore.Events;
using GameCore.Input;
using GameCore.Interaction.Interfaces;
using GameCore.Player;
namespace GameCore.Interaction;
@@ -26,12 +27,22 @@ public class InteractionSystem(float interactionRange) : ISystem
if (hit.DidHit && hit.HitEntity.HasValue)
{
var door = world.GetComponent<DoorComponent>(hit.HitEntity.Value);
var targetEntity = hit.HitEntity.Value;
var door = world.GetComponent<DoorComponent>(targetEntity);
if (door != null)
{
world.AddComponent(player, new IsLookingAtInteractableComponent(hit.HitEntity.Value));
if (input.IsInteracting) TryInteractWithDoor(world, player, hit.HitEntity.Value, door);
return;
}
var button = world.GetComponent<ButtonComponent>(targetEntity);
if (button != null)
{
world.AddComponent(player, new IsLookingAtInteractableComponent(targetEntity));
if (input.IsInteracting) TryInteractWithButton(world, player, targetEntity, button);
}
}
}
@@ -41,7 +52,7 @@ public class InteractionSystem(float interactionRange) : ISystem
switch (door.CurrentState)
{
case DoorComponent.DoorState.Locked:
if (CheckRequirements(world, interactor, door))
if (CheckRequirements(world, interactor, door.Requirements))
{
world.Logger.Info($"Door {doorEntity.Id} requirements met. Unlocking door.");
door.CurrentState = DoorComponent.DoorState.Opening;
@@ -67,13 +78,39 @@ public class InteractionSystem(float interactionRange) : ISystem
}
}
private bool CheckRequirements(World world, Entity interactor, DoorComponent door)
private void TryInteractWithButton(World world, Entity interactor, Entity buttonEntity, ButtonComponent button)
{
foreach (var req in door.Requirements)
if (button.IsOneTimeUse && button.HasBeenUsed)
{
world.Logger.Info($"Button {buttonEntity.Id} is one-time-use and has already been used.");
return;
}
if (CheckRequirements(world, interactor, button.Requirements))
{
if (button.IsToggle)
button.IsPressed = !button.IsPressed;
else
button.IsPressed = true;
button.HasBeenUsed = true;
world.Logger.Info(
$"Button {buttonEntity.Id} pressed. Channel: {button.ChannelId}, NewState: {button.IsPressed}");
world.PublishEvent(new ButtonPressedEvent(button.ChannelId, button.IsPressed));
}
else
{
world.Logger.Info($"Button {buttonEntity.Id} requirements not met.");
}
}
private bool CheckRequirements(World world, Entity interactor, List<IInteractionRequirement> requirements)
{
foreach (var req in requirements)
if (!req.IsMet(interactor, world))
return false;
foreach (var req in door.Requirements) req.ApplySideEffects(interactor, world);
foreach (var req in requirements) req.ApplySideEffects(interactor, world);
return true;
}

View File

@@ -0,0 +1,8 @@
using GameCore.ECS.Interfaces;
namespace GameCore.Interaction;
public class WorldIdComponent(string worldId) : IComponent
{
public readonly string WorldId = worldId;
}