using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour 
{
	float				m_ShotCooldownTime	= 0.016667f * 5.0f;
	Vector3 			m_vVel 				= new Vector3();
	GameController 		m_GameController 	= null;
	Vector3 			m_vMouseInWorld 	= new Vector3();
	Vector3 			m_vFacing 			= new Vector3();
	float 				m_fRot    			= 0.0f;
	GameObject			m_Turret			= null;		
	GameObject			m_Shield			= null;
	float				m_fShotCooldown		= 0.0f;
	float				m_fInvulnTimer		= 0.0f;
	bool				m_bIsDead			= false;
	GameObject			m_Kaboom			= null;
	
	void Start () 
	{
		m_vFacing.z = 1.0f;
		m_Turret    = Util.FindChild(this.gameObject, "Turret");
		m_Shield    = Util.FindChild(this.gameObject, "Shield");
		m_Kaboom    = Util.FindChild(this.gameObject, "Kaboom");
		m_fInvulnTimer = 3.0f;
	}
	
	
	public void Init( GameController gc )
	{
		m_GameController = gc;
		m_fInvulnTimer = 3.0f;
	}
	
	
	void Update () 
	{		
		float fTimeStep = Time.deltaTime;
		
		
		if (!m_bIsDead)
		{
		
			if ( m_fInvulnTimer>0.0f )
			{
				m_fInvulnTimer-= Time.deltaTime;
							
				float fFade = Mathf.Min ( 1.0f, m_fInvulnTimer );
				float fPulse = 0.7f + 0.3f*Mathf.Sin( m_fInvulnTimer*12.0f );
				
				Color c = m_Shield.renderer.material.color;
				c.a = fFade * fPulse * 0.35f;
				m_Shield.renderer.material.color = c;
				m_Shield.transform.Rotate(0.0f, Time.deltaTime*90.0f, 0.0f );
				
				if ( m_fInvulnTimer<0.0f )
				{
					m_Shield.SetActive(false);
				}
			}
			
			if (m_GameController!=null)
			{
				GameObject cam = m_GameController.GetCamera();
				
				Vector3 vMouse = Input.mousePosition;
				Ray r = cam.camera.ScreenPointToRay( vMouse );
				
				RaycastHit hitInfo;
				
				if ( Physics.Raycast(r, out hitInfo, 2500.0f ))
				{
					m_vMouseInWorld = hitInfo.point;	
					
					Vector3 vTarget = m_vMouseInWorld-transform.position;
					vTarget.y=0.0f;
					vTarget.Normalize();
					
					m_vFacing = Vector3.Lerp( m_vFacing, vTarget, fTimeStep*25.0f );
					m_vFacing.Normalize(); 
					
					m_fRot = Mathf.Atan2( m_vFacing.x, m_vFacing.z );
					
					Vector3 vAngles = new Vector3( 0.0f, m_fRot*Mathf.Rad2Deg, 0.0f );
					Quaternion q = Quaternion.Euler( vAngles );
					m_Turret.transform.localRotation = q;
					
					
					if ( Input.GetMouseButton(0) )
					{
						if ( m_fShotCooldown<=0.0f )
						{
							GameObject bullet = Instantiate( m_GameController.GetBullet() ) as GameObject;
							bullet.transform.parent = m_GameController.GetActiveLevel().transform;
							bullet.transform.localPosition = transform.localPosition;
							bullet.transform.localRotation = m_Turret.transform.localRotation;					
							
							m_fShotCooldown += m_ShotCooldownTime;
						}
					}
				}
			}
			
			
			if ( m_fShotCooldown>0.0f )
			{
				m_fShotCooldown-=Time.deltaTime;
			}
		}
		
	}
	
	
	public void Dead()
	{
		if ( !m_bIsDead && m_fInvulnTimer<=0.0f )
		{
			m_bIsDead = true;
			rigidbody.velocity = Vector3.zero;
			collider.enabled = false;
			if ( m_Turret!=null ) m_Turret.SetActive( false );
			m_Kaboom.SetActive( true );
		}
	}
	
	public bool IsDead()
	{
		return m_bIsDead;
	}
	
	
	void FixedUpdate()
	{				
		
		if (!m_bIsDead)
		{
		
			float fFwd = 0.0f;
			float fLR  = 0.0f;
			
			if ( Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow   ))  fFwd += 1.0f;
			if ( Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow ))  fFwd -= 1.0f;
			if ( Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow ))  fLR  -= 1.0f;
			if ( Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))  fLR  += 1.0f;
					
			float fAcc = 300.0f;
			float fDec = 10.0f;		
			Vector3 vForce = new Vector3();
					
			// Damping
			float fTimeStep = Time.fixedDeltaTime;
			m_vVel = rigidbody.velocity;		
			m_vVel *= (1.0f-(fTimeStep*fDec));
			rigidbody.velocity = m_vVel;
			
			// Acceleration
			vForce += transform.forward * fFwd * fAcc;
			vForce += transform.right   * fLR  * fAcc;
			rigidbody.AddForce( vForce, ForceMode.Acceleration );
		}
		
	}
	
}
