using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class DropOnDestroy : MonoBehaviour {

	public List<Transform> Drops;	
	public float Spread=0.75f;
    public bool Rotate = false;

	private GameController gc;	

	void Start () {
		GameObject go = GameObject.Find ("GameController");
		gc = go.GetComponent<GameController>();
	}

	void OnDestroy() {
		if(!gc.IsGameShuttingDown && gc.isGameRunning())
			Drop();
	}

	private void Drop()
	{
		if(gc.isGameRunning())
		{
			foreach(Transform p in Drops)
			{
				Transform d = (Transform) GameObject.Instantiate(p);
				d.position = GetRandomPosition() + transform.position;
                if(Rotate)
                    d.localEulerAngles = new Vector3(0, 0, Random.Range(0f, 360f));
			}
		}
	}

	private Vector3 GetRandomPosition()
	{
		return new Vector3(Random.Range(-1.0f, 1.0f), Random.Range(-1.0f, 1.0f)).normalized * Spread;
	}

}
