class ClaymoreMine extends WeaponObject
{
	private var m_visualComponent : MeshComponent;
	private var m_triggerAreaIndicator : MeshComponent;
	private var m_shootCollision : SimpleColliderComponent;
	private var m_triggerComponent : TriggerComponent;
	private var m_alive : Bool;
	default m_alive = true;
	private var m_armed : Bool;
	default m_armed = false;

	protected event OnRequestComponents( ri : EntityRequestComponentsInterface )
	{
		super.OnRequestComponents( ri );
		EntityRequestComponentsInterface.RequestComponent( ri, 'ClaymoreMesh', 'MeshComponent', false );
		EntityRequestComponentsInterface.RequestComponent( ri, 'TriggerAreaIndicator', 'MeshComponent', true );
		EntityRequestComponentsInterface.RequestComponent( ri, 'ShootCollision', 'SimpleColliderComponent', true );
	}

	protected event OnTakeControl( ri : EntityResolveComponentsInterface )
	{
		super.OnTakeControl( ri );
		m_visualComponent = ( ( MeshComponent )( EntityResolveComponentsInterface.GetComponent( ri, 'ClaymoreMesh' ) ) );
		m_triggerAreaIndicator = ( ( MeshComponent )( EntityResolveComponentsInterface.GetComponent( ri, 'TriggerAreaIndicator' ) ) );
		m_shootCollision = ( ( SimpleColliderComponent )( EntityResolveComponentsInterface.GetComponent( ri, 'ShootCollision' ) ) );
		m_triggerComponent = ( ( TriggerComponent )( EntityResolveComponentsInterface.GetComponent( ri, 'Trigger' ) ) );
	}

	protected event OnGameAttached()
	{
		var mineArmEvent : MineArmEvent;
		mineArmEvent = new MineArmEvent;
		GameInstance.GetDelaySystem( GetGame() ).DelayEvent( this, mineArmEvent, 2.0 );
		AdjustRotation();
	}

	protected event OnProjectileInitialize( eventData : gameprojectileSetUpEvent ) {}

	protected event OnHit( evt : gameHitEvent )
	{
		var sourceIsPlayer : Bool;
		sourceIsPlayer = evt.attackData.GetInstigator() == GetPlayer( GetGame() );
		if( m_alive && sourceIsPlayer )
		{
			Explode();
		}
	}

	private function AdjustRotation()
	{
		var normal : Vector4;
		var rotation : EulerAngles;
		normal = GameInstance.GetBlackboardSystem( GetGame() ).Get( GetAllBlackboardDefs().Mines ).GetVector4( GetAllBlackboardDefs().Mines.CurrentNormal );
		rotation = Vector4.ToRotation( normal );
		m_visualComponent.SetLocalOrientation( EulerAngles.ToQuat( rotation ) );
		m_triggerComponent.SetLocalOrientation( EulerAngles.ToQuat( rotation ) );
		m_shootCollision.SetLocalOrientation( EulerAngles.ToQuat( rotation ) );
	}

	protected event OnAreaEnter( evt : AreaEnteredEvent )
	{
		var sourceIsPlayer : Bool;
		var activatorID : EntityID;
		activatorID = EntityGameInterface.GetEntity( evt.activator ).GetEntityID();
		sourceIsPlayer = activatorID == GetPlayer( GetGame() ).GetEntityID();
		if( ( m_armed && m_alive ) && !( sourceIsPlayer ) )
		{
			Explode();
		}
	}

	protected event OnAreaExit( evt : AreaExitedEvent ) {}

	private function Explode()
	{
		var despawnEvent : MineDespawnEvent;
		var explosionRadius : Float;
		var impulseRadius : Float;
		var attackRecord : Attack_Record;
		explosionRadius = TDB.GetFloat( T"weapons.E3_grenade.damageRadius" );
		impulseRadius = TDB.GetFloat( T"weapons.E3_grenade.physicalImpulseRadius" );
		attackRecord = TweakDBInterface.GetAttackRecord( T"Attacks.FragGrenade" );
		CombatGadgetHelper.SpawnAttack( this, explosionRadius, attackRecord, GetPlayer( GetGame() ) );
		CombatGadgetHelper.SpawnPhysicalImpulse( this, impulseRadius );
		GameObject.PlayMetadataEvent( this, 'exploded' );
		GameInstance.GetAudioSystem( GetGame() ).PlayShockwave( 'explosion', GetWorldPosition() );
		m_alive = false;
		m_visualComponent.Toggle( false );
		despawnEvent = new MineDespawnEvent;
		GameInstance.GetDelaySystem( GetGame() ).DelayEvent( this, despawnEvent, 0.5 );
	}

	protected event OnRelease( evt : MineDespawnEvent )
	{
		var objectPool : ObjectPoolSystem;
		objectPool = GameInstance.GetObjectPoolSystem( GetGame() );
		objectPool.Release( this );
	}

	protected event OnArmed( evt : MineArmEvent )
	{
		m_armed = true;
	}

	protected event OnScanningEvent( evt : ScanningEvent )
	{
		if( evt.state == gameScanningState.Complete )
		{
			ToggleTriggerAreaIndicator( true );
		}
	}

	protected event OnScanningLookedAt( evt : ScanningLookAtEvent )
	{
		super.OnScanningLookedAt( evt );
		if( m_scanningComponent.IsScanned() )
		{
			if( evt.state )
			{
				ToggleTriggerAreaIndicator( evt.state );
			}
		}
	}

	protected function ToggleTriggerAreaIndicator( visible : Bool )
	{
		m_triggerAreaIndicator.Toggle( visible );
	}

	protected event OnMinePlace( evt : PlaceMineEvent )
	{
		var item : ItemID;
		var m_position : Vector4;
		item = ItemID.FromTDBID( T"Items.claymore_mine" );
		m_position = evt.m_position;
		GameInstance.GetBlackboardSystem( GetGame() ).Get( GetAllBlackboardDefs().Mines ).SetVector4( GetAllBlackboardDefs().Mines.CurrentNormal, evt.m_normal );
		GameInstance.GetLootManager( GetGame() ).SpawnItemDrop( GameInstance.GetPlayerSystem( GetGame() ).GetLocalPlayerMainGameObject(), item, m_position );
	}

}

class MineDespawnEvent extends Event
{
}

class MineArmEvent extends Event
{
}

class PlaceMineEvent extends Event
{
	var m_position : Vector4;
	var m_normal : Vector4;
}

