SupaLidlGame/State/Thinker/PursueState.cs

71 lines
1.9 KiB
C#

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 IState<ThinkerState> Enter(IState<ThinkerState> prev)
{
GD.Print("pursuing");
return base.Enter(prev);
}
public override ThinkerState Think()
{
var bestTarget = NPC.FindBestTarget();
if (bestTarget is not null && NPC.HasLineOfSight(bestTarget))
{
// navigate towards the best target
var pos = bestTarget.GlobalPosition;
NavigationAgent.TargetPosition = pos;
NPC.Target = NPC.GlobalPosition.DirectionTo(pos);
NPC.LastSeenPosition = pos;
if (NPC.GlobalPosition.DistanceTo(pos) < MinDistanceToTarget)
{
return AttackState;
}
}
else
{
// go to last seen position of last best target
NavigationAgent.TargetPosition = NPC.LastSeenPosition;
}
return null;
}
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);
}
}