NPCs/Implementation/Crackhead/ChaseState.cs
namespace Opium.AI;

[Title( "Chase" )]
public partial class ChaseState : StateMachine.State
{
	/// <summary>
	/// Should we be forcing into this state? Not super happy with this but it's event based.
	/// </summary>
	private bool forceIntoState = false;

	public override bool ShouldEnterState( StateMachine machine )
	{
		// KEEP THIS!
		if ( machine.TimeInState < 1f && machine.CurrentState is AttackStunState )
			return false;

		// Forcing into state bypasses every other check
		if ( forceIntoState )
			return true;

		// Only if we've been in another state for a second
		if ( machine.TimeInState < 1f )
			return false;

		// Enemy spotted?
		if ( machine.CurrentState is AlertState && Agent.LastStimulus is EnemySpottedStimulus )
			return true;

		// Was stunned before?
		if ( machine.CurrentState is AttackStunState )
			return true;

		// Was opening a door before?
		if ( machine.CurrentState is OpenDoorState )
			return true;

		// Was blocking before?
		if ( machine.CurrentState is BlockingState )
		{
			if ( machine.TimeInState > 1.2f )
				return true;
		}

		return base.ShouldEnterState( machine );
	}

	public override void Tick()
	{
		if ( !Agent.IsValid() )
			return;

		if ( Agent.LastStimulus is null )
			return;

		Agent.WalkTo( Agent.LastStimulus.Position );
	}

	public override void OnStateEnter( StateMachine.State prev )
	{
		base.OnStateEnter( prev );
		forceIntoState = false;
	}

	public override void OnEvent( string eventName, params object[] obj )
	{
		if ( eventName == "damage" )
		{
			forceIntoState = true;
		}
	}

	public override UpperBodyState GetUpperAnimationState()
	{
		return UpperBodyState.Charge;
	}
}