🚧 WIP detection works now
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user