コード例 #1
0
 public void StartImpactFX(SO_ImpactFX sOImpactFX)
 {
     inUse         = true;
     sO_ImpactFX   = sOImpactFX;
     sprites       = sO_ImpactFX.impactFXSprites;
     spriteTimings = sO_ImpactFX.impactFXTimings;
     totalTicks    = spriteTimings.Length - 1;
     tick          = 0;
     timer         = 0f;
     this.gameObject.SetActive(true);
     StartCoroutine(PlayImpactFX());
 }
コード例 #2
0
    // public PolygonCollider2D[] attackColliders;
    // public bool[] activeAttacks;
    // public PolygonCollider2D attackCollider;
    // public List<Collider2D> collidersHit;
    // public List<Collider2D> collidersDamaged;

    public IEnumerator AttackCollider(bool attackFromWeaponOne, SO_Weapon sOWeapon, SO_Weapon.AttackChain WeapAtkChain, SO_AttackFX sO_AttackFX, PolygonCollider2D atkFXCol, Transform weapOrigTrans)
    {
        // References from the SO_AttackFX.
        float thisColStart = sO_AttackFX.colStart;
        float thisColEnd   = sO_AttackFX.colEnd;

        // Set chosen pool attack FX collider off in case the attack FX dosn't use a collider.
        atkFXCol.enabled = false;
        SO_ImpactFX sO_ImpactFX = sO_AttackFX.soImpactFX;

        // Apply the collision shape and offset position to the chosen pool collider.
        atkFXCol.points = sO_AttackFX.collider.points;
        atkFXCol.offset = sO_AttackFX.collider.offset;
        // Set other variables.
        List <Collider2D> collidersHit     = new List <Collider2D>();
        List <Collider2D> collidersDamaged = new List <Collider2D>();
        float             timer            = 0f;
        bool detectCol  = false;
        bool hitAnEnemy = false;

        // Wait a frame to avoid having the collider appear for a frame at its last position, the atkFX's new position HAS to be set before the collider is enabled. (Maybe specifically for atkFX's that have the same colStart time, first sprite time and moveDelay all set to the same value)
        yield return(null);

        while (timer < thisColEnd)
        {
            timer += Time.deltaTime;
            if (timer >= thisColStart && !detectCol)
            {
                yield return(null);

                // If the attack has a collider, enable it otherwise leave it off.
                if (sO_AttackFX.collider)
                {
                    atkFXCol.enabled = true;
                }
                detectCol = true;
                yield return(null);
            }
            if (detectCol)
            {
                // Detect hit collision.
                Physics2D.OverlapCollider(atkFXCol, hitLayers, collidersHit);
                foreach (Collider2D col in collidersHit)
                {
                    if (!collidersDamaged.Contains(col))
                    {
                        collidersDamaged.Add(col);
                        // If this is an enemy, apply damage.
                        if (col.gameObject.CompareTag("Enemy"))
                        {
                            col.GetComponent <Enemy_Health>().ReceiveDamage(WeapAtkChain.DamageRoll, charEquippedWeapons.activeWeapon.poiseDamage, atkFXCol.transform.position, col.bounds.center);
                            // If at least one enemy is hit apply durability damage to the active weapon.
                            if (!hitAnEnemy)
                            {
                                hitAnEnemy = true;
                                if (sOWeapon.durabilityDamageOnHit)
                                {
                                    charEquippedWeapons.DurabilityDamage(attackFromWeaponOne);
                                }
                            }
                        }
                        else if (col.gameObject.CompareTag("Destructible"))
                        {
                            col.GetComponent <Clutter_Health>().ReceiveDamage(WeapAtkChain.DamageRoll, atkFXCol.transform.position, col.bounds.center);
                        }
                        // Hit impact FX. Apply the correct rotation, position, sprites and layerMask to an impactFX.
                        HitImpact.PlayImpactFX(atkFXCol, col.bounds.center, sO_ImpactFX, hitLayers.layerMask, col);
                        // Set camera nudge backwards from the hit.
                        CameraFollow.CameraNudge_St(weapOrigTrans.up, nudgeStrength);
                        // Slow time. Duration to be set by weapon damage, slow to be adjusted (animation curve) by TimeSlow script.
                        TimeSlow.StartTimeSlow(timeSlowDur);
                        //Debug.Log("Collider: " + col.gameObject.name + " was hit! Hit hit, hurraay!");
                    }
                }
            }
            yield return(null);
        }
        if (collidersHit.Count > 0)
        {
            collidersHit.Clear();
            collidersDamaged.Clear();
        }
        atkFXCol.enabled = false;
        yield return(null);
    }
コード例 #3
0
    public static void PlayImpactFX(Collider2D hittingCollider, Vector2 receivingColliderPos, SO_ImpactFX sOImpactFX, LayerMask hitLayerMask, Collider2D receivingCollider, bool projectileImpact = false)
    {
        // Get the hitting colldier's postion.
        Vector2 hittingColliderPos = (Vector2)hittingCollider.transform.position;

        // Request an ImpactFX script(attached to a GameObject) from an ImpactFX pool.
        impactFX_St = impactFXPool_St.RequestImpactFX();
        // Calculate the direction from the hitting colldier to the receiving collider.
        Vector2 dirToEnemy = (Vector2)receivingCollider.bounds.center - hittingColliderPos;

        //Vector2 dirToEnemy = (Vector2)receivingCollider.transform.position - hittingColliderPos;
        // Does this impact keep the attack/projectile's direction.
        if (sOImpactFX.keepDirection)
        {
            impactFX_St.transform.up = hittingCollider.transform.up;
        }
        else
        {
            // Calculate and apply the direction from the hittingCollider to the receivingCollider.
            impactFX_St.transform.up = dirToEnemy;
        }
        // Either apply the impact effect at the position of the hitting collider or fire a raycast to get the more appropriate position. This was mainly done for projectiles hitting tilemap walls.
        if (projectileImpact)
        {
            impactFX_St.transform.position = new Vector3(hittingCollider.transform.position.x, hittingCollider.transform.position.y, impactFX_St.transform.position.z);
        }
        else
        {
            // Fire a raycast from the hittingCollider to the receivingCollider to get a "hit" position, in order to place the impactFX close the the receiver.
            Debug.DrawRay(hittingColliderPos, dirToEnemy, Color.white, 1f);
            foreach (RaycastHit2D hit in Physics2D.RaycastAll(hittingColliderPos, dirToEnemy, dirToEnemy.magnitude, hitLayerMask))
            {
                if (hit.collider == receivingCollider)
                {
                    impactFX_St.transform.position = new Vector3(hit.point.x, hit.point.y, impactFX_St.transform.position.z);
                    break;
                }
            }
        }
        // Start the impactFX sprite animation.
        impactFX_St.StartImpactFX(sOImpactFX);
    }