SupaLidlGame/State/Thinker/PursueState.cs

65 lines
1.8 KiB
C#
Raw Normal View History

2023-08-05 23:50:08 -07:00
using Godot;
namespace SupaLidlGame.State.Thinker;
public partial class PursueState : ThinkerState
{
[Export]
public NavigationAgent2D NavigationAgent { get; set; }
[Export]
public ThinkerState AttackState { get; set; }
[Export]
public ThinkerState PassiveState { get; set; }
/// <summary>
/// Minimum distance the NPC should be, otherwise entering attack state
/// </summary>
[Export]
public float MinDistanceToTarget { get; set; }
/// <summary>
/// Maximum distance the NPC should be, otherwise entering passive state
/// </summary>
[Export]
public float MaxDistanceFromOrigin { get; set; }
public override ThinkerState Think()
{
var bestTarget = NPC.FindBestTarget();
2023-11-13 09:49:18 -08:00
if (bestTarget is not null && NPC.HasLineOfSight(bestTarget))
2023-08-05 23:50:08 -07:00
{
// navigate towards the best target
var pos = bestTarget.GlobalPosition;
NavigationAgent.TargetPosition = pos;
NPC.Target = NPC.GlobalPosition.DirectionTo(pos);
NPC.LastSeenPosition = pos;
2023-11-13 09:49:18 -08:00
if (NPC.GlobalPosition.DistanceTo(pos) < MinDistanceToTarget)
2023-08-05 23:50:08 -07:00
{
2023-11-13 09:49:18 -08:00
return AttackState;
2023-08-05 23:50:08 -07:00
}
}
else
{
// go to last seen position of last best target
NavigationAgent.TargetPosition = NPC.LastSeenPosition;
}
2023-08-16 21:02:49 -07:00
return null;
2023-08-05 23:50:08 -07:00
}
public override ThinkerState PhysicsProcess(double delta)
{
if (!NavigationAgent.IsTargetReachable())
{
return PassiveState ?? base.PhysicsProcess(delta);
}
var navPos = NavigationAgent.GetNextPathPosition();
NPC.Direction = NPC.GlobalPosition.DirectionTo(navPos);
return base.PhysicsProcess(delta);
}
}