🚧 WIP detection works now
This commit is contained in:
@@ -9,14 +9,16 @@ public partial class DetectableInteractionArea : Area2D
|
||||
{
|
||||
[Export] public InteractionArea2D interactionArea2D;
|
||||
|
||||
public void Detected()
|
||||
|
||||
public void InteractionAreaSelectionChanged(Variant instanceID)
|
||||
{
|
||||
GD.Print("Detected " + interactionArea2D.GetParent().Name);
|
||||
interactionArea2D.IsSelectedByDetector = true;
|
||||
}
|
||||
|
||||
public void NoLongerDetected()
|
||||
{
|
||||
interactionArea2D.IsSelectedByDetector = false;
|
||||
if (instanceID.AsString() == GetInstanceId().ToString())
|
||||
{
|
||||
interactionArea2D.HighlightInteractable();
|
||||
}
|
||||
else
|
||||
{
|
||||
interactionArea2D.ResetHighlight();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,7 @@ namespace Babushka.scripts.CSharp.Common.CharacterControls;
|
||||
public partial class DetectionCross : Node2D
|
||||
{
|
||||
[Export] private Detector _collider;
|
||||
[Export] private RaycastDetector _detector;
|
||||
[Export] private ShapeCast2D _shapeCast2D;
|
||||
[Export] private float _xOffset;
|
||||
[Export] private float _yOffset;
|
||||
|
||||
@@ -20,6 +20,6 @@ public partial class DetectionCross : Node2D
|
||||
{
|
||||
Vector2 newPos = new Vector2(direction.X * _xOffset, direction.Y * _yOffset);
|
||||
_collider.Position = newPos;
|
||||
_detector.TargetPosition = newPos;
|
||||
_shapeCast2D.TargetPosition = newPos;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -43,10 +43,10 @@ public partial class InteractionArea2D : Node2D
|
||||
_backupMaterials = _spritesToOutline.Select(s => s.Material).ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
public void OnPlayerEntered(Node2D player)
|
||||
{
|
||||
GD.Print("OnPlayerEntered: " + this.GetParent().Name);
|
||||
if (!_active || !InputService.Instance.InputEnabled)
|
||||
return;
|
||||
|
||||
@@ -59,13 +59,28 @@ public partial class InteractionArea2D : Node2D
|
||||
if (!IsSelectedByDetector)
|
||||
return;
|
||||
|
||||
ActivateOutline();
|
||||
}
|
||||
*/
|
||||
|
||||
public void HighlightInteractable()
|
||||
{
|
||||
if (!_active || !InputService.Instance.InputEnabled)
|
||||
return;
|
||||
|
||||
if (_showLabel)
|
||||
_label.Show();
|
||||
|
||||
if (!_useOutline)
|
||||
return;
|
||||
|
||||
foreach (var sprite in _spritesToOutline)
|
||||
{
|
||||
sprite.Material = _outlineMaterial;
|
||||
}
|
||||
}
|
||||
|
||||
public void OnPlayerExited(Node2D player)
|
||||
public void ResetHighlight()
|
||||
{
|
||||
_label.Hide();
|
||||
|
||||
@@ -78,6 +93,13 @@ public partial class InteractionArea2D : Node2D
|
||||
sprite.Material = _backupMaterials[i];
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
public void OnPlayerExited(Node2D player)
|
||||
{
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
public override void _Input(InputEvent @event)
|
||||
{
|
||||
@@ -134,4 +156,5 @@ public partial class InteractionArea2D : Node2D
|
||||
_label.Hide();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
using Godot;
|
||||
|
||||
namespace Babushka.scripts.CSharp.Common.CharacterControls;
|
||||
|
||||
public partial class RaycastDetector : RayCast2D
|
||||
{
|
||||
[Export] private bool _active = true;
|
||||
|
||||
private DetectableInteractionArea? _lastDetected;
|
||||
|
||||
public bool IsActive
|
||||
{
|
||||
get => _active;
|
||||
set
|
||||
{
|
||||
Visible = value;
|
||||
_active = value;
|
||||
}
|
||||
}
|
||||
|
||||
public override void _PhysicsProcess(double delta)
|
||||
{
|
||||
if (!_active)
|
||||
return;
|
||||
|
||||
if (IsColliding())
|
||||
{
|
||||
if (GetCollider() is DetectableInteractionArea interactionArea)
|
||||
{
|
||||
if (_lastDetected != null && _lastDetected != interactionArea)
|
||||
{
|
||||
_lastDetected.NoLongerDetected();
|
||||
}
|
||||
|
||||
GD.Print("Colliding with: " + interactionArea.GetParent().Name);
|
||||
_lastDetected = interactionArea;
|
||||
interactionArea.Detected();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
uid://b4n0nlu4ckqga
|
||||
Reference in New Issue
Block a user