Beispiel #1
0
 private void Awake()
 {
     self         = GetComponentInParent <IDamageableObject>();
     obj          = transform.parent.parent;
     animator     = obj.GetComponent <Animator>();
     thisCollider = GetComponent <BoxCollider2D>();
 }
Beispiel #2
0
    private void OnTriggerEnter(Collider other)
    {
        IDamageableObject victim = other.gameObject.GetComponent <IDamageableObject>();

        if (victim != null)
        {
            victim.TakeDamage(this.damageOutput);
            Destroy(this.gameObject);
        }
    }
Beispiel #3
0
    private void AttemptKnockback(IDamageableObject victim)
    {
        float knockbackRoll = Random.Range(0f, 1f);

        if (knockbackRoll < this.controllingCreature.activeWeapon.knockbackChance)
        {
            Vector3 knockbackDirection = this.controllingCreature.transform.up;

            victim.Knockback(knockbackDirection, this.controllingCreature.activeWeapon.knockbackDistance);
        }
    }
Beispiel #4
0
    private void OnTriggerEnter(Collider other)
    {
        IDamageableObject victim = other.gameObject.GetComponent <IDamageableObject>();

        if (victim != null)
        {
            float damageOutput = this.controllingCreature.activeWeapon.GetRandomRangeValue();

            victim.TakeDamage(damageOutput, false, this.controllingCreature.activeWeapon);
            this.AttemptKnockback(victim);
        }
    }
Beispiel #5
0
    private void OnTriggerEnter2D(Collider2D collision)
    {
        IDamageableObject other = collision.GetComponent <IDamageableObject>();
        bool isKnockedBacked    = animator.GetBool("Knockedbacked");

        if (other != null && !isKnockedBacked)
        {
            // Setting up the knockback direction & force
            float   directionX     = (obj.localScale.x > 0) ? 1 : -1;
            Vector2 knockbackForce = new Vector2(5 * directionX, Mathf.Abs(5));

            // Find if there is a block collider on the collided object.
            // For example: If the enemy hits the player, we check if the player has a block collider.
            // If it exists, we check if the enemy's hit collider is touching the player's block collider.
            BoxCollider2D blockCollider = collision.transform.Find("Colliders") ? collision.transform.Find("Colliders").Find("Block").GetComponent <BoxCollider2D>() : null;

            if (blockCollider != null)
            {
                bool isColliderInBlockCollider = Physics2D.IsTouching(thisCollider, blockCollider);

                if (isColliderInBlockCollider)
                {
                    self.Knockback(knockbackForce * -1);
                    other.Knockback(knockbackForce / 4);
                    return;
                }
            }

            if (other.BloodSpray != null)
            {
                GameObject blood = Instantiate <GameObject>(other.BloodSpray, collision.transform.position, Quaternion.identity, Game.gameHolder);
                blood.transform.position = blood.transform.position + new Vector3(directionX, 0, 0);
                ParticleSystem ps = blood.GetComponent <ParticleSystem>();
                ParticleSystem.VelocityOverLifetimeModule vel = ps.velocityOverLifetime;
                ParticleSystem.MinMaxCurve curve = new ParticleSystem.MinMaxCurve(vel.x.constantMin * directionX, vel.x.constantMax * directionX);
                vel.x = curve;

                Destroy(blood, 5);
            }
            other.TakeDamage(10);
            other.Knockback(knockbackForce);
        }
    }
Beispiel #6
0
    private void OnTriggerStay2D(Collider2D collision)
    {
        if (Main.EditorMode)
        {
            return;
        }

        if (collision.CompareTag("Player"))
        {
            IDamageableObject damageableObject = collision.GetComponent <IDamageableObject>();

            if (damageableObject != null)
            {
                damageableObject.TakeDamage(damageData.damage);
                float directionX = (collision.transform.localScale.x > 0) ? -1 : 1;
                float directionY = (int)transform.up.y == 0 ? 1 : transform.up.y;
                //print(directionY);

                Vector2 knockbackForce = new Vector2(damageData.knockbackForce * directionX, damageData.knockbackForce * directionY);
                damageableObject.Knockback(knockbackForce);
            }
        }
    }
Beispiel #7
0
    private void OnTriggerStay2D(Collider2D collision)
    {
        if (collided)
        {
            return;
        }

        foreach (string tag in enemyList)
        {
            if (collision.gameObject.CompareTag(tag))
            {
                collided = true;

                IDamageableObject damageableObject = collision.GetComponentInParent <IDamageableObject>();

                if (damageableObject != null)
                {
                    Vector2 knockbackForce;

                    // A local scale z of 0 means it's shot by an object which flips when the object moves. [See LevelController.CreateProjectileTowardsDirection]
                    if (transform.localScale.z == 0)
                    {
                        float directionX = (transform.localScale.x > 0) ? 1 : -1;
                        knockbackForce = new Vector2(projectileData.knockbackForce * directionX, Mathf.Abs(projectileData.knockbackForce));
                    }
                    else
                    {
                        // Use transform.right to knock player towards facing of projectile
                        // Use transform.up to knock player towards direction of projectile

                        knockbackForce = transform.up * projectileData.knockbackForce;
                    }

                    // If the object is blocking, we decrease the force.
                    // We need to check if the projectile is in the blocking collider.
                    if (collision.name == "Block")
                    {
                        damageableObject.Knockback(knockbackForce / 4);
                    }
                    else
                    {
                        if (damage != 0)
                        {
                            damageableObject.TakeDamage(damage);
                        }
                        else
                        {
                            damageableObject.TakeDamage(projectileData.damage);
                        }

                        damageableObject.Knockback(knockbackForce);
                    }
                }

                Destroy(gameObject);

                /*if (enemy) {
                 *  enemy.TakeDamage(projectileData.damage);
                 *  float directionX = (transform.localScale.x > 0) ? 1 : -1;
                 *  Vector2 knockbackForce = new Vector2(projectileData.knockbackForce * directionX, Mathf.Abs(projectileData.knockbackForce));
                 *  enemy.Knockback(knockbackForce);
                 * } else if (player) {
                 *  player.TakeDamage(projectileData.damage);
                 *  float directionX = (transform.localScale.x > 0) ? 1 : -1;
                 *  Vector2 knockbackForce = new Vector2(projectileData.knockbackForce * directionX, Mathf.Abs(projectileData.knockbackForce));
                 *  player.Knockback(knockbackForce);
                 * }*/
            }
        }
    }