using UnityEngine;
using System.Collections;

public class Meteor2Behaviour : MonoBehaviour {
	private const float SPEED = 7.0f;
	// Use this for initialization
	private bool exploded;
	private 
	void Start () {
		exploded = false;
	}
	
	// Update is called once per frame
	void Update () {
		if (!exploded) {
			float delta = Time.deltaTime * SPEED;
			Vector3 vec = new Vector3 (0, -delta, 0);
			vec = Vector3.ClampMagnitude (vec, delta);
//		vec = vec * Referee.LevelSpeed;
			this.transform.Translate (vec);
		}
	}
	void OnCollisionEnter2D(Collision2D coll) {
		if (coll.gameObject.tag != "Enemy") {
			exploded = true;
			StartCoroutine(splashing());

			Debug.Log(coll.gameObject.name);
		}
	}

	IEnumerator splashing() {
		this.gameObject.transform.rotation = Quaternion.Euler (Vector3.zero);
		//if this.gameObject.name comincia con meteor vai in crossfade con un'animazione, altrimenti con l'altra!
		Animator anim = GetComponent<Animator>();
		if(this.gameObject.name.Substring(0, 2) == "Me") {
			anim.CrossFade("MeteorSplash", 0f);
		}
		else {
			anim.CrossFade("Shock", 0f);
		}
		yield return new WaitForSeconds(0.2f);
		Debug.Log ("collisione");
		Destroy (this.gameObject);
	}

	void OnCollisionStay2D(Collision2D coll) {
		if (coll.gameObject.tag != "Enemy") {
			exploded = true;
			StartCoroutine(splashing());
		}
		
	}

}
