SupaLidlGame/State/Thinker/PursueState.cs

71 lines
1.9 KiB
C#
Raw Permalink 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();
if (bestTarget is not null)
{
// 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)
{
if (NPC.HasLineOfSight(bestTarget))
{
GD.Print("To attack state");
return AttackState;
}
}
}
else
{
// go to last seen position of last best target
GD.Print("Last seen " + NPC.LastSeenPosition);
NavigationAgent.TargetPosition = NPC.LastSeenPosition;
}
return PassiveState ?? base.Think();
}
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);
}
}