// Use this for initialization
 void Start()
 {
     myExplodingParent = transform.parent.GetComponent <ExplodingObject>();
     CalculateStartPos();
     CalculateDistanceToCenter();
     FindDirectionToCenter();
     Find_OPP_DirectionToCenter();
     CalculateEndPos();
 }
Exemplo n.º 2
0
    /// <summary>
    /// Explode this object. Typically called when struck by a projectile, but
    /// it could be called by something else. Some ideas include remote or
    /// proximity mines.
    /// </summary>
    public void explode()
    {
        exploding = true;
        // Damage things that were hit
        Collider[] hitObjects = Physics.OverlapSphere(transform.position, explosionRadius, hitMask);
        foreach (Collider hit in hitObjects)
        {
            if (hit.gameObject == gameObject)
            {
                continue;
            }
            float randomizedDamage = explosionDamage * Random.Range(0.5f, 2f);
            int   layerHit         = hit.gameObject.layer;
            if (layerHit == PLAYER)    // Player takes extra damage. It's really easy to profit with the dumb AI. Might change it back if the smart AI doesn't clump up.
            {
                player.takeDamage(randomizedDamage * 2);
            }

            // Damage enemies, stun them, and make them flop around
            if (layerHit == ENEMY)
            {
                EnemyHealth eh = hit.gameObject.GetComponentInChildren <EnemyHealth>();
                if (eh != null)
                {
                    eh.addImpact(hit.gameObject.transform.position - transform.position, randomizedDamage * 2f);
                    eh?.takeDamage(randomizedDamage);
                    eh?.showDamageExplosion(null, 0f);
                    eh?.stun(3f);
                }
            }
            if (layerHit == DEBT_COLLECTOR)
            {
                DebtCollectorMovement dcm = hit.gameObject.GetComponentInChildren <DebtCollectorMovement>() ?? hit.gameObject.GetComponentInParent <DebtCollectorMovement>();
                dcm?.externalStun(Random.Range(3f, 8f));
            }

            // Chain explosions
            if (layerHit == EXPLOSIVE)
            {
                ExplodingObject boom = hit.gameObject.GetComponentInChildren <ExplodingObject>() ?? hit.gameObject.GetComponentInParent <ExplodingObject>();
                if (boom != null && !boom.exploding)
                {
                    boom.explode();
                }
            }
        }

        // Hide game object, can't destroy it just yet
        foreach (Renderer r in meshes)
        {
            r.enabled = false;
        }
        foreach (Collider c in colliders)
        {
            c.enabled = false;
        }

        Destroy(Instantiate(explosionEffect, transform.position, transform.rotation), 2f);

        // Show a light
        GameObject light = new GameObject();

        light.transform.position = transform.position;
        Light l = light.AddComponent <Light>();

        l.color     = new Color(1f, 0.29f, 0, 0.5f);
        l.intensity = 5f;
        l.range     = explosionRadius * 2;
        l.type      = LightType.Point;
        Destroy(light, 1f);

        // Play sound
        aso.PlayOneShot(explosionNoise, Settings.volume);
        Destroy(gameObject, 2f);
    }