//var MovFactor : float = 5;
var ColorInactive : Color = Color.green;
var ColorActive : Color = Color.red;
var VisibilityDist : float = 100;

var MaxTextureSize : int = 32;
var MinTextureSize : int = 16;

var DetectLayers : String[];
var IgnoreLayers : String[];

//private var FPC : GameObject;
private var PixelInsetX : float = 0;
private var PixelInsetY : float = 0;


function Start(){
	AdjustPosition();
}

// Situo la mira justo en el centro de la pantalla.
function AdjustPosition(){
	//FPC = GameObject.Find("First Person Controller");
	PixelInsetX = (Screen.width - this.guiTexture.pixelInset.width) / 2;
	PixelInsetY = (Screen.height - this.guiTexture.pixelInset.height) / 2;
	this.guiTexture.pixelInset.x = PixelInsetX;
	this.guiTexture.pixelInset.y = PixelInsetY;
}

// Compruebo a que esoy mirando, para cambiar el color de la mira si es un enemigo.
function Update(){
	var hit : RaycastHit;
	var fwd : Vector3 = Camera.main.transform.TransformDirection(Vector3.forward);
	var layerMask : int = 1 << 9;
	layerMask = ~layerMask;
	
	this.guiTexture.color = ColorInactive;
    if (Physics.Raycast (Camera.main.transform.position, fwd, hit, VisibilityDist, layerMask)){
	
		for(var MyTag : String in IgnoreLayers){
			if(hit.transform.tag == MyTag){
				this.guiTexture.pixelInset.width = MinTextureSize;
				this.guiTexture.pixelInset.height = MinTextureSize;
				return;
			}
		}
		
		var distance : float = Vector3.Distance(Camera.main.transform.position, hit.point);
		var SizeAimTexture : float = MaxTextureSize - (distance * MaxTextureSize / VisibilityDist);
		if(SizeAimTexture < MinTextureSize)
			SizeAimTexture = MinTextureSize;
		this.guiTexture.pixelInset.width = SizeAimTexture;
		this.guiTexture.pixelInset.height = SizeAimTexture;
		
		for(var MyTag : String in DetectLayers){
			if(hit.transform.tag == MyTag)
				this.guiTexture.color = ColorActive;
		}
	}
	else{
		this.guiTexture.pixelInset.width = MinTextureSize;
		this.guiTexture.pixelInset.height = MinTextureSize;
	}
	AdjustPosition();
}
