var life:int;

var ball:Rigidbody;

var slowBall = new Hashtable();
var speedBall = new Hashtable();

function Start () {
	yield initializeLife();
}

function Update () {
}

function decreaseLife(ballObject){
	life--;
	if(life<1){
		destroyBrick(ballObject);
	}
}

function destroyBrick(ballObject){
	Destroy(this.gameObject);
	//Check if this brick has any power
	powerUps(ballObject);
	
	//Increment Score
	var menu:mainMenuController;
	menu = GameObject.FindGameObjectWithTag('menuSetting').GetComponent(mainMenuController);
	
	var player = menu.player;
	player.incrementScore();
	
	var cylinder:cylinderController;
	cylinder = GameObject.FindGameObjectWithTag('cylinder').GetComponent(cylinderController);
	
	cylinder.updateTotalBrickNumber();
}

function OnCollisionEnter(otherObject:Collision){
	if(otherObject.transform.tag=='ball'){
		if(otherObject.gameObject.renderer.material.color!=Color.red){//Normal Ball State
			decreaseLife(otherObject.gameObject);	
		}
		else{//Color is red, ball has more power
			destroyBrick(otherObject.gameObject);
		}
			
	}
}

function initializeLife(){
	if(this.gameObject.transform.tag=='rubberBrick'){
		life = 1;
	}
	else if(this.gameObject.transform.tag=='iceBrick'){
		life = 2;
	}
	else if(this.gameObject.transform.tag=='woodBrick'){
		life = 3;
	}
	else if(this.gameObject.transform.tag=='metalBrick'){
		life = 4;
	}
	
	yield;
}

function extraBall(){
	var ballg:Rigidbody;
	ballg = Instantiate(ball, this.gameObject.transform.position, Quaternion.identity);
	
	var menu:mainMenuController;
	menu = GameObject.FindGameObjectWithTag('menuSetting').GetComponent(mainMenuController);
	
	var player = menu.player;
	
	player.addBalls(ballg.gameObject);
}

function stickBall(ballObject){
	var cylinder:cylinderController;
	cylinder = GameObject.FindGameObjectWithTag('cylinder').GetComponent(cylinderController);
	cylinder.stickTheBall(ballObject);
}

function speedUpStick(){
	var cylinder:cylinderController;
	cylinder = GameObject.FindGameObjectWithTag('cylinder').GetComponent(cylinderController);
	cylinder.speedUpStick();
}

function slowDownStick(){
	var cylinder:cylinderController;
	cylinder = GameObject.FindGameObjectWithTag('cylinder').GetComponent(cylinderController);
	cylinder.slowDownStick();
}

function powerUps(ballObject){
	var cylinder:cylinderController;
	cylinder = GameObject.FindGameObjectWithTag('cylinder').GetComponent(cylinderController);
	
	var brick = Random.Range(1, cylinder.totalBricks);
	//We want that only 25% of bricks have powers
	var poweredBricks = (cylinder.totalBricks * 25)/100;
	
	if(brick<=poweredBricks){
		//This brick has a power
		//Find which one
		var power = Random.Range(1,5);
		switch(power){
			case 1:
				extraBall();
			break;
			case 2:
				stickBall(ballObject);
			break;
			case 3:
				speedUpStick();
			break;
			case 4:
				slowDownStick();
			break;
		}
	}
}