class BaseDestructibleDevice extends Device
{
	default m_controllerTypeName = 'BaseDestructibleController';
	protected editable var m_minTime : Float;
	default m_minTime = 5.0f;
	protected editable var m_maxTime : Float;
	default m_maxTime = 10.0f;
	protected var m_destroyedMesh : PhysicalMeshComponent;

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

	protected event OnTakeControl( ri : EntityResolveComponentsInterface )
	{
		super.OnTakeControl( ri );
		m_destroyedMesh = ( ( PhysicalMeshComponent )( EntityResolveComponentsInterface.GetComponent( ri, 'mesh_destroyed' ) ) );
		m_controller = ( ( BaseDestructibleController )( EntityResolveComponentsInterface.GetComponent( ri, 'controller' ) ) );
	}

	protected override function ResolveGameplayState()
	{
		super.ResolveGameplayState();
		if( GetDevicePS().IsMasterDestroyed() || GetDevicePS().IsDestroyed() )
		{
			DeactivateDeviceSilent();
		}
		else
		{
			ActivateDevice();
		}
	}

	protected event OnMasterDeviceDestroyed( evt : MasterDeviceDestroyed )
	{
		CreateDestructionEffects();
	}

	protected event OnPhysicalDestructionEvent( evt : PhysicalDestructionEvent )
	{
		GetDevicePS().SetDestroyed();
		GetDevicePS().ForceDisableDevice();
		CreateDestructionEffects();
	}

	protected virtual function CreateDestructionEffects()
	{
		var delayEvent : DelayEvent;
		delayEvent = new DelayEvent;
		CreatePhysicalBody();
		GameInstance.GetDelaySystem( GetGame() ).DelayEvent( this, delayEvent, RandRangeF( m_minTime, m_maxTime ) );
	}

	protected virtual function CreatePhysicalBody()
	{
		var meshInterface : PhysicalBodyInterface;
		if( !( m_destroyedMesh.IsEnabled() ) )
		{
			m_destroyedMesh.Toggle( true );
		}
		meshInterface = m_destroyedMesh.CreatePhysicalBodyInterface();
		meshInterface.ToggleKinematic( true );
		meshInterface.AddLinearImpulse( Vector4( 0.0, 0.5, 0.0, 0.0 ), true );
	}

	protected event OnDelayEvent( evt : DelayEvent )
	{
		HidePhysicalBody();
	}

	protected virtual function HidePhysicalBody()
	{
		m_destroyedMesh.Toggle( false );
	}

	protected override function DeactivateDevice()
	{
		GetDevicePS().GetDeviceOperationsContainer().Execute( 'death_VFX', this );
	}

	protected virtual function DeactivateDeviceSilent()
	{
		if( m_destroyedMesh.IsEnabled() )
		{
			m_destroyedMesh.Toggle( false );
		}
	}

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

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

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

}

