using UnityEngine;
using System.Collections;

public class MoreProjectilesOnDeathComponent : MonoBehaviour
{
    private TM_Projectile myProjectile;
    public GameObject Prefab_SmallerProjectilesToFire;
    public Vector2 MinMax_NumProjectilesToFire;
    private ClearEnemyProjectilesRing groundExplosion;

    void Awake()
    {
        myProjectile = GetComponent<TM_Projectile>();
        groundExplosion = GetComponentInChildren<ClearEnemyProjectilesRing>();
    }
	// Use this for initialization
	void Start () 
    {
	    myProjectile.AddOnDestroyFunction(OnProjectileDestroyed);
	}
	
	// Update is called once per frame
	void Update () 
    {
	
	}

    public void OnProjectileDestroyed()
    {
        int iNumToFire = (int)Random.Range(MinMax_NumProjectilesToFire.x, MinMax_NumProjectilesToFire.y + 1.0f);
        float fRotAmount = 360.0f / iNumToFire;
        float fRotBase = Random.Range(0f,360.0f);

        for (int t = 0; t < iNumToFire; t++)
        {
            TM_Projectile p = GameObject.Instantiate(Prefab_SmallerProjectilesToFire).GetComponent<TM_Projectile>();
            p.transform.position = transform.position;
            p.transform.rotation = Quaternion.Euler(0, 0, fRotBase + fRotAmount * t);
            p.vDirection = p.transform.up;
            p.fSpeed = Random.Range(p.MinMax_ProjectileSpeed.x, p.MinMax_ProjectileSpeed.y);
            p.iFakeAltitude = myProjectile.iFakeAltitude;
            p.gameObject.SetActive(true);
        }

        if (groundExplosion != null)
        {
            groundExplosion.transform.parent = null;
            groundExplosion.ExpandRing();
            Destroy(groundExplosion.gameObject, 3.0f);
        }
    }
}
