:memo::fire:🔨
- removed a lot of unnecessary code - Made a minimal working version - Added documentation
This commit is contained in:
@@ -3,20 +3,32 @@ using Godot;
|
||||
|
||||
namespace Babushka.scripts.CSharp.Common.CharacterControls;
|
||||
|
||||
/// <summary>
|
||||
/// Defines an <see cref="Area2D"/> Node with a <see cref="CollisionShape2D"/> used for detecting <see cref="InteractionArea2D"/> nodes.
|
||||
/// </summary>
|
||||
public partial class Detector : Area2D
|
||||
{
|
||||
|
||||
[Export] private bool _active = true;
|
||||
[Export] private bool _oneAtATime = true;
|
||||
|
||||
/// <summary>
|
||||
/// Called when entering an interactionArea node.
|
||||
/// </summary>
|
||||
[Signal] public delegate void InteractableEnteredEventHandler();
|
||||
[Signal] public delegate void InteractableExitedEventHandler();
|
||||
|
||||
private DetectableInteractionArea? _currentInteractionArea;
|
||||
/// <summary>
|
||||
/// Called when exiting an interactionArea node.
|
||||
/// </summary>
|
||||
[Signal] public delegate void InteractableExitedEventHandler();
|
||||
|
||||
public bool IsActive
|
||||
{
|
||||
get => _active;
|
||||
set => _active = value;
|
||||
set
|
||||
{
|
||||
Visible = value;
|
||||
_active = value;
|
||||
}
|
||||
}
|
||||
|
||||
public override void _Ready()
|
||||
@@ -25,39 +37,33 @@ public partial class Detector : Area2D
|
||||
AreaExited += OnExitedInteractable;
|
||||
}
|
||||
|
||||
public void OnEnteredInteractable(Node body)
|
||||
/// <summary>
|
||||
/// Called every time this node enters an Area2D.
|
||||
/// </summary>
|
||||
/// <param name="area"></param>
|
||||
public void OnEnteredInteractable(Node area)
|
||||
{
|
||||
if (!_active || !InputService.Instance.InputEnabled)
|
||||
return;
|
||||
|
||||
if (area is DetectableInteractionArea interactionArea2D)
|
||||
{
|
||||
EmitSignal(SignalName.InteractableEntered);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called whenever this node exits an Area2D.
|
||||
/// </summary>
|
||||
/// <param name="area"></param>
|
||||
public void OnExitedInteractable(Node area)
|
||||
{
|
||||
if (!_active || !InputService.Instance.InputEnabled)
|
||||
return;
|
||||
|
||||
GD.Print("Entered Node2D.");
|
||||
|
||||
if (body is DetectableInteractionArea interactionArea2D)
|
||||
if (area 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user