🚧 WIP: trying to rework the detection system (it fails)
This commit is contained in:
@@ -7,4 +7,16 @@ namespace Babushka.scripts.CSharp.Common.CharacterControls;
|
||||
/// </summary>
|
||||
public partial class DetectableInteractionArea : Area2D
|
||||
{
|
||||
[Export] public InteractionArea2D interactionArea2D;
|
||||
|
||||
public void Detected()
|
||||
{
|
||||
GD.Print("Detected " + interactionArea2D.GetParent().Name);
|
||||
interactionArea2D.IsSelectedByDetector = true;
|
||||
}
|
||||
|
||||
public void NoLongerDetected()
|
||||
{
|
||||
interactionArea2D.IsSelectedByDetector = false;
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,8 @@ namespace Babushka.scripts.CSharp.Common.CharacterControls;
|
||||
/// </summary>
|
||||
public partial class DetectionCross : Node2D
|
||||
{
|
||||
[Export] private Detector _detector;
|
||||
[Export] private Detector _collider;
|
||||
[Export] private RaycastDetector _detector;
|
||||
[Export] private float _xOffset;
|
||||
[Export] private float _yOffset;
|
||||
|
||||
@@ -17,6 +18,8 @@ public partial class DetectionCross : Node2D
|
||||
/// <param name="direction"></param>
|
||||
public void SetDirection(Vector2 direction)
|
||||
{
|
||||
_detector.Position = new Vector2(direction.X * _xOffset, direction.Y * _yOffset);
|
||||
Vector2 newPos = new Vector2(direction.X * _xOffset, direction.Y * _yOffset);
|
||||
_collider.GlobalPosition = newPos;
|
||||
_detector.TargetPosition = newPos;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
using Babushka.scripts.CSharp.Common.Services;
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
|
||||
namespace Babushka.scripts.CSharp.Common.CharacterControls;
|
||||
|
||||
@@ -10,7 +11,7 @@ public partial class Detector : Area2D
|
||||
{
|
||||
|
||||
[Export] private bool _active = true;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Called when entering an interactionArea node.
|
||||
/// </summary>
|
||||
@@ -41,7 +42,7 @@ public partial class Detector : Area2D
|
||||
/// Called every time this node enters an Area2D.
|
||||
/// </summary>
|
||||
/// <param name="area"></param>
|
||||
public void OnEnteredInteractable(Node area)
|
||||
public void OnEnteredInteractable(Area2D area)
|
||||
{
|
||||
if (!_active || !InputService.Instance.InputEnabled)
|
||||
return;
|
||||
@@ -56,13 +57,14 @@ public partial class Detector : Area2D
|
||||
/// Called whenever this node exits an Area2D.
|
||||
/// </summary>
|
||||
/// <param name="area"></param>
|
||||
public void OnExitedInteractable(Node area)
|
||||
public void OnExitedInteractable(Area2D area)
|
||||
{
|
||||
if (!_active || !InputService.Instance.InputEnabled)
|
||||
return;
|
||||
|
||||
if (area is DetectableInteractionArea interactionArea2D)
|
||||
{
|
||||
|
||||
EmitSignal(SignalName.InteractableExited);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System.Linq;
|
||||
using Babushka.scripts.CSharp.Common.Services;
|
||||
using Babushka.scripts.CSharp.Low_Code.Variables;
|
||||
using Godot;
|
||||
|
||||
namespace Babushka.scripts.CSharp.Common.CharacterControls;
|
||||
@@ -28,6 +29,8 @@ public partial class InteractionArea2D : Node2D
|
||||
set => _active = value;
|
||||
}
|
||||
|
||||
public bool IsSelectedByDetector { get; set; } = false;
|
||||
|
||||
public void SetActiveInverse(bool active)
|
||||
{
|
||||
IsActive = !active;
|
||||
@@ -52,6 +55,9 @@ public partial class InteractionArea2D : Node2D
|
||||
if (!_useOutline)
|
||||
return;
|
||||
|
||||
if (!IsSelectedByDetector)
|
||||
return;
|
||||
|
||||
foreach (var sprite in _spritesToOutline)
|
||||
{
|
||||
sprite.Material = _outlineMaterial;
|
||||
@@ -76,6 +82,9 @@ public partial class InteractionArea2D : Node2D
|
||||
{
|
||||
if (!_active || !InputService.Instance.InputEnabled)
|
||||
return;
|
||||
|
||||
if(!IsSelectedByDetector)
|
||||
return;
|
||||
|
||||
if (@event.IsAction("interact") && @event.IsPressed())
|
||||
{
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
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();
|
||||
}
|
||||
|
||||
_lastDetected = interactionArea;
|
||||
interactionArea.Detected();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://b4n0nlu4ckqga
|
||||
Reference in New Issue
Block a user