Example #1
0
    /**
     * SETTERS AND CHANGERS
     */
    // defence
    public void DealDamage(float amount, UnitStats attackerStats, UnitStatsComponent attackerStatsComponent)
    {
        if (Invulnerable)
        {
            return;
        }

        // Check if the target evades the projectile and proceed if it doesn't
        if (!(EvasionChance > 0f && Random.value < EvasionChance))
        {
            // Check if the thrower crits and increase damage if successful
            if (attackerStats.critChance > 0f && Random.value < attackerStats.critChance)
            {
                // Crit was successful
                amount *= (1 + attackerStats.critExtraMultiplier);
            }

            // Modify damage from target's armor and armor type, and attacker's attack type.
            amount *= GameplayConstants.ArmorDamageReduction(attackerStats.attackType, ArmorType, Armor);

            unitStats.health -= amount;

            if (attackerStatsComponent)
            {
                attackerStatsComponent.ApplyOnAttackEffects(amount);
            }

            if (IsDead)
            {
                // Give gold to killing player (always gives to Client as-of-now)
                GameObject client = GameObject.Find("Client");
                client.GetComponent <GoldContainer>().ChangeGold(gameObject.GetComponent <UnitStatsComponent>().GoldDropped);

                // Give experience to the killing team (always gives to west team as-of-now)
                Teams team        = Globals.Teams;
                int   teamMembers = team.CountTeam(true);
                foreach (GameObject hero in team.WestTeam)
                {
                    if (hero)
                    {
                        // Divide experience between all Heroes on the killing team, calculated from a base value factored by the killed unit's level
                        float experience = GameplayConstants.MonsterLevelOneExpDrop * Mathf.Pow(GameplayConstants.MonsterExpDropIncreaseFactorPerLevel, MonsterLevel) / teamMembers;
                        hero.GetComponent <HeroStatsComponent>().AddExperience(experience);
                    }
                }

                // and destroy
                Destroy(gameObject);
            }
            else if (Health > MaxHealth)
            {
                unitStats.health = unitStats.maxHealth;
            }
        }
    }