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

public partial class OpenDoorState : 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;
	private DoorComponent target;

	public override void Tick()
	{
		target ??= Agent.FindNearestDoor();

		if ( target == null )
		{
			forceIntoState = false;
			return;
		}

		if ( !target.IsOpen && !target.Locked )
			target.ToggleDoor( GameObject );

		if ( target.IsOpen )
			forceIntoState = false;
	}

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

	public override void OnStateEnter( StateMachine.State prev )
	{
		target = Agent.FindNearestDoor();
	}

	public override bool ShouldEnterState( StateMachine machine )
	{
		return forceIntoState;
	}
}