Example #1
0
    public bool TakeDamage(IAttackable attacker, int damage)
    {
        if (gameObject == null)
        {
            return(false);
        }

        if (Stats.IsDead)
        {
            return(false);
        }

        if (!(attacker is PlayerController player))
        {
            return(false);
        }

        if (attackers.Count == 0)
        {
            Target = player.transform;
            transform.LookAt(player.transform);
        }

        attackers[attacker.Name] = attacker;

        if (!damageCounterManager)
        {
            damageCounterManager = GameObject.FindObjectOfType <DamageCounterManager>();
        }

        InCombat = true;

        damageCounterManager.Add(transform, damage);
        Stats.Health.Add(-damage);

        attackerAggro.TryGetValue(attacker.Name, out var aggro);
        var totalAggro = aggro + damage;

        attackerAggro[attacker.Name] = totalAggro;

        if (highestAttackerAggroValue < totalAggro)
        {
            highestAttackerAggroValue = totalAggro;
            if (attacker.Name != Target?.name)
            {
                Target = attacker.Transform;
            }
        }

        if (healthBar)
        {
            healthBar.UpdateHealth();
        }

        if (!Stats.IsDead)
        {
            // when an enemy has been ignored for some time, it will lose its interest
            // to attack its target. this is to avoid having them forever following
            // a player that has stopped training combat.
            noDamageDropTargetTimer = 0f;
            return(false);
        }

        noDamageDropTargetTimer = -1f;
        InCombat = false;
        highestAttackerAggroValue = 0;
        Target = null;
        Unlock();

        if (animations)
        {
            animations.Die();
        }

        StartCoroutine(Respawn());
        return(true);
    }
Example #2
0
    public bool TakeDamage(IAttackable attacker, int damage)
    {
        if (this == null || !this || gameObject == null || !gameObject)
        {
            return(false);
        }

        if (attacker == null)
        {
            return(false);
        }

        if (Stats.IsDead)
        {
            return(false);
        }

        if (!(attacker is PlayerController player))
        {
            return(false);
        }

        if (!player)
        {
            return(false);
        }

        try
        {
            if (attackers.Count == 0)
            {
                Target = player.transform;
                transform.LookAt(player.transform);
            }

            var attackerName = attacker.Name;

            attackers[attackerName] = attacker;

            if (!damageCounterManager)
            {
                damageCounterManager = GameObject.FindObjectOfType <DamageCounterManager>();
            }

            InCombat = true;

            var dc = damageCounterManager.Add(transform, damage);
            //dc.Color = player.PlayerNameHexColor;

            Stats.Health.Add(-damage);

            attackerAggro.TryGetValue(attackerName, out var aggro);

            var aggroMultiplier = 1f;
            var sword           = player.Inventory.GetMeleeWeapon();
            if (sword != null && sword.Type == RavenNest.Models.ItemType.OneHandedSword)
            {
                aggroMultiplier = 2.5f;
            }

            var totalAggro = aggro + (damage * aggroMultiplier);

            attackerAggro[attackerName] = totalAggro;

            if (highestAttackerAggroValue <= totalAggro)
            {
                highestAttackerAggroValue = totalAggro;
                if (attackerName != Target?.name)
                {
                    Target = attacker.Transform;
                }
            }

            if (healthBar)
            {
                healthBar.UpdateHealth();
            }

            if (!Stats.IsDead)
            {
                // when an enemy has been ignored for some time, it will lose its interest
                // to attack its target. this is to avoid having them forever following
                // a player that has stopped training combat.
                noDamageDropTargetTimer = 0f;
                return(false);
            }

            noDamageDropTargetTimer = -1f;
            InCombat = false;
            highestAttackerAggroValue = 0;
            Target = null;
            Unlock();

            if (movement)
            {
                movement.Die();
            }
            if (AutomaticRespawn)
            {
                Respawn();
            }
            return(true);
        }
        catch
        {
            return(false);
        }
    }