#pragma strict

private var CamTarget : Vector3;
private var PlayerCmpt : Player;
private var camRot : Quaternion;

function UpdateCamera()
{
	var mainCamTF = Camera.main.transform;
	var hitinfo : RaycastHit;
	var srcpos = transform.position;
	var tgtdir : Vector3 = Vector3(0,4,-4);
	var topdist = 4;
	
	var State = PlayerCmpt.State;
	var WallHugDir = PlayerCmpt.WallHugDir;
	
	if( State == EPlayerState.WallHug )
	{
		tgtdir = WallHugDir * 2 + Vector3(0,1,0);
		topdist = 2;
	}
	if( Physics.Raycast( Ray( srcpos, tgtdir.normalized ), hitinfo, tgtdir.magnitude ) )
	{
		var tophit = Physics.Raycast( srcpos, Vector3(0,1,0), topdist );
		tgtdir = Vector3.Lerp( tgtdir, tgtdir * hitinfo.distance / tgtdir.magnitude, 1 - hitinfo.distance / tgtdir.magnitude + (tophit?0.5:0) );
		if( !tophit )
			tgtdir.y = topdist;
	}
	mainCamTF.position = Vector3.Lerp(mainCamTF.position, transform.position + tgtdir, Time.deltaTime * 10);
	//Vector3.MoveTowards( mainCamTF.position, transform.position + tgtdir, Time.deltaTime * 12 );
	CamTarget = transform.position + ( State == EPlayerState.WallHug ? Vector3.up * 0.6 : Vector3.zero );
	//Vector3.MoveTowards( CamTarget, transform.position + ( State == EPlayerState.WallHug ? Vector3.up * 0.6 : Vector3.zero ), Time.deltaTime * 12 );
	
	//mainCamTF.LookAt( CamTarget );
	// Smooth rotation
	camRot = Quaternion.Lerp(camRot,Quaternion.LookRotation(CamTarget-mainCamTF.position,Vector3.up),10*Time.deltaTime);
	mainCamTF.rotation = camRot;
}

function Start()
{
	PlayerCmpt = GetComponent(Player);
	CamTarget = transform.position;
	PlayerCmpt.C_HitTimeout = 0.9;
}

function Update()
{
	if (Input.GetKeyDown("enter") || Input.GetKeyDown("return"))
	{ Application.LoadLevel(Application.loadedLevelName); }
	
	PlayerCmpt.I_MoveDir = Vector2( Input.GetAxis("Horizontal"), Input.GetAxis("Vertical") );
	PlayerCmpt.I_Fire = Input.GetAxis("Fire1") > 0.5;
	if( PlayerCmpt.State == EPlayerState.Shooting )
	{
		for( var obj : GameObject in GameObject.FindGameObjectsWithTag( "Enemy" ) )
		{
			var pos : Vector3 = obj.transform.position;
			var mtx : Matrix4x4 = Matrix4x4.TRS( transform.position, transform.rotation, Vector3(1,1,1) ).inverse;
			var localPos : Vector3 = mtx.MultiplyPoint( pos );
			localPos.x /= localPos.z * 0.1;
			localPos.y /= localPos.z * 0.1;
			var q : float = 0.5;
			var see : boolean =
				localPos.x > -q && localPos.x < q &&
				localPos.y > -q && localPos.y < q &&
				localPos.z > 0.01 && localPos.z < 10
			;
			
			if( see )
			{
				var rcd = pos - transform.position;
				see = !Physics.Raycast( transform.position, rcd.normalized, rcd.magnitude );
				if( see )
				{
					var OtherPlayerCmpt = obj.GetComponent(Player);
					OtherPlayerCmpt.BulletHit();
				}
			}
		}
	}
}

function LateUpdate()
{
	UpdateCamera();
}

function OnGUI()
{
	var W = Screen.width;
	var H = Screen.height;
	var min_size = Mathf.Min( W, H );
	var unit_big = min_size / 10;
	var unit_small = min_size / 50;
	var unit_xsm = min_size / 200;
	
	if( PlayerCmpt.Health <= 0 )
	{
		GUI.color = Color(0.8,0.2,0,Mathf.Sin(Time.time*Mathf.PI*2)*0.2+0.8);
		PlayerCmpt.AlertGUIStyle.fontSize = unit_big;
		PlayerCmpt.AlertGUIStyle.normal.textColor = GUI.color;
		GUI.Label( new Rect( unit_big, unit_big, W - unit_big * 2, H - unit_big * 2 ), "Press Enter to restart", PlayerCmpt.AlertGUIStyle );
		GUI.color = Color(1,1,1);
	}
}

