using UnityEngine;
using System.Collections;

public class BallControl : MonoBehaviour 
{
	public float explosionPower = 300f;
	public float explosionRadius = 50f;
	public float limitTime = 5f;
	public GameObject particlePrefab;
	
	IEnumerator Start ()
	{
		yield return new WaitForSeconds(limitTime);
		Destroy(gameObject);
	}
	
	void OnCollisionEnter(Collision collision)
	{
		if(collision.rigidbody != null)
			Explosion();
	}
	
	void Explosion()
	{
		Vector3 explosionPos = transform.position;
		Collider[] colliders = Physics.OverlapSphere(explosionPos, explosionRadius);
		foreach(Collider hit in colliders)
		{
			if(hit != null && hit.rigidbody != null)
				hit.rigidbody.AddExplosionForce(explosionPower, explosionPos, explosionRadius, 3.0f);
		}
		
		Instantiate(particlePrefab, transform.position, Quaternion.identity);
		Destroy(gameObject);
	}
}