Example #1
0
    void OnTriggerEnter(Collider other)
    {
        Debug.Log(other.name + "Just entered!");
        var explodable = other.GetComponent <IExplodeable>();

        if (explodable == null)
        {
            return;
        }
        Vector3 vec = other.transform.position - transform.position;

        explodable.Explode(ExplosionUtils.ExplosiveForce(vec, vec.magnitude, GameSettings.Instance.ExplosionRadius));
    }
Example #2
0
    IEnumerator ExplosionSequence(float countdownTime)
    {
        // show countdown for non trivial explosion timings
        if (countdownTime >= GameSettings.Instance.MinDelayForCountdownPopup)
        {
            createCountdownText(countdownTime).transform.SetParent(transform, false);
        }

        yield return(new WaitForSeconds(countdownTime));

        LevelSingleton.Instance.OnBarrelExplosion.Invoke(transform.position);
        int     explosionBlockersLayer = 1 << LayerMask.NameToLayer("ExplosionBlockers"); // check only explosion blockers (walls etc.).  We don't want barrels to block other barrels.
        int     explodeablesLayer      = 1 << LayerMask.NameToLayer("Explodeables");
        var     radius     = MyExplosionRadius();
        Vector3 myCenter   = GetObjectCenter();
        var     collisions = Physics.OverlapSphere(myCenter, radius, explodeablesLayer); // find all nearby barrels (only)

        foreach (Collider collision in collisions)
        {
            // If it's not something that can explode, we don't care.
            var explodeable = collision.GetComponent <IExplodeable>();
            if (explodeable == null)
            {
                continue;
            }


            Vector3 displacement = explodeable.GetObjectCenter() - myCenter;
            float   distance     = displacement.magnitude;
            // If there's a wall in the way, or if we're checking out ourselves, we also don't care.
            RaycastHit hit;
            if (displacement.sqrMagnitude < 0.01f || Physics.Raycast(myCenter, displacement, out hit, distance, explosionBlockersLayer))
            {
                continue;
            }

            explodeable.Explode(ExplosionUtils.ExplosiveForce(displacement, distance, radius));
        }
        if (ExplosionPrefab != null)
        {
            GameObject.Instantiate(ExplosionPrefab.gameObject, gameObject.transform.position, LevelSingleton.Instance.GroundRotator.transform.rotation, LevelSingleton.Instance.GroundRotator.transform);
        }

        Destroy(this.gameObject);
    }