You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
1.8 KiB
63 lines
1.8 KiB
using Babushka.scripts.CSharp.Common.Services;
|
|
using Godot;
|
|
|
|
namespace Babushka.scripts.CSharp.Common.CharacterControls;
|
|
|
|
public partial class Detector : Area2D
|
|
{
|
|
[Export] private bool _active = true;
|
|
[Export] private bool _oneAtATime = true;
|
|
|
|
[Signal] public delegate void InteractableEnteredEventHandler();
|
|
[Signal] public delegate void InteractableExitedEventHandler();
|
|
|
|
private DetectableInteractionArea? _currentInteractionArea;
|
|
|
|
public bool IsActive
|
|
{
|
|
get => _active;
|
|
set => _active = value;
|
|
}
|
|
|
|
public override void _Ready()
|
|
{
|
|
AreaEntered += OnEnteredInteractable;
|
|
AreaExited += OnExitedInteractable;
|
|
}
|
|
|
|
public void OnEnteredInteractable(Node body)
|
|
{
|
|
if (!_active || !InputService.Instance.InputEnabled)
|
|
return;
|
|
|
|
GD.Print("Entered Node2D.");
|
|
|
|
if (body is DetectableInteractionArea interactionArea2D)
|
|
{
|
|
GD.Print("Entered interactable.");
|
|
_currentInteractionArea = interactionArea2D;
|
|
interactionArea2D.ActivateInteractionArea(true);
|
|
EmitSignal(SignalName.InteractableEntered);
|
|
if (_oneAtATime)
|
|
_active = false;
|
|
}
|
|
}
|
|
|
|
public void OnExitedInteractable(Node body)
|
|
{
|
|
GD.Print("Exited Node2D.");
|
|
|
|
if (body is DetectableInteractionArea interactionArea2D)
|
|
{
|
|
GD.Print("Exited interactable.");
|
|
|
|
if (_oneAtATime && _currentInteractionArea != interactionArea2D)
|
|
return;
|
|
|
|
interactionArea2D.ActivateInteractionArea(false);
|
|
_currentInteractionArea = null;
|
|
EmitSignal(SignalName.InteractableExited);
|
|
_active = true;
|
|
}
|
|
}
|
|
} |