//=============================================================================
// MiniGunSentry.
//=============================================================================
class MiniGunSentry expands Pickup;

#exec MESH IMPORT MESH=Sentry ANIVFILE=MODELS\sentry_a.3D DATAFILE=MODELS\sentry_d.3D X=0 Y=0 Z=0
#exec MESH ORIGIN MESH=Sentry X=-90 Y=90 Z=-80 YAW=64
#exec MESH SEQUENCE MESH=Sentry SEQ=All    STARTFRAME=0  NUMFRAMES=46
#exec MESH SEQUENCE MESH=Sentry SEQ=Up     STARTFRAME=0  NUMFRAMES=5
#exec MESH SEQUENCE MESH=Sentry SEQ=Still  STARTFRAME=5  NUMFRAMES=1
#exec MESH SEQUENCE MESH=Sentry SEQ=WarmUp STARTFRAME=6  NUMFRAMES=14
#exec MESH SEQUENCE MESH=Sentry SEQ=Fire   STARTFRAME=20 NUMFRAMES=4
#exec MESH SEQUENCE MESH=Sentry SEQ=Down   STARTFRAME=24  NUMFRAMES=21
//#exec TEXTURE IMPORT NAME=Ainv1 FILE=MODELS\sentry.PCX GROUP="Skins"
#exec MESHMAP SCALE MESHMAP=Sentry X=0.06 Y=0.06 Z=0.12
#exec MESHMAP SETTEXTURE MESHMAP=Sentry NUM=1 TEXTURE=Default

#exec MESH NOTIFY MESH=Sentry SEQ=Fire TIME=0.3 FUNCTION=GenerateBullet
#exec MESH NOTIFY MESH=Sentry SEQ=Fire TIME=0.8 FUNCTION=GenerateBullet

var BulletHit Bullet;
var vector X,Y,Z;	
var Vector HitNorm,HitLocation;
var Actor Other;

state Activated  // Delete from inventory and toss in front of player.
{
	function Timer()
	{
		BecomePickup();
	}
	function HitWall (vector HitNormal, actor Wall)
	{
		Velocity = 0.8*(( Velocity dot HitNormal ) * HitNormal * (-2.0) + Velocity);   // Reflect off Wall w/damping
	//	PlaySound(Sound 'GrenadeFloor', SLOT_Misc, Size(Velocity)/1300 );
		If (Size(Velocity) < 20)
		{
			bBounce = False;
			SetPhysics(PHYS_None);
			GoToState('Monitor');
		}
	}
Begin:
	GetAxes(Pawn(Owner).ViewRotation,X,Y,Z);
    SetCollision(False,False,False);
	SetPhysics(PHYS_Falling);
	SetRotation(Owner.Rotation);
	Velocity = Owner.Velocity + Vector(Owner.Rotation) * 180.0;
	Velocity.z += 120;
	SetTimer(0.3,false);          
	SetLocation(Owner.Location+Y*10-Z*20+X*15);
	Pawn(Owner).NextItem();
	if (Pawn(Owner).SelectedItem == Self) Pawn(Owner).SelectedItem=None;	
	Pawn(Owner).DeleteInventory(Self);
	bBounce=True;
}

////////////////////////////////////////////////////////////////////
//
// Monitor path in front of Sentry.  If pawn is found, start firing
//
auto state Monitor
{
	function Timer()
	{
		
		GetAxes(Rotation,X,Y,Z);	
		Other = Trace(HitLocation,HitNorm,Location+1000*Vector(Rotation),Location,True);
		if (Pawn(Other) != None) GoToState('Firing');
	}
Begin:
	SetOwner(None);
	PlayAnim('Up',0.2);
	FinishAnim();
Begin2:
	SetTimer(0.2,True);	
	PlayAnim('Still');
	FinishAnim();
}

Function GenerateBullet()
{
	Charge--;
	if (Charge <= 0) GoToState('Depleted');

	Owner.MakeNoise(1.0);
	GetAxes(Rotation,X,Y,Z);
	Other = Trace(HitLocation, HitNorm,
		Location + 10000 * Vector(Rotation),Location,True);
	if (Other == Level)   // Hit a wall
	{
		Bullet = spawn(class'BulletHit',,,HitLocation+HitNorm*5,rotation(HitNorm));
		Bullet.AnimType = 'Fall';			
	}
	else if ((Other != self) && (Other != Owner))
	{
		Other.Damage(30, Pawn(Owner), HitLocation, 13000.0*vector(Rotation), 'shot');			
	}
	else
	{
		log("I don't know what sentry shot");		
	}
}

//
// Fire until ammo used up or nothing is in front of sentry
//
state Firing
{
	function Timer()
	{
		GetAxes(Rotation,X,Y,Z);
		Other = Trace(HitLocation,HitNorm,Location+10000*Vector(Rotation),Location,True);
		if (Other == Level) GoToState('Monitor','Begin2');
	}
Begin:
	SetTimer(0.3,True);
	PlayAnim('WarmUp');
	FinishAnim();
	LoopAnim('Fire',0.3,0.0);
}

//
// Ammo Gone, shut down and destroy.
//
state Depleted
{
Begin:
	SetCollision(False,False,False);
	PlayAnim('Down',0.2);
	FinishAnim();
	Destroy();
}

