Exemple #1
0
    /// <summary>
    /// Mark true if colision with rocket caused the explosion. Damage will be caused if marked false.
    /// </summary>
    /// <param name="fromRocket"></param>
    /// <returns></returns>
    public IEnumerator Explode(bool fromRocket)
    {
        if (!exploded)
        {
            exploded = true;
            GetComponent <Image>().color = new Color(0, 0, 0, 0);
            int meteoriteNumber = Random.Range(1, meteoritePrefabs.Length);
            explosion = Instantiate(explosion, transform.position, new Quaternion(), transform.parent);
            explosion.transform.localScale = transform.localScale;
            for (int i = 0; i <= meteoriteNumber; i++)
            {
                GameObject meteorite = Instantiate(meteoritePrefabs[i], transform.position, new Quaternion(), transform.parent);

                //add metorites to list to keep track of to destroy later
                meteorites.Add(meteorite);
                float   x         = Random.Range(-1f, 1f);
                float   y         = Random.Range(-1f, 1f);
                Vector2 direction = new Vector2(x, y) * meteoriteForce;
                meteorite.GetComponent <Rigidbody2D>().AddForce(direction, ForceMode2D.Impulse);
            }
            if (!fromRocket)
            {
                miniGameManager.TakeDamage();
                StartCoroutine(FlashDamageIndicator());
                while (flashingIndicator)
                {
                    yield return(null);
                }
            }
            else
            {
                if (miniGameManager.requiredAstroids > 0)
                {
                    miniGameManager.requiredAstroids--;
                }
                yield return(new WaitForSeconds(1f));
            }
            foreach (GameObject meteorite in meteorites)
            {
                Destroy(meteorite);
            }
            Destroy(this);
        }
    }