class RoadBlockTrap extends InteractiveMasterDevice
{
	default m_controllerTypeName = 'RoadBlockTrapController';
	protected instanceeditable var m_areaComponent : TriggerComponent;

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

	protected event OnTakeControl( ri : EntityResolveComponentsInterface )
	{
		super.OnTakeControl( ri );
		m_areaComponent = ( ( TriggerComponent )( EntityResolveComponentsInterface.GetComponent( ri, 'trapArea' ) ) );
		m_controller = ( ( RoadBlockTrapController )( EntityResolveComponentsInterface.GetComponent( ri, 'controller' ) ) );
	}

	protected event OnActivateDevice( evt : ActivateDevice )
	{
		if( IsPlayerInside() )
		{
			GetDevicePS().RefreshSlaves_Event();
		}
	}

	protected event OnAreaEnter( evt : AreaEnteredEvent )
	{
		var activator : weak< GameObject >;
		super.OnAreaEnter( evt );
		if( evt.componentName != 'trapArea' )
		{
			return false;
		}
		if( !( GetDevicePS().IsActivated() ) || GetDevicePS().IsDisabled() )
		{
			return false;
		}
		activator = ( ( GameObject )( EntityGameInterface.GetEntity( evt.activator ) ) );
		if( activator.IsPlayer() )
		{
			TrapPlayer( ( ( PlayerPuppet )( activator ) ) );
			GetDevicePS().RefreshSlaves_Event();
			GetDevicePS().ForceDisableDevice();
		}
	}

	protected event OnAreaExit( evt : AreaExitedEvent )
	{
		super.OnAreaExit( evt );
	}

	private function IsPlayerInside() : Bool
	{
		var puppets : array< Entity >;
		var i : Int32;
		puppets = m_areaComponent.GetOverlappingEntities();
		if( puppets.Size() > 0 )
		{
			for( i = 0; i < puppets.Size(); i += 1 )
			{
				if( ( ( PlayerPuppet )( puppets[ i ] ) ) )
				{
					return true;
				}
			}
		}
		return false;
	}

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

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

	private final function TrapPlayer( player : weak< PlayerPuppet > )
	{
		var ev : PSMImpulse;
		var playerVelocity : Vector4;
		ev = new PSMImpulse;
		ev.id = 'impulse';
		playerVelocity = player.GetVelocity();
		playerVelocity.Z = 0.0;
		ev.impulse = -1.0 * playerVelocity;
		player.QueueEvent( ev );
		GameInstance.GetTeleportationFacility( GetGame() ).Teleport( player, this.GetWorldPosition(), Vector4.ToRotation( player.GetWorldForward() ) );
	}

}

