//=============================================================================
// Target.
//=============================================================================
class Target expands Translocator;

#exec MESH IMPORT MESH=TeleportProj ANIVFILE=MODELS\telemo_a.3D DATAFILE=MODELS\telemo_d.3D X=0 Y=0 Z=0
#exec MESH ORIGIN MESH=TeleportProj X=0 Y=0 Z=0 YAW=64 PITCH=0
#exec MESH SEQUENCE MESH=TeleportProj SEQ=All  STARTFRAME=0  NUMFRAMES=1
//#exec TEXTURE IMPORT NAME=Ahand1 FILE=MODELS\hand.PCX
#exec MESHMAP SCALE MESHMAP=TeleportProj X=0.03 Y=0.03 Z=0.06
#exec MESHMAP SETTEXTURE MESHMAP=TeleportProj NUM=0 TEXTURE=Ahand1

var pawn OriginPawn;

function Fire( float Value )
{
	bFireMem = false;
	bAltFireMem = false;
	bPointing=True;
 	GotoState('NormalFire');
}

function AltFire( float Value )
{
	bFireMem = false;
	bAltFireMem = false;
	bPointing=True;
	GoToState('AltFiring');
}

////////////////////////////////////////////////////////
state AltFiring
{
Begin:
	PlayAnim('Down',0.6);
	FinishAnim();
	PlayAnim('Finger',0.5);
	FinishAnim();
	PlayAnim('Select',0.6);
	FinishAnim();	
	GoToState('Idle');
}

function Toss()
{
	local Target TTarget;
	local Vector X,Y,Z;

	PlaySound(AltFireSound, SLOT_Misc, Pawn(Owner).SoundDampening);	
	GetAxes(Pawn(Owner).ViewRotation,X,Y,Z);
	TTarget = Spawn(class 'Target',None,'',Location + 11.0 * X - 8 * Y - 2 * Z); 
	TTarget.Tag = Tag;
	TTarget.OriginPawn = Pawn(Owner);
	TTarget.GoToState('Throw');
}

////////////////////////////////////////////////////////
state NormalFire
{
	function Fire( float Value ){ }
	function AltFire( float Value ) { }

	function SwitchToSource() 
	{
		local Inventory Inv;

		for( Inv=Owner.Inventory; Inv!=None; Inv=Inv.Inventory ) 
			if (Translocator(Inv)!=None ) 
			{	
				Pawn(Owner).PendingWeapon = Weapon(Inv);	
				Pawn(Owner).ChangedWeapon();
				Destroy();
				return;			
			}

		Pawn(Owner).SwitchToBestWeapon();
		Destroy();
	}

Begin:
	InvCalcView();
	PlayAnim('Throw',0.4);
	FinishAnim();
	PlayAnim('Select');	
	Pawn(Owner).DeleteInventory(Self);
	SwitchToSource();		
}

////////////////////////////////////////////////////////
state Active
{

Begin:
//	skin = texture'AHand2';
	PlayAnim('Select',0.9);
	FinishAnim();
	PlaySound(SelectSound, SLOT_Misc, Pawn(Owner).SoundDampening);	
	GoToState('Idle');
}

////////////////////////////////////////////////////////
auto state Pickup
{

	function Touch( actor Other )
	{	
		if( ValidTouch(Other) )
		{
			Pawn(Other).AddInventory(self);
			RemoteRole    = ROLE_DumbProxy;
			Mesh          = PlayerViewMesh;
			DrawScale     = PlayerViewScale;
			bOnlyOwnerSee = false;
			bHidden       = true;
			SetCollision( false, false, false );
			Pawn(Other).ClientMessage(PickupMessage);
			WeaponSet(Pawn(Other));
		}
	}
}

////////////////////////////////////////////////////////
state Throw
{
	function Touch( actor Other )
	{	
		if( Pawn(Other)!=None && Pawn(Other).bIsPlayer )
		{
			Pawn(Other).AddInventory(self);
			RemoteRole    = ROLE_DumbProxy;
			Mesh          = PlayerViewMesh;
			DrawScale     = PlayerViewScale;
			bOnlyOwnerSee = false;
			bHidden       = true;
			SetCollision( false, false, false );
			Pawn(Other).ClientMessage(PickupMessage);
			WeaponSet(Pawn(Other));			
		}
	}
	
	function HitWall (vector HitNormal, actor Wall)
	{
		PlaySound(Misc2Sound, SLOT_Misc, Pawn(Owner).SoundDampening);	  // hit wall sound
		Velocity = 0.8*(( Velocity dot HitNormal ) * HitNormal * (-2.0) + Velocity);   // Reflect off Wall w/damping
		If (Size(Velocity) < 20) 
		{
			bBounce = False;
			SetPhysics(PHYS_None);
			GoToState('Pickup');
		}
	}
	
	function Timer()
	{
		bHidden=False;
		BecomePickup();	
		enable('Touch');		
	}

	function BeginState()
	{
		local Rotation ProjRot;

		Disable('Touch');
		SetPhysics(PHYS_Falling);
		ProjRot = Rotation;
		ProjRot.Roll += 7000;
		SetRotation(ProjRot);
		Velocity = OriginPawn.Velocity + Vector(Rotation) * 300;
		Velocity.z += 120;
		RotationRate = rot(15000,15000,15000);
		DesiredRotation = Rotation(vect(0,0,1));
		bRotateToDesired=True;
		SetTimer(0.1,False);
		bHidden=True;          	
		bBounce=True;
		SetOwner(none);	
		PlaySound(Misc1Sound, SLOT_Misc, Pawn(Owner).SoundDampening);	// Throw sound
	}
}

