//=============================================================================
//
//	missile_seeking.txt
//
//	This is the script for a seeking missile. It constantly faces and moves
//	towards its target. However, its Y coordinate never changes, allowing
//	the missile to be ducked/jumped over.
//
//	For documentation on scripting, please consult the Wrack Wiki at:
//	http://wrack.wikia.com/
//
//=============================================================================

#include "commondefines.h"

//== STATE PROTOTYPES =========================================================

state	Spawn;
state	MoveTowardsTarget;
state	Move;
state	Explode;

//== VARIABLES ================================================================

int	g_iStruckObject;

//== EVENTS ===================================================================

event_instant( "struck wall" )
{
	// Spawn our decal (if we have one), then explode.
	SpawnImpactDecal( GetStringProperty( "decal" ));

	g_iStruckObject = 0;
	ChangeState( Explode );
}

//-----------------------------------------------------------------------------
event_instant( "struck floor" )
{
	// Spawn our decal (if we have one), then explode.
	SpawnImpactDecal( GetStringProperty( "decal" ));

	g_iStruckObject = 0;
	ChangeState( Explode );
}

//-----------------------------------------------------------------------------
event_instant( "player revived" )
	DestroySelf( );

//-----------------------------------------------------------------------------
event_instant( "player continued" )
	DestroySelf( );

//-----------------------------------------------------------------------------
event_instant( "struck object" )
{
	// If we're a ripping projectile, damage the object.
	if ( RipStruckObject( ))
	{
		// If we didn't kill the object, explode.	
		if ( DamageStruckObject( GetIntProperty( "damage" ), GetIntProperty( "knockback" ), 0 ) == false )
			ChangeState( Explode );
	}
	else
	{
		g_iStruckObject = 1;
		ChangeState( Explode );
	}
}

//== STATES ===================================================================

state Spawn
{
	MainLoop
	{
		// Play our traveling sound.
		PlaySound( "missile_seeking_travel.wav", 25, SFXF_LOOP, SFXPRIORITY_HIGH );

		// Set our spinning animation.
		SetAnimation( "spin", -1, ANIMF_LOOP | ANIMF_RANDOMSTARTTIME );

		// Move towards our target.
		changestate( MoveTowardsTarget );
	}
}

//-----------------------------------------------------------------------------
state MoveTowardsTarget
{
	event( "target died" )
		changestate( Move );

	MainLoop
	{
		// If we're above/under the target, stop trying to follow it
		// and just travel straight. The target successfully evaded
		// the attack.
		if ( InTargetXZSpace( ))
			changestate( Move );

		// Turn towards our target.
		TurnTowardsTarget( ANGLE_1 / 2, false );

		// Update our velocity that that we're always travelling
		// towards our target.
		UpdateVelocity( GetIntProperty( "speed" ), false );
	}
}

//-----------------------------------------------------------------------------
state Move
{
	MainLoop
	{
	}
}

//-----------------------------------------------------------------------------
state Explode
{
	OnEnter
	{
		// Damage the object.
		if ( g_iStruckObject )
			DamageStruckObject( GetIntProperty( "damage" ), GetIntProperty( "knockback" ), 0 );

		// Play our impact sound.
		PlaySound( "missile_impact.wav", 100, SFXF_STOPEXISTING_ALL, SFXPRIORITY_LOWEST );

		// Create a particle effect where the projectile struck.
		if ( InFrontOfPlayer( ))
			DoExplosionParticles( GetStringProperty( "projectile" ), 32 );

		// Stop rendering and doing collision.
		AddFlag( FLAG_DONTRENDER );
		AddFlag( FLAG_NOCOLLISION );
		AddFlag( FLAG_NOCOLLISIONAGAINST );

		// Stop moving.
		SetVelocity( 0, 0, 0 );
		AddFlag( FLAG_NOGRAVITY );

		// If this is a finisher missile, end the player's chain.
		if ( GetFlag( FLAG_FINISHERMISSILE ))
		{
			ClearFlag( FLAG_FINISHERMISSILE );
			EndKillChain( );
		}
	}

	MainLoop
	{
		// When the explosion sound is done playing, destroy ourself.
		delay( GetSoundLength( "missile_impact.wav" ));
		DestroySelf( );
	}
}
