//FPS Constructor - Weapons
//Copyright Dastardly Banana Productions 2010
//This script, and all others contained within the Dastardly Banana Weapons Package, may not be shared or redistributed. They can be used in games, either commerical or non-commercial, as long as Dastardly Banana Productions is attributed in the credits.
//Permissions beyond the scope of this license may be available at mailto://info@dastardlybanana.com.


var length1 : float;  
var width1 : float;
private var textu : Texture;
private var lineStyle : GUIStyle;
var debug : boolean = false;

var crosshairTexture : Texture2D;
var friendTexture : Texture2D;
var foeTexture : Texture2D;
var otherTexture : Texture2D;

var crosshairRange : int = 200;

private var crosshair : boolean = true;

function Start () {
	crosshair = true;
	lineStyle = GUIStyle();
	lineStyle.normal.background = crosshairTexture;
}

//Right now this script fires a raycast every frame
//This might impact performance, and is an area to consider when optimizing
function Update(){
	var hit : RaycastHit;
	var direction = transform.TransformDirection(Vector3(0,0,1));
	if(Physics.Raycast(transform.position, direction, hit, crosshairRange)){
		if(hit.collider && hit.transform.gameObject.GetComponent(CrosshairColor) != null){
			var colorScript : CrosshairColor = hit.transform.gameObject.GetComponent(CrosshairColor);
			if(colorScript.crosshairType == crosshairTypes.Friend){
				ChangeColor("Friend");
			}else if(colorScript.crosshairType == crosshairTypes.Foe){
				ChangeColor("Foe");
			}else if(colorScript.crosshairType == crosshairTypes.Other){
				ChangeColor("Other");
			}
		}else{
			ChangeColor(""); //Any string not recognized by ChangeColor is the default color
		}
	}else{
		ChangeColor("");
	}
}

function OnGUI () {
	distance1 = gunscript.crosshairSpread;
	if(!(distance1 > (Screen.height/2)) &&(crosshair || debug)){
	
		GUI.Box(Rect((Screen.width - distance1)/2 - length1, (Screen.height - width1)/2, length1, width1), textu, lineStyle);
		GUI.Box(Rect((Screen.width + distance1)/2, (Screen.height- width1)/2, length1, width1), textu, lineStyle);
	
		GUI.Box(Rect((Screen.width - width1)/2, (Screen.height - distance1)/2 - length1, width1, length1), textu, lineStyle);
		GUI.Box(Rect((Screen.width - width1)/2, (Screen.height + distance1)/2, width1, length1), textu, lineStyle);
	}
}

function ChangeColor(targetStatus : String){
	if(targetStatus == "Friend"){
		lineStyle.normal.background = friendTexture;
	}else if(targetStatus == "Foe"){
		lineStyle.normal.background = foeTexture;
	}else if (targetStatus == "Other"){
		lineStyle.normal.background = otherTexture;
	}else{
		lineStyle.normal.background = crosshairTexture;
	}
}

function aiming(){
	crosshair=false;
}
function normalSpeed(){
	crosshair=true;
}
function sprinting(){
	crosshair=false;
}
