using UnityEngine;
using System.Collections;

// Applies an explosion force to all nearby rigidbodies
public class explode : MonoBehaviour {
	public float explosion_delay = 1f;
	public float explosion_rate = 1f;
	public float explosion_max_size = 10f;
	public float explosion_speed = 1f;
	public float current_radius;
    public float explosion_multi = 300f;
	public GameObject self;

	bool exploded = false;
	CircleCollider2D explosion_radius;

	void Start(){
		explosion_radius = gameObject.GetComponent<CircleCollider2D>();
		//self = GetComponent<GameObject> ();
	}

	void Update(){
		/*
		explosion_delay -= Time.deltaTime;
		if (explosion_delay < 0) {
			exploded = true;
		}*/
	}


	void FixedUpdate(){
		if (exploded == true) {
			if (current_radius < explosion_max_size)
			{
				current_radius += explosion_rate;
			}
			else
			{
				//Destroy(self);
				//Object.Destroy(this.gameObject, 1);//.parent.gameObject);
			}

			explosion_radius.radius = current_radius;
		}
	}

	void OnTriggerEnter2D(Collider2D col){
		if (col.tag == "surface") {
			exploded = true;
		}
		if (exploded == true) {
			if (col.gameObject.GetComponent<Rigidbody2D>() != null)
			{
				Vector2	target = col.gameObject.transform.position;
				Vector2 bomb = gameObject.transform.position;

				Vector2 direction = 140f * (target - bomb);

				col.gameObject.GetComponent<Rigidbody2D>().AddForce(direction.normalized * explosion_multi);
				//
			}
			Object.Destroy(this.gameObject, 0.1f);
		
		}


	}
}