using UnityEngine;
using System.Collections;

public class core : MonoBehaviour {

	//LevelLimits
	public GameObject horizon;

	public float totalDensity = 0.0f;

	public int stage = 1;

	public float coreMaxDensity = 10.0f;

	public AudioClip explosion;
	public float changeto;
	public float barratio;
	public float levelprogress;
	float levelrequirement = 10f;
	public bool resetme =false;

	public int playerID = 0;
	public GameObject mePlaya;
	public GameObject winCondition;
	/*public void AddDensity(float given){
		totalDensity += given;
		Debug.Log("Total Density: " + totalDensity);
	}*/

	void nextLayer(){
		transform.localScale *= 1.25f;


	}

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {

		if (totalDensity > 100f) 
		{
			int playerID = mePlaya.GetComponent<AstroControls>().playerID;
			winCondition.SendMessage("gameEnds", playerID);
		}

		if (stage == 1) 
		{
			coreMaxDensity = 10f;
			renderer.material.color = new Color(0f,1f,163/255f);
		}

		if (stage == 2) 
		{
			coreMaxDensity = 30f;
			renderer.material.color = Color.yellow;
		}
		if (stage == 3) 
		{
			coreMaxDensity = 60f;
			renderer.material.color = Color.red;
		}
		if (stage == 4) 
		{
			coreMaxDensity = 100f;
			renderer.material.color = new Color (139f / 255, 69f / 255, 19f / 255);
		}

		barratio = levelprogress / levelrequirement;
		levelrequirement = stage*10f;
		
		if(totalDensity >= coreMaxDensity && stage<4){
			stage++;
			nextLayer();
			levelprogress = 0f;
			//coreMaxDensity = 10f + (2*stage-1)*10f;
			//levelrequirement = stage*10f;
			audio.PlayOneShot(explosion);
		}
	}

	void OnCollisionEnter(Collision col)
	{
		if (col.gameObject.name == "LeftWall") {
			transform.position += new Vector3(135.0f, 0.0f, 0.0f);		
		}
		if (col.gameObject.name == "RightWall") {
			transform.position -= new Vector3(135.0f, 0.0f, 0.0f);		
		}
		if (col.gameObject.name == "TopWall") {
			transform.position -= new Vector3(0.0f, 75.0f, 0.0f);		
		}
		if (col.gameObject.name == "BottomWall") {
			transform.position += new Vector3(0.0f, 75.0f, 0.0f);		
		}
		if (col.gameObject.name.StartsWith ("Meteor") && col.gameObject.GetComponent<particles>().taggy == stage && col.transform.parent == null) 
		{
			col.gameObject.transform.parent = this.gameObject.transform;

			col.rigidbody.velocity = Vector3.zero;
			col.rigidbody.isKinematic = true;
			totalDensity += col.gameObject.GetComponent<particles>().density;
			levelprogress += col.gameObject.GetComponent<particles>().density;

			col.gameObject.GetComponent<particles>().connected = true;
			col.gameObject.GetComponent<particles>().density = 0;

		}
	}
}
