/* 	
============================================================================================================
	 _	 _  ____  _      _  ____  _____  _____  _______  ____  _____
	| | | || ___|| |    | || ___||  _  || ___ ||__   __|| ___|| ___ |
	| |_| || |__ | |    | || |   | | | || |_| |   | |   | |__ | |_| |
	|  _  || ___|| |    | || |   | | | || ____|   | |   | ___|| __  |
	| | | || |__ | |___ | || |__ | |_| || |       | |   | |__ | | \ \
	|_| |_||____||_____||_||____||_____||_|       |_|   |____||_|  \_\
	
		 _______  _   _  _______  _____  _____   _  _____  _
		|__   __|| | | ||__   __||  _  || ___ | | ||  _  || |
		   | |   | | | |   | |   | | | || |_| | | || |_| || |
		   | |   | | | |   | |   | | | ||  _  | | ||  _  || |
		   | |   | |_| |   | |   | |_| || | \ \ | || | | || |___
		   |_|   |_____|   |_|   |_____||_|  \_\|_||_| |_||_____|
	   
				   	 _________________________
					|_____by: Andrew Gotow____|
			
============================================================================================================

	This is a simple script that is attached to the player object to make a simple gun. It uses a raycast to draw an imaginary
line forward, and uses that data to determine the object it hit. In this case, it simply instantiates a prefab object (a bullet 
impact effect) at the point of contact.
*/

var		gun_Emitter_Object		: GameObject;	// the object the script uses to determine the gun orientation (also has audio 
												// and particle emitters attached
var		weapon_Fire_Delay 		: float = 0.0;	// the delay between shots (in seconds)
var 	bullet_Impact_Prefab 	: GameObject;	// the effect created when a "bullet" hits the ground

private var weapon_Fire_Timer : float = 0.0;	// a counter to time up to the weapon fire delay.

function Update () {
	gun_Emitter_Object.particleEmitter.emit = false;	// turn off the particle emitter attached to the gun...
	
	if ( Input.GetButton( "Fire1" ) && weapon_Fire_Timer >= weapon_Fire_Delay ) {	// if the fire key is down, and the fire timer
																					// is greater than the fire delay, then
		weapon_Fire_Timer = 0.0;	// set the fire timer to 0 so we can begin counting again.
		gun_Emitter_Object.audio.Play();	// play the sound attached to the gun object.
		gun_Emitter_Object.particleEmitter.emit = true;		// and turn on it's particle emitter.
		
		
		// now we perform the raycast. The variable "hit" is defined as a "Raycast hit". When you perform a raycast with a raycast hit
		// as one of the variables, it is automatically set up with all of the results from the raycast, including distance, object hit,
		// normal of the surface hit, ect.
		var hit : RaycastHit;
		
		// now if the raycast from the gun object returns true (if we hit something)
		if ( Physics.Raycast( gun_Emitter_Object.transform.position, gun_Emitter_Object.transform.forward, hit ) ) {
			// instantiate the bullet impact prefab so that it is pointing at the normal direction of the surface, and is at the hit 
			// location
			Instantiate( bullet_Impact_Prefab, hit.point, Quaternion.LookRotation( hit.normal ) );
		}
	}
	
	// and last, increase the fire timer.
	weapon_Fire_Timer += Time.deltaTime;
}