
//@script ExecuteInEditMode()

var timerText:GUIText;
var messageText:GUIText;



static var playerLives: int=3;

static var playerScore: int;
static var HiScore: int=100000;
static var stageClearFlag:boolean=false;

var  mainskin : GUISkin; 
static var   stageNum:int=1;
private var pauseFlag:boolean=false;
private var mainCamera:cameraController;
private var mainPlayer:playerScript;
private var bossAwake:boolean;

var optionState:int; 
var optionCursor:GUITexture;

var se01:AudioClip;

static var optionHit:boolean;

static var HScore:int[];
static var Name:String[];



function Awake(){


         ScoreScript.InitScore();
	//	 ScoreScript.PrintScore();

         messageText.text="";
         
		

}
function Start(){


    mainCamera=GameObject.Find("Main Camera").GetComponent("cameraController") ;
	
	mainPlayer=GameObject.Find("player").GetComponent("playerScript") ;
	               
    var bossTransform:Transform = GameObject.Find("enemyBoss").transform;
	bossTransform.position =Vector3(100,5,0);
		
    bossAwake=false;
    stageClearFlag=false;
	
    optionState=0; 
    optionHit=false;
	optionCursor.pixelInset.x = -1000;
	
    startInitialize();






}

function Update () {


	timerText.text="" +  Time.timeSinceLevelLoad;


	if(( Time.timeSinceLevelLoad>=95)&&(bossAwake==false)){

	bossAwake=true;
	var  bossActive: enemyController=  GameObject.Find("enemyBoss").GetComponent("enemyController") ;

	bossActive.enemyActive =true;
	  
				mainCamera.speed=0.0;

}


if( Time.timeSinceLevelLoad>= 160){

	stageClearFlag=true;


}



if(stageClearFlag){

	stageClear();
 


}




if(optionHit==true){
  optionState++;
	if(optionState>5){optionState=0;} 
	if(optionState==0){optionCursor.pixelInset.x = -1000;}
	else{optionCursor.pixelInset.x =148.0+optionState*140;}
	optionHit=false;
}

if( Input.GetKeyDown("x") && optionState>0 ){
      
        audio.clip = se01;
        audio.Play();
     mainPlayer.powerUpActive(optionState);
     optionState=0;
	 optionCursor.pixelInset.x = -1000;
}






if(playerLives <= -100){

  GameOverMode();

}

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

  PauseMode();


}
}



function  startInitialize(){


        messageText.text="";
		yield WaitForSeconds(0.5);
        messageText.text="STAGE "+stageNum;
		yield WaitForSeconds(0.5);
		messageText.text="";
		yield WaitForSeconds(0.5);
		messageText.text="STAGE "+stageNum;
		yield WaitForSeconds(0.5);
		messageText.text="";
		yield WaitForSeconds(0.5);
		messageText.text="STAGE "+stageNum;
		yield WaitForSeconds(0.5);
		messageText.text="";


}
function PauseMode(){
              pauseFlag=!pauseFlag;
              
			  if(pauseFlag){
			    messageText.text="PAUSE";
               Time.timeScale = 0.0;
			   AudioListener.pause = true;
              
				}else{
                 messageText.text="";
               Time.timeScale = 1.0;
				AudioListener.pause = false;
			    }
				
}
function stageClear(){


	 messageText.text="STAGE CLEAR ";
	 
	 yield WaitForSeconds(3.0);
	 
	 stageNum++;
	  mainCamera.speed=1.0;
	  
	  if(stageNum>=3){GameOverMode();}
	 else{Application.LoadLevel("level01");}
	 



}


function ContinueMode(){
           var leftTime:int=10;
              Time.timeScale = 0.0;
               
			   while(leftTime>0){
               messageText.text="CONTINUE "+ leftTime.ToString;
                if(Input.anyKeyDown){
				messageText.text="";
				 playerLives=3;
				 break;
				}
     			
				}
				GameOverMode();

}
function GameOverMode(){

                messageText.text="GAME OVER";
				yield WaitForSeconds(3.0);
				stageNum=1;
				playerLives=3;
				playerScore=0;
				Application.LoadLevel("ranking");
                
              
}

function OnGUI(){

	GUI.skin =  mainskin; 

	GUI.Label(Rect(10,Screen.height-70,200,50), "Score: " + playerScore);
	GUI.Label(Rect(Screen.width/2,Screen.height-30,400,50), "HiScore: " + HiScore);
	GUI.Label(Rect(10,Screen.height-50, 200, 50), "Lives: " + playerLives);


}
