🚧 WIP detection works now

This commit is contained in:
2025-12-12 13:46:32 +01:00
parent 7ffbb8e68d
commit 9e0e87ef17
13 changed files with 105 additions and 99 deletions
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using Babushka.scripts.CSharp.Common.Services;
using Babushka.scripts.CSharp.Low_Code.Variables;
using Godot;
using Godot.Collections;
namespace Babushka.scripts.CSharp.Common.CharacterControls;
@@ -9,18 +10,11 @@ namespace Babushka.scripts.CSharp.Common.CharacterControls;
/// </summary>
public partial class Detector : Area2D
{
[Export] private bool _active = true;
/// <summary>
/// Called when entering an interactionArea node.
/// </summary>
[Signal] public delegate void InteractableEnteredEventHandler();
[Export] private ShapeCast2D _shapeCast2D;
[Export] private VariableResource _itemToTriggerResource;
/// <summary>
/// Called when exiting an interactionArea node.
/// </summary>
[Signal] public delegate void InteractableExitedEventHandler();
private readonly List<ulong> _areasInDetector = new();
public bool IsActive
{
@@ -47,9 +41,11 @@ public partial class Detector : Area2D
if (!_active || !InputService.Instance.InputEnabled)
return;
if (area is DetectableInteractionArea interactionArea2D)
if (area is DetectableInteractionArea detectable)
{
EmitSignal(SignalName.InteractableEntered);
ulong id = detectable.GetInstanceId();
_areasInDetector.Add(id);
CalculateClosestInteractable();
}
}
@@ -62,10 +58,35 @@ public partial class Detector : Area2D
if (!_active || !InputService.Instance.InputEnabled)
return;
if (area is DetectableInteractionArea interactionArea2D)
if (area is DetectableInteractionArea detectable)
{
EmitSignal(SignalName.InteractableExited);
ulong id = detectable.GetInstanceId();
if( _areasInDetector.Contains(id))
_areasInDetector.Remove(id);
CalculateClosestInteractable();
}
}
private void CalculateClosestInteractable()
{
GD.Print($"Areas in detector: {_areasInDetector.Count}");
float smallestDistance = float.MaxValue;
string closestInteractable = null;
foreach (var area in _areasInDetector)
{
Area2D? area2D = InstanceFromId(area) as Area2D;
if(area2D == null)
continue;
float distance = area2D.GlobalPosition.DistanceSquaredTo(ToGlobal(_shapeCast2D.TargetPosition));
if (distance < smallestDistance)
{
closestInteractable = area.ToString();
smallestDistance = distance;
}
}
GD.Print($"Closest interactable: {closestInteractable}");
_itemToTriggerResource.Payload = closestInteractable;
}
}