class forklift extends InteractiveDevice
{
	protected instanceeditable var m_reversed : Bool;
	protected var m_animFeature : AnimFeature_ForkliftDevice;
	protected var m_animationController : AnimationControllerComponent;
	protected var m_isPlayerUnder : Bool;
	protected var m_cargoBox : PhysicalMeshComponent;

	protected event OnRequestComponents( ri : EntityRequestComponentsInterface )
	{
		super.OnRequestComponents( ri );
		EntityRequestComponentsInterface.RequestComponent( ri, 'animController', 'AnimationControllerComponent', false );
		EntityRequestComponentsInterface.RequestComponent( ri, 'cargo_box', 'PhysicalMeshComponent', true );
	}

	protected event OnTakeControl( ri : EntityResolveComponentsInterface )
	{
		super.OnTakeControl( ri );
		m_animationController = ( ( AnimationControllerComponent )( EntityResolveComponentsInterface.GetComponent( ri, 'animController' ) ) );
		m_animationController.Toggle( true );
		m_cargoBox = ( ( PhysicalMeshComponent )( EntityResolveComponentsInterface.GetComponent( ri, 'cargo_box' ) ) );
		m_controller = ( ( ForkliftController )( EntityResolveComponentsInterface.GetComponent( ri, 'controller' ) ) );
		m_isPlayerUnder = false;
	}

	protected event OnGameAttached()
	{
		super.OnGameAttached();
		m_animFeature = new AnimFeature_ForkliftDevice;
		UpdateAnimState();
	}

	protected const override function GetController() : ForkliftController
	{
		return ( ( ForkliftController )( m_controller ) );
	}

	public const override function GetDevicePS() : ForkliftControllerPS
	{
		return GetController().GetPS();
	}

	protected event OnActivateDevice( evt : ActivateDevice )
	{
		var forkliftCompleteActivateEvent : ForkliftCompleteActivateEvent;
		var isForkliftUp : Bool;
		forkliftCompleteActivateEvent = new ForkliftCompleteActivateEvent;
		isForkliftUp = GetDevicePS().IsForkliftUp();
		if( m_reversed )
		{
			isForkliftUp = !( isForkliftUp );
		}
		if( !( isForkliftUp ) )
		{
			if( m_isPlayerUnder )
			{
				( ( PlayerPuppet )( GameInstance.GetPlayerSystem( GetGame() ).GetLocalPlayerControlledGameObject() ) ).Kill( this );
				m_cargoBox.ToggleCollision( false );
			}
		}
		UpdateAnimState();
		GameInstance.GetDelaySystem( GetGame() ).DelayEvent( this, forkliftCompleteActivateEvent, GetDevicePS().GetLiftingAnimationTime() );
	}

	private function UpdateAnimState()
	{
		m_animFeature.isDown = !( GetDevicePS().IsForkliftUp() );
		m_animFeature.isUp = GetDevicePS().IsForkliftUp();
		m_animFeature.distract = GetDevicePS().IsDistracting();
		if( m_reversed )
		{
			m_animFeature.isDown = !( m_animFeature.isDown );
			m_animFeature.isUp = !( m_animFeature.isUp );
		}
		AnimationControllerComponent.ApplyFeature( this, 'ForkliftAnimFeature', m_animFeature );
		if( !( m_wasAnimationFastForwarded ) )
		{
			FastForwardAnimations();
		}
	}

	protected event OnForkliftCompleteActivateEvent( evt : ForkliftCompleteActivateEvent )
	{
		GetDevicePS().ChangeState( EDeviceStatus.ON );
		RefreshDeviceInteractions();
	}

	protected function RefreshDeviceInteractions()
	{
		var playerPuppet : PlayerPuppet;
		playerPuppet = ( ( PlayerPuppet )( GameInstance.GetPlayerSystem( GetGame() ).GetLocalPlayerMainGameObject() ) );
		RefreshInteraction( gamedeviceRequestType.Direct, playerPuppet );
		UpdateAnimState();
	}

	protected override function StartGlitching( glitchState : EGlitchState, optional intensity : Float )
	{
		UpdateAnimState();
		GameObjectEffectHelper.ActivateEffectAction( this, gamedataFxActionType.Start, 'fx_distraction' );
	}

	protected override function StopGlitching()
	{
		GameObjectEffectHelper.ActivateEffectAction( this, gamedataFxActionType.BreakLoop, 'fx_distraction' );
		RefreshDeviceInteractions();
	}

	protected event OnPlayerEnter( evt : AreaEnteredEvent )
	{
		m_isPlayerUnder = true;
	}

	protected event OnPlayerExit( evt : AreaExitedEvent )
	{
		m_isPlayerUnder = false;
	}

}

class ForkliftCompleteActivateEvent extends Event
{
}

