class BaseAnimatedDevice extends InteractiveDevice
{
	default m_controllerTypeName = 'BaseAnimatedDeviceController';
	[ category = "AnimationSetup" ]
	instanceeditable var m_openingSpeed : Float;
	default m_openingSpeed = 2.0f;
	[ category = "AnimationSetup" ]
	instanceeditable var m_closingSpeed : Float;
	default m_closingSpeed = 2.0f;
	protected var m_animationController : AnimationControllerComponent;
	protected var m_animFeature : AnimFeature_RoadBlock;
	protected editable var m_animationType : EAnimationType;

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

	protected event OnTakeControl( ri : EntityResolveComponentsInterface )
	{
		super.OnTakeControl( ri );
		m_animationController = ( ( AnimationControllerComponent )( EntityResolveComponentsInterface.GetComponent( ri, 'animController' ) ) );
		m_controller = ( ( BaseAnimatedDeviceController )( EntityResolveComponentsInterface.GetComponent( ri, 'controller' ) ) );
	}

	protected override function ResolveGameplayState()
	{
		super.ResolveGameplayState();
		if( m_animationType == EAnimationType.REGULAR )
		{
			m_animFeature = new AnimFeature_RoadBlock;
			m_animFeature.initOpen = GetDevicePS().IsActive();
		}
		ActivateAnimation();
	}

	private const override function GetController() : BaseAnimatedDeviceController
	{
		return ( ( BaseAnimatedDeviceController )( m_controller ) );
	}

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

	protected event OnQuickHackToggleActivate( evt : QuickHackToggleActivate )
	{
		ActivateAnimation();
		UpdateDeviceState();
	}

	protected event OnActivateDevice( evt : ActivateDevice )
	{
		ActivateAnimation();
		UpdateDeviceState();
	}

	protected event OnDeactivateDevice( evt : DeactivateDevice )
	{
		ActivateAnimation();
		UpdateDeviceState();
	}

	protected override function DeactivateDevice()
	{
		super.DeactivateDevice();
		m_animationController.Toggle( false );
	}

	protected override function ActivateDevice()
	{
		super.ActivateDevice();
		m_animationController.Toggle( true );
	}

	protected virtual function ActivateAnimation()
	{
		if( m_animationType == EAnimationType.REGULAR )
		{
			Animate();
		}
		else if( m_animationType == EAnimationType.TRANSFORM )
		{
			InvokePlayAnimationEvent();
		}
	}

	protected virtual function Animate()
	{
		if( GetDevicePS().IsActive() )
		{
			m_animFeature.isOpening = true;
			m_animFeature.duration = m_openingSpeed;
		}
		else
		{
			m_animFeature.isOpening = false;
			m_animFeature.duration = m_closingSpeed;
		}
		AnimationControllerComponent.ApplyFeature( this, 'Road_block', m_animFeature );
	}

	private function InvokePlayAnimationEvent()
	{
		var playEvent : gameTransformAnimationPlayEvent;
		playEvent = new gameTransformAnimationPlayEvent;
		playEvent.looping = false;
		playEvent.timesPlayed = 1;
		playEvent.timeScale = GetTimeScale();
		playEvent.animationName = GetCurrentAnimationName();
		QueueEvent( playEvent );
		OnPlayAnimation();
	}

	protected virtual function GetTimeScale() : Float
	{
		if( GetDevicePS().Randomize() )
		{
			return RandRangeF( 0.80000001, 1.20000005 );
		}
		return 1.0;
	}

	private function GetCurrentAnimationName() : CName
	{
		return ( ( GetDevicePS().IsActive() ) ? ( 'opening' ) : ( 'closing' ) );
	}

	protected virtual function OnPlayAnimation() {}

	public const override function DeterminGameplayRole() : EGameplayRole
	{
		return EGameplayRole.ClearPath;
	}

}

