Ejemplo n.º 1
0
    //find objects in blast radius, apply damage and physics force, and remove mine object
    IEnumerator Detonate()
    {
        if (triggered)        //if player stepped on mine, play beep sound effect and detonate after damageDelay
        {
            if (beepFx)
            {
                GetComponent <AudioSource>().clip = beepFx;
                GetComponent <AudioSource>().Play();
            }
            yield return(new WaitForSeconds(damageDelay));
        }

        //play explosion effects
        WeaponEffectsComponent.ExplosionEffect(myTransform.position);

        myTransform.GetComponent <MeshRenderer>().enabled = false;
        GetComponent <AudioSource>().clip  = explosionFX;
        GetComponent <AudioSource>().pitch = Random.Range(0.75f * Time.timeScale, 1.0f * Time.timeScale);       //add slight random value to sound pitch for variety
        GetComponent <AudioSource>().Play();
        audioPlayed = true;

        if (!triggered)        //unless player stepped on mine, apply explosion damage and force after damageDelay
        {
            yield return(new WaitForSeconds(damageDelay));
        }

        //find surrounding objects to be damaged by explosion
        Collider[] hitColliders = Physics.OverlapSphere(myTransform.position, radius * 1.5f, blastMask);
        //apply damage and force to surrounding objects
        for (int i = 0; i < hitColliders.Length; i++)
        {
            Transform hitTransform = hitColliders[i].transform;
            if ((hitTransform != myTransform)           //do not call ApplyDamage on this mine object
                //don't damage or apply force to object if it is shielded/hidden from blast by other object
                && (Physics.Linecast(hitTransform.position, myTransform.position, out hit, blastMask) &&
                    hit.collider == myTransform.GetComponent <Collider>()))
            {
                //call ApplyDamage() function of objects in radius
                switch (hitColliders[i].GetComponent <Collider>().gameObject.layer)
                {
                case 0:                        //hit object is a breakable or explosive object
                    if (hitColliders[i].GetComponent <Collider>().gameObject.GetComponent <BreakableObject>())
                    {
                        hitColliders[i].GetComponent <Collider>().gameObject.GetComponent <BreakableObject>().ApplyDamage(explosionDamage);
                    }
                    else if (hitColliders[i].GetComponent <Collider>().gameObject.GetComponent <ExplosiveObject>())
                    {
                        hitColliders[i].GetComponent <Collider>().gameObject.GetComponent <ExplosiveObject>().ApplyDamage(explosionDamage);
                    }
                    else if (hitColliders[i].GetComponent <Collider>().gameObject.GetComponent <MineExplosion>())
                    {
                        hitColliders[i].GetComponent <Collider>().gameObject.GetComponent <MineExplosion>().ApplyDamage(explosionDamage);
                    }
                    else if (hitColliders[i].GetComponent <Collider>().gameObject.GetComponent <AppleFall>())
                    {
                        hitColliders[i].GetComponent <Collider>().gameObject.GetComponent <AppleFall>().ApplyDamage(explosionDamage);
                    }
                    break;

                case 11:                        //hit object is player
                    if (hitColliders[i].GetComponent <Collider>().gameObject.GetComponent <FPSPlayer>())
                    {
                        hitColliders[i].GetComponent <Collider>().gameObject.GetComponent <FPSPlayer>().ApplyDamage(explosionDamage);
                    }
                    break;

                case 13:                        //hit object is an NPC
                    if (hitColliders[i].GetComponent <Collider>().gameObject.GetComponent <CharacterDamage>())
                    {
                        hitColliders[i].GetComponent <Collider>().gameObject.GetComponent <CharacterDamage>().ApplyDamage(explosionDamage, Vector3.zero, myTransform.position, null, false, true);
                    }
                    if (hitColliders[i].GetComponent <Collider>().gameObject.GetComponent <LocationDamage>())
                    {
                        hitColliders[i].GetComponent <Collider>().gameObject.GetComponent <LocationDamage>().ApplyDamage(explosionDamage, Vector3.zero, myTransform.position, null, false, true);
                    }
                    break;

                default:
                    break;
                }

                //apply explosion force
                if (hitTransform.GetComponent <Rigidbody>())
                {
                    hitTransform.GetComponent <Rigidbody>().AddExplosionForce(blastForce * hitTransform.GetComponent <Rigidbody>().mass, myTransform.position, radius, 3.0F, ForceMode.Impulse);
                }
            }

            if (i < hitColliders.Length - 1)
            {
                continue;
            }
            else              //if all objects have been damaged by blast, disable collider (it was needed for the line cast above)
            {
                myTransform.GetComponent <BoxCollider>().enabled = false;
            }
        }
    }
