コード例 #1
0
    private IEnumerator DeathCoroutine()
    {
        //print(gameObject + " " + gameObject.GetComponent<NetworkIdentity>().netId  + " started death coroutine ");
        //only run on server
        //handle score
        if (lastColliderTag.CompareTo("Player Weapon") == 0 ||
            lastColliderTag.CompareTo("Player Missile Detector") == 0)   //easy fix for now
        {
            EnemyScoreInfo scoreInfo = GetComponent <EnemyScoreInfo>();
            if (scoreInfo == null)
            {
                print("Score info is null for object " + name);
            }
            else
            {
                int amount = scoreInfo.score;
                networkManager.AddScore(amount);
            }
        }

        //Play explosion, if there are any
        if (explosionEffectList.Length > 0)
        {
            Pools.Initialize(
                explosionEffectList[Random.Range(0, explosionEffectList.Length)],
                transform.position, Quaternion.identity);
        }
        yield return(null);

        //Play death sound, if there are any
        if (explosionSoundList.Length > 0)
        {
            GameObject audio = Pools.Initialize(
                explosionSoundList[Random.Range(0, explosionSoundList.Length)],
                transform.position, Quaternion.identity);
            audio.GetComponent <AudioSource>().Play();
        }

        //If the player died, notifiy the network manager
        if (tag.CompareTo("Player") == 0)
        {
            networkManager.PlayerKilled();
        }

        //stop if calculations aren't done
        while (!dropCalculationDone)
        {
            yield return(null);
        }

        //drop everything
        for (int index = 0; index < dropList.Count; index++)
        {
            Vector3 spawnLocation = new Vector3(
                Random.Range(-0.4f, 0.4f) + transform.position.x,
                transform.position.y,
                Random.Range(-0.4f, 0.4f) + transform.position.z
                );
            yield return(null);

            GameObject dropSpawned = dropList[index];
            GameObject newObj      = Pools.Initialize(dropSpawned, spawnLocation, transform.rotation);
            //DO NOT YIELD HERE, NEED TO INITIALIZE STRAIGHT MOVER

            //If object is a straight mover, then make sure that it goes in a random direction
            ObjectStraightMover straightMover = newObj.GetComponent <ObjectStraightMover>();
            if (straightMover != null)
            {
                straightMover.wasDropped = true;
            }
            yield return(null);
        }

        //finally kill object
        Pools.Terminate(gameObject);
    }