


var playerSpeed : float;


var bullet: Rigidbody;
var missile:Rigidbody;
var twin  :Rigidbody;
var laser  :Rigidbody;
var option:GameObject;

var explosion: Transform;

public var optionFlag:boolean=true;

static var playerConstSpeed:float;

private var cameraController:cameraController;

private var missileFlag:boolean;
private var doubleFlag:boolean;
private var laserFlag:boolean;
private var PlayerOption:int;


private var target:Transform;
function Start(){

      
     
     cameraController= gameObject.Find("Main Camera").GetComponent("cameraController");
	 target= gameObject.Find("player").transform;
	 
	  missileFlag=false;
      doubleFlag=false;
      laserFlag=false;
      PlayerOption=0;	 
	 
	 
	 
     
}

function Update () {
playerConstSpeed=cameraController.speed;
transform.position.x += Time.deltaTime*playerConstSpeed;

	HMove = (playerSpeed * Input.GetAxis("Horizontal"))  * Time.deltaTime;
	VMove = (playerSpeed * Input.GetAxis("Vertical"))  * Time.deltaTime;




   var newpos=Vector3(transform.position.x+HMove,transform.position.y+VMove,transform.position.z);
   var viewportPos:Vector2=  Camera.main.WorldToViewportPoint(newpos);
	
	
    if( 0.025>viewportPos.x || viewportPos.x >0.975){
         HMove=0;
    }
	
	if( 0.05>viewportPos.y || viewportPos.y >0.95 ){
          VMove=0;
    }

   
   transform.Translate(HMove, VMove,0, Space.World);
  
   
  
  

     
	
if( Input.GetKeyDown("z") ){ 

    if(!laserFlag){

		var tempBullet: Rigidbody;
	
		tempBullet = Instantiate(bullet, transform.position, transform.rotation);
	
	
	
		if(optionFlag==true){
				var gos : GameObject[];
				gos = GameObject.FindGameObjectsWithTag("option"); 
    
				for (var go : GameObject in gos)  { 
				Instantiate(bullet, go.transform.position, go.transform.rotation);
				}
	
	
	
		}	
	
	}
////////////////////////////////////////////////////////////////////////////////////	
  if(doubleFlag){

		var tempDouble: Rigidbody;
	
		tempDouble = Instantiate(twin, transform.position, transform.rotation);
	
	
	
		if(optionFlag==true){
				var gos3 : GameObject[];
				gos3 = GameObject.FindGameObjectsWithTag("option"); 
    
				for (var go : GameObject in gos3)  { 
				Instantiate(twin, go.transform.position, go.transform.rotation);
				}
	
	
	
		}	
	
	}
////////////////////////////////////////////////////////////////////////////////////	
	
	if(laserFlag){
	var tempLaser: Rigidbody;
	
	tempLaser = Instantiate(laser, transform.position, transform.rotation);
	
	
	
		if(optionFlag==true){
				var gos2: GameObject[];
	           gos2= GameObject.FindGameObjectsWithTag("option"); 
    
				for (var go : GameObject in gos2)  { 
				Instantiate(laser, go.transform.position, go.transform.rotation);
			}
		}
	}
///////////////////////////////////////////////////////////////////////////////////	
	
	
	if(missileFlag){
		var tempMissile: Rigidbody;
	
		tempMissile = Instantiate(missile, transform.position, transform.rotation);
				if(optionFlag==true){
					var gos1 : GameObject[];
					gos1 = GameObject.FindGameObjectsWithTag("option"); 
    
					for (var go : GameObject in gos1)  { 
					Instantiate(missile, go.transform.position, go.transform.rotation);
					}
	

				}
	}
	
	
	

}



}






function  powerUpActive(optionState:int){

     switch(optionState){
	 
	 
	 case   1: playerSpeed *= 1.2;  break;
	 
	 case   2: missileFlag=true;  break;
	 
	 case   3: doubleFlag=true;  break;
	 
	 case   4: laserFlag=true; doubleFlag=false; break;
	 
	 case   5: if(PlayerOption<4) {PlayerOption++; AddOption(); }
	 
	 print("PlayerOption="+PlayerOption);
	 break;
	 
	 
	 }



}

function AddOption(){

var target:Transform;
     if(PlayerOption>0){
	 
	 var gos:GameObject[];
	   gos=GameObject.FindGameObjectsWithTag("option");
       	   for (var go : GameObject in gos)  { 
		   
		   
				if	(go.GetComponent("optionController").index==PlayerOption-1){
				
                     target=go.transform;				
				}
		           
		   
		   }
	 
	 
	 }else if (PlayerOption==0){target=this.transform;}

	 var tempOption: GameObject;
	
	
//	var tempPosition = Vector3(transform.position.x-1.0,transform.position.y,transform.position.z);
	var tempPosition = Vector3(transform.position.x-1.0,transform.position.y,transform.position.z);
	
	tempOption = Instantiate(option, tempPosition, transform.rotation);
	tempOption.GetComponent("optionController").target = target;
	tempOption.GetComponent("optionController").index = PlayerOption;




}

function ApplyItem(itemType:int){
 
    GameController.optionHit=true;     


  
  

}

function OnTriggerEnter(otherObject: Collider){

	if(otherObject.gameObject.tag == "enemy"){
	
		
		
		var tempExplosion: Transform;
		
		tempExplosion = Instantiate(explosion, transform.position, transform.rotation);
	
		GameController.playerLives --;
		
		
		
	}else if(otherObject.gameObject.tag == "BG"){
      
	  var tempExplosion1: Transform;
		
		tempExplosion1 = Instantiate(explosion, transform.position, transform.rotation);
	
		GameController.playerLives --;

        
    }


}