Ejemplo n.º 2
0
    //find objects in blast radius, apply damage and physics force, and remove explosive object
    IEnumerator Detonate()
    {
        //play explosion effects and apply explosion damage and force after damageDelay
        yield return(new WaitForSeconds(damageDelay));

        //play explosion effects
        WeaponEffectsComponent.ExplosionEffect(myTransform.position);

        myTransform.GetComponent <MeshRenderer>().enabled = false;
        aSource.pitch = Random.Range(0.75f * Time.timeScale, 1.0f * Time.timeScale);        //add slight random value to sound pitch for variety
        aSource.Play();
        audioPlayed = true;

        //find surrounding objects to be damaged by explosion
        Collider[] hitColliders = Physics.OverlapSphere(myTransform.position, radius, blastMask, QueryTriggerInteraction.Ignore);
        //apply damage and force to surrounding objects
        for (int i = 0; i < hitColliders.Length; i++)
        {
            RaycastHit hit;
            //don't call ApplyDamage on this explosive object
            if (hitColliders[i].transform != myTransform
                //don't damage or apply force to object if it is shielded/hidden from blast by other object
                && !(Physics.Linecast(hitColliders[i].transform.position, myTransform.position, out hit, obstructionMask, QueryTriggerInteraction.Ignore)))
            {
                hitCollider        = hitColliders[i].GetComponent <Collider>();
                explosionDamageAmt = explosionDamage * Mathf.Clamp01((1.0f - (myTransform.position - hitColliders[i].transform.position).magnitude / radius));                //make damage decrease by distance from center
                if (explosionDamageAmt >= 1)
                {
                    //call ApplyDamage() function of objects in radius
                    switch (hitCollider.gameObject.layer)
                    {
                    case 13:                            //hit object is an NPC
                        if (hitCollider.gameObject.GetComponent <CharacterDamage>())
                        {
                            hitCollider.gameObject.GetComponent <CharacterDamage>().ApplyDamage(explosionDamageAmt, Vector3.zero, myTransform.position, myTransform, false, true);
                        }
                        if (hitCollider.gameObject.GetComponent <LocationDamage>())
                        {
                            hitCollider.gameObject.GetComponent <LocationDamage>().ApplyDamage(explosionDamageAmt, Vector3.zero, myTransform.position, myTransform, false, true);
                        }
                        break;

                    case 0:                            //hit object
                        if (hitCollider.gameObject.GetComponent <BreakableObject>())
                        {
                            hitCollider.gameObject.GetComponent <BreakableObject>().ApplyDamage(explosionDamageAmt);
                        }
                        else if (hitCollider.gameObject.GetComponent <ExplosiveObject>())
                        {
                            hitCollider.gameObject.GetComponent <ExplosiveObject>().ApplyDamage(explosionDamageAmt);
                        }
                        else if (hitCollider.gameObject.GetComponent <MineExplosion>())
                        {
                            hitCollider.gameObject.GetComponent <MineExplosion>().ApplyDamage(explosionDamageAmt);
                        }
                        else if (hitCollider.gameObject.GetComponent <AppleFall>())
                        {
                            hitCollider.gameObject.GetComponent <AppleFall>().ApplyDamage(explosionDamageAmt);
                        }
                        break;

                    case 11:                            //hit object is player
                        if (hitCollider.gameObject.GetComponent <FPSPlayer>())
                        {
                            hitCollider.gameObject.GetComponent <FPSPlayer>().ApplyDamage(explosionDamageAmt);
                        }
                        break;

                    default:
                        break;
                    }
                }
                //apply explosion force
                if (hitColliders[i].transform.GetComponent <Rigidbody>())
                {
                    hitRigidbody = hitColliders[i].transform.GetComponent <Rigidbody>();
                    hitRigidbody.AddExplosionForce(blastForce * hitRigidbody.mass, myTransform.position, radius, 3.0F, ForceMode.Impulse);
                }
            }

            if (i < hitColliders.Length - 1)
            {
                continue;
            }
            else              //if all objects have been damaged by blast, disable collider (it was needed for the line cast above)
            {
                if (objectPoolIndex == 0)
                {
                    myTransform.GetComponent <MeshCollider>().enabled = false;
                }
            }
        }
        StartCoroutine(DetectDestroyed());
    }