//=============================================================================
// PeaceBarrel.
//=============================================================================
class PeaceBarrel expands Projectile;

#exec MESH IMPORT MESH=peace ANIVFILE=MODELS\peace_a.3D DATAFILE=MODELS\peace_d.3D X=0 Y=0 Z=0
#exec MESH ORIGIN MESH=peace X=0 Y=0 Z=0 YAW=-64 ROLL=0 PITCH=0
#exec MESH SEQUENCE MESH=peace SEQ=All       STARTFRAME=0   NUMFRAMES=33
#exec MESH SEQUENCE MESH=peace SEQ=UnFold    STARTFRAME=0   NUMFRAMES=29
#exec MESH SEQUENCE MESH=peace SEQ=Launch1   STARTFRAME=29  NUMFRAMES=1
#exec MESH SEQUENCE MESH=peace SEQ=Launch2   STARTFRAME=30  NUMFRAMES=1
#exec MESH SEQUENCE MESH=peace SEQ=Launch3   STARTFRAME=31  NUMFRAMES=1
#exec MESH SEQUENCE MESH=peace SEQ=Launch4   STARTFRAME=32  NUMFRAMES=1
#exec TEXTURE IMPORT NAME=Jpeace1 FILE=MODELS\peaceg.PCX
#exec MESHMAP SCALE MESHMAP=peace  X=0.08 Y=0.08 Z=0.16
#exec MESHMAP SETTEXTURE MESHMAP=peace NUM=1 TEXTURE=Jpeace1

var float CountDown;
var Rotation Rot;
var int i;
var bool bFireType;

function Timer()
{
	GoToState('UnFolding');
}

singular function Touch (Actor Other)
{
	local vector HitNormal;
	local PeaceMaker P;
	if (Pawn(Other).bIsPlayer) {
		P = Spawn(Class'PeaceMaker');
		P.Touch(Other);
		Destroy();
	}
	HitNormal = Other.Location - Location;
	HitWall(HitNormal, None);
}

auto state Flying
{
	function Landed( vector HitNormal )
	{	
		HitWall( HitNormal, None );	
	}
	function HitWall( vector HitNormal, actor Wall )
	{
		if ( (Mover(Wall) != None) && Mover(Wall).bDamageTriggered )
			Wall.Damage( Damage, instigator, Location, MomentumTransfer * Normal(Velocity), '');
		PlaySound(ImpactSound);
		SetPhysics(PHYS_Falling);
		MakeNoise(0.6);		
		Velocity = 0.8*(( Velocity dot HitNormal ) * HitNormal * (-1.8 + FRand()*0.8) + Velocity);   // Reflect off Wall w/damping
		SetRotation(Rotation(Velocity));
		if (Size(Velocity)<40) GoToState('AtRest');
	}

	function BeginState()
	{
		local Vector X,Y,Z;
	
		GetAxes(Instigator.ViewRotation,X,Y,Z);	
		Velocity = (X dot Instigator.Velocity) * X +     // Impart ONLY forward
			Vector(Rotation) * 100.0; 				// Velocity 
		Velocity.Z += 250;
		bBounce = True;
		PlaySound(SpawnSound);
	}
}

///////////////////////////////////////////////////////////
state AtRest
{
Begin:
	SetRotation(Rotation(Vect(0,1,0)));
	SetTimer(CountDown,False);     	
}

state Unfolding
{
	ignores Touch;
Begin:
	if (!bFireType) Explosion();
	PlayAnim('UnFold',0.3,0.05);
	FinishAnim();
	GoToState('Launching');
}

///////////////////////////////////////////////////////////
state Launching
{
	ignores Touch;
	function Timer()
	{
		local PeaceRocket r;
		local Vector X,Y,Z;

		GetAxes(Rotation,X,Y,Z);
		if (i == 1) {
			Rot.Yaw += 32768;	
			r = Spawn(class'PeaceRocket',, '',
				Location - 15 * X -  0 * Y - Z * 10);		
			r.SetRotation(Rot);
			Rot.Yaw -= 16384;	
			PlayAnim('Launch1',1.0, 0.05);		
		}
		else if (i == 2) {
			r = Spawn(class'PeaceRocket',, '', 
				Location +  0 * X + 15 * Y - Z * 10);	
			r.SetRotation(Rot);
			Rot.Yaw -= 32768;	
			PlayAnim('Launch2',1.0, 0.05);		
		}
		else if (i == 3) {
			r = Spawn(class'PeaceRocket',, '', 
				Location + 0 * X - 15 * Y - Z * 10);	
			r.SetRotation(Rot);
			Rot.Yaw += 16384;	
			PlayAnim('Launch3',1.0, 0.05);		
		}
		else if (i == 4) {
			r = Spawn(class'PeaceRocket',, '', 
				Location +  15 * X - 0 * Y - Z * 10);	
			r.SetRotation(Rot);	
			PlayAnim('Launch4',1.0, 0.05);		
		}	
		else if (i == 6) {
			Spawn(class'BallExplosion');	
			SetTimer(0,False);
			Destroy();
		}	
		i++;
	}

	function BeginState()
	{
		Local Vector X,Y,Z;

		GetAxes(Rotation,X,Y,Z);
		Rot = Rotation;
		Rot.Pitch += 6000;
		i=0;
		SetTimer(0.2,True);
	}
}

///////////////////////////////////////////////////////////
function Explosion()
{
	HurtRadius(damage, 200.0, 'exploded', MomentumTransfer, Location);		
	Spawn(class'BallExplosion');	
	Destroy();
}

