SupaLidlGame/BoundingBoxes/InteractionRay.cs

66 lines
1.4 KiB
C#
Raw Normal View History

2023-06-10 22:15:28 -07:00
using Godot;
namespace SupaLidlGame.BoundingBoxes;
public partial class InteractionRay : RayCast2D
{
private InteractionTrigger _trigger;
public InteractionTrigger Trigger
{
get => _trigger;
protected set
{
if (_trigger != value)
{
EmitSignal(SignalName.TriggerHit, value);
2023-08-05 23:50:08 -07:00
if (value is not null)
{
// focus on the new trigger
value.Focus();
}
2023-06-10 22:15:28 -07:00
}
if (_trigger is not null)
{
2023-08-05 23:50:08 -07:00
if (_trigger != value)
{
// unfocus from the old trigger
_trigger.Unfocus();
}
2023-06-10 22:15:28 -07:00
LastValidTrigger = value;
}
_trigger = value;
}
}
public InteractionTrigger LastValidTrigger { get; set; }
[Signal]
public delegate void TriggerHitEventHandler(InteractionTrigger trigger);
public override void _Ready()
{
}
public override void _PhysicsProcess(double delta)
{
if (IsColliding())
{
var obj = GetCollider();
// if obj is an InteractionTrigger then we signal hit
if (obj is InteractionTrigger trigger)
{
Trigger = trigger;
}
}
else
{
Trigger = null;
}
}
}