Beispiel #1
0
        /// <summary>
        /// Calculates the effects of the explosion as damage and force applied to nearby objects.
        /// </summary>
        /// <param name="radius">The blast radius.</param>
        /// <param name="force">How strong the objects will be pushed if within the blast radius.</param>
        /// <param name="damage">The damage dealt to objects if within the blast radius.</param>
        /// <param name="position">The world space position of the explosion.</param>
        /// <param name="ignoreCover">Should the explosion ignore objects within the radius that may block its effects?</param>
        public static void CalculateExplosionDamage(float radius, float force, float damage, Vector3 position, bool ignoreCover = false)
        {
            // Array of colliders near of the player.
            Collider[] m_HitColliders = new Collider[128];

            // Amount of objects near the explosion.
            int amount = Physics.OverlapSphereNonAlloc(position, radius, m_HitColliders);

            if (amount == 0)
            {
                return;
            }

            for (int i = 0; i < amount; i++)
            {
                Collider c = m_HitColliders[i];

                if (c.gameObject.isStatic)
                {
                    continue;
                }

                Vector3 pos       = c.transform.position;
                Vector3 direction = (pos - position).normalized;
                float   intensity = (radius - Vector3.Distance(position, pos)) / radius;

                IStunnable stunnableTarget = c.GetComponent <IStunnable>();
                stunnableTarget?.DeafnessEffect(intensity);

                if (!TargetInSight(position, c, ignoreCover))
                {
                    continue;
                }

                IExplosionDamageable damageableTarget = c.GetComponent <IExplosionDamageable>();
                damageableTarget?.ExplosionDamage(intensity * damage, position, hitInfo.point);

                Rigidbody rigidBody = c.GetComponent <Rigidbody>();
                if (rigidBody && !c.CompareTag("Player"))
                {
                    if (!rigidBody.isKinematic)
                    {
                        // Apply force to all rigidBody hit by explosion (except the player).
                        rigidBody.AddForce(direction * (force * intensity), ForceMode.Impulse);
                    }
                }
            }
        }
    public override void UseActive(Vector3 position)
    {
        if (enemyLayer == 0)
        {
            enemyLayer = LayerMask.GetMask("Enemy");
        }
        int nTurrets = Physics.OverlapSphereNonAlloc(position, activeRange, colsCache, enemyLayer);

        for (int i = 0; i < nTurrets; i++)
        {
            IStunnable enemy = colsCache[i].GetComponent <IStunnable>();
            if (enemy != null)
            {
                enemy.Stun(stunTime);
            }
        }
    }
Beispiel #3
0
    protected override void OnCollisionEnter2D(Collision2D other)
    {
        base.OnCollisionEnter2D(other);

        IStunnable toStun    = other.gameObject.GetComponent <IStunnable>();
        bool       stunOther = toStun != null && _stunTags.Contains(other.gameObject.tag);

        if (stunOther)
        {
            toStun.GetStunned(_stunDuration);
        }

        if (!other.collider.isTrigger)
        {
            Destroy(this.gameObject);
        }
    }
Beispiel #4
0
    IEnumerator StunSphereAnimation()
    {
        AudioManager.Instance.PlaySound(stunClip, gameObject);
        Effects.instance.ShakeCameraRelative(0.3f, 0.3f);

        Vector3 targetPos = transform.position;

        targetPos.y -= yOffset;
        stunSphere.transform.position = targetPos;

        Collider[] enemies = Physics.OverlapSphere(transform.position, range, LayerMask.GetMask("Enemy"));

        foreach (Collider enemy in enemies)
        {
            IStunnable stunnable = enemy.GetComponent <IStunnable>();

            if (stunnable != null)
            {
                stunnable.Stun(stunDuration);
            }
        }


        float sinIN = 0;

        while (sinIN < 1)
        {
            sinIN += 0.01F;

            float opacity    = OpacityCurve.Evaluate(sinIN);
            float distortion = DistortionCurve.Evaluate(sinIN);
            float size       = SizeCurve.Evaluate(sinIN) * range;

            stunSphereRenderer.material.SetFloat("_Opacity", opacity);
            stunSphereRenderer.material.SetFloat("_DistortionAmt", distortion * 5);
            stunSphere.transform.localScale = new Vector3(size, size, size);

            yield return(new WaitForSeconds(0.01F));
        }

        stunSphereRenderer.material.SetFloat("_Opacity", 0);
        stunSphereRenderer.material.SetFloat("_DistortionAmt", 0);
        stunSphere.transform.localScale = Vector3.zero;
    }
    IEnumerator Stun(float visualsTimer, float stunTime)
    {
        //Visuals or particleFX;


        //Actual fireball: make a 2D circlecast and damage all IDamageables
        List <Transform> enemyUnits = targetManager.FindAllTargetsWithinRadius(gameObject.transform, thisPlayer, attackRadius);

        foreach (Transform unit in enemyUnits)
        {
            IStunnable behaviourScript = unit.GetComponent <IStunnable>();
            if (behaviourScript != null)
            {
                behaviourScript.Stun(stunTime);
            }
        }

        GameObject parent = transform.parent.gameObject;

        yield return(new WaitForSeconds(visualsTimer));

        Destroy(parent);
    }
Beispiel #6
0
 void OnTriggerStay(Collider other)
 {
     if (!hasTractor && other.gameObject.GetComponent <EnemyHealthManager>() != null && canDamage)
     {
         other.gameObject.GetComponent <EnemyHealthManager>().Hit(hit);
         GameObject myEffect = (GameObject)Instantiate(hitEffect, other.transform.position, Quaternion.identity);
         Destroy(myEffect, 5f);
         canDamage = false;
         damageTickTimer.Go(damageTickTime);
     }
     else if (hasTractor && other.gameObject.CompareTag("Fodder"))
     {
         if (!hasTractoredEnemy)
         {
             tractorDistance = Vector3.Distance(transform.position, other.transform.position);
             tractorTarget   = other.gameObject;
         }
         hasTractoredEnemy = true;
         tractorTarget.gameObject.transform.position = new Vector3(transform.position.x, transform.position.y + tractorDistance, 0);
         IStunnable stunnableTarget = tractorTarget.GetComponent(typeof(IStunnable)) as IStunnable;
         stunnableTarget.Stun();
     }
 }
Beispiel #7
0
    public void ForcePulseExplosion()
    {
        Instantiate(forcePulseWave, transform.position, Quaternion.identity, ParticleGenerator.holder);
        Vector2 point  = transform.position;
        int     layers = (1 << LayerSolid) | (1 << LayerProjectile);

        Collider2D[]       colliders = Physics2D.OverlapCircleAll(point, explosionRadius, layers);
        List <Rigidbody2D> rbs       = new List <Rigidbody2D>();

        for (int i = 0; i < colliders.Length; i++)
        {
            Collider2D col = colliders[i];
            if (col.attachedRigidbody.bodyType == RigidbodyType2D.Static)
            {
                continue;
            }
            bool found = false;
            for (int j = 0; j < rbs.Count; j++)
            {
                Rigidbody2D colRb = rbs[j];
                if (colRb == col.attachedRigidbody)
                {
                    found = true;
                    break;
                }
            }
            if (!found)
            {
                rbs.Add(col.attachedRigidbody);
            }
        }

        for (int i = 0; i < rbs.Count; i++)
        {
            Rigidbody2D colRb     = rbs[i];
            IStunnable  stunnable = colRb.GetComponent <IStunnable>();
            stunnable = stunnable ?? colRb.GetComponentInChildren <IStunnable>();
            stunnable?.Stun();

            if (colRb == rb)
            {
                continue;
            }
            Vector2 dir      = ((Vector2)colRb.transform.position - point).normalized;
            float   distance = Vector2.Distance(point, colRb.transform.position);
            if (distance >= explosionRadius)
            {
                continue;
            }
            colRb.velocity += dir * Mathf.Pow((explosionRadius - distance) / explosionRadius, 0.5f)
                              * explosionStrength;
            colRb.AddTorque((UnityEngine.Random.value > 0.5 ? 1f : -1f) * explosionStrength * 5f);
        }

        Vector2 screenPos = Camera.main.WorldToViewportPoint(transform.position);

        if (screenPos.x > -0.5f || screenPos.x < 1.5f || screenPos.y > -0.5f || screenPos.y < 1.5f)
        {
            screenRippleSO.StartRipple(this, distortionLevel: 0.03f,
                                       position: screenPos);
        }
    }
 internal void StartStun(IStunnable stunnable, float stunTime)
 {
     StartCoroutine(StartUnstunDelay(stunnable, stunTime));
 }
    private IEnumerator StartUnstunDelay(IStunnable stunnable, float stunTime)
    {
        yield return(new WaitForSeconds(stunTime));

        stunnable?.UnStun();
    }