class ActivatedDeviceTrap extends ActivatedDeviceTransfromAnim
{
	default m_controllerTypeName = 'ActivatedDeviceController';
	protected instanceeditable var m_areaComponent : TriggerComponent;

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

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

	protected override function RefreshAnimation()
	{
		if( GetDevicePS().IsDisabled() )
		{
			SendSimpleAnimFeature( true, false, false );
		}
		else if( GetDevicePS().IsDistracting() )
		{
			SendSimpleAnimFeature( false, true, false );
		}
		else
		{
			SendSimpleAnimFeature( false, false, false );
		}
		if( !( m_wasAnimationFastForwarded ) )
		{
			FastForwardAnimations();
		}
	}

	protected event OnActivateDevice( evt : ActivateDevice )
	{
		var evnt : TimerEvent;
		evnt = new TimerEvent;
		super.OnActivateDevice( evt );
		GameInstance.GetDelaySystem( GetGame() ).DelayEvent( this, evnt, ( ( ActivatedDeviceControllerPS )( GetDevicePS() ) ).GetAnimationTime() );
	}

	protected event OnTimerEvent( evt : TimerEvent )
	{
		DoAttack( ( ( ActivatedDeviceControllerPS )( GetDevicePS() ) ).GetAttackType() );
		SpawnVFXs( ( ( ActivatedDeviceControllerPS )( GetDevicePS() ) ).GetVFX() );
	}

	protected function DoAttack( attackRecord : TweakDBID )
	{
		var i : Int32;
		var attack : Attack_GameEffect;
		var hitFlags : array< SHitFlag >;
		var entities : array< Entity >;
		var npcType : gamedataNPCType;
		entities = GetEntitiesInArea();
		for( i = 0; i < entities.Size(); i += 1 )
		{
			if( ( ( NPCPuppet )( entities[ i ] ) ) )
			{
				npcType = ( ( NPCPuppet )( entities[ i ] ) ).GetNPCType();
				if( StatusEffectSystem.ObjectHasStatusEffect( ( ( NPCPuppet )( entities[ i ] ) ), T"BaseStatusEffect.Unconscious" ) )
				{
					StatusEffectHelper.ApplyStatusEffect( ( ( NPCPuppet )( entities[ i ] ) ), T"BaseStatusEffect.ForceKill", GetEntityID() );
					continue;
				}
				else if( StatusEffectSystem.ObjectHasStatusEffect( ( ( NPCPuppet )( entities[ i ] ) ), T"BaseStatusEffect.Grappled" ) )
				{
					StatusEffectHelper.ApplyStatusEffect( ( ( NPCPuppet )( entities[ i ] ) ), T"BaseStatusEffect.Unconscious", GetEntityID() );
				}
				else if( npcType == gamedataNPCType.Human || npcType == gamedataNPCType.Android )
				{
					if( GetDevicePS().ShouldRadgollOnAttack() )
					{
						entities[ i ].QueueEvent( CreateForceRagdollEvent( 'Hit by a trap device - activatedDeviceTrap.script' ) );
					}
				}
			}
		}
		attack = RPGManager.PrepareGameEffectAttack( GetGame(), GameInstance.GetPlayerSystem( GetGame() ).GetLocalPlayerMainGameObject(), this, attackRecord, , hitFlags );
		if( attack )
		{
			attack.StartAttack();
		}
	}

	protected function Distract()
	{
		var i : Int32;
		var distractionName : CName;
		var areaEffectData : AreaEffectData;
		distractionName = StringToName( "distractEffect" );
		for( i = 0; i < GetFxResourceMapper().GetAreaEffectDataSize(); i += 1 )
		{
			if( GetFxResourceMapper().GetAreaEffectDataByIndex( i ).areaEffectID == distractionName )
			{
				areaEffectData = GetFxResourceMapper().GetAreaEffectDataByIndex( i );
				TriggerArreaEffectDistraction( areaEffectData );
				break;
			}
		}
	}

	public function GetEntitiesInArea() : array< Entity >
	{
		return m_areaComponent.GetOverlappingEntities();
	}

	protected event OnAreaEnter( evt : AreaEnteredEvent )
	{
		if( ( ( ActivatedDeviceControllerPS )( GetDevicePS() ) ).ShouldActivateTrapOnAreaEnter() && !( GetDevicePS().IsDisabled() ) )
		{
			DoAttack( ( ( ActivatedDeviceControllerPS )( GetDevicePS() ) ).GetAttackType() );
			SpawnVFXs( ( ( ActivatedDeviceControllerPS )( GetDevicePS() ) ).GetVFX() );
		}
	}

	protected override function ResolveGameplayState()
	{
		var i : Int32;
		super.ResolveGameplayState();
		if( m_fxResourceMapper )
		{
			m_fxResourceMapper.CreateEffectStructDataFromAttack( ( ( ActivatedDeviceControllerPS )( GetDevicePS() ) ).GetAttackType(), i, 'trapTargets' );
		}
	}

}

class EffectObjectProvider_TrapEntities extends EffectObjectProvider_Scripted
{

	public function Process( ctx : EffectScriptContext, providerCtx : EffectProviderScriptContext )
	{
		var entities : array< Entity >;
		var i : Int32;
		entities = ( ( ActivatedDeviceTrap )( EffectScriptContext.GetSource( ctx ) ) ).GetEntitiesInArea();
		for( i = 0; i < entities.Size(); i += 1 )
		{
			EffectProviderScriptContext.AddTarget( ctx, providerCtx, entities[ i ] );
		}
	}

}

