2
\$\begingroup\$

In my little game project, I have the following piece of code, in the static class DamageHelper, that takes a Spell, and returns it's damage in the Entity:

public static float DamageIncrease(SpellData attack, Entity caster)
{
    //Elemental damage is more increased by wisdom attribute
    //Impact damage is more increased by dominance attribute 
    //summon level is increased by control attribute

    float bonus = 0f;
    switch (attack.element)
    {
        case Element.Fire:
        case Element.Earth:
        case Element.Water:
        case Element.Wind:
            bonus = caster.wisdom * 0.3f + caster.dominance * 0.1f + caster.control * 0.1f;
            break;
        case Element.Impact:
            bonus = caster.wisdom * 0.1f + caster.dominance * 0.3f + caster.control * 0.1f;
            break;
        default:
            bonus = caster.wisdom * 0.1f + caster.dominance * 0.1f + caster.control * 0.3f;
            break;
    }

    return attack.damage + bonus;
}

The function above is called by the this RealDamage function, that may also be improved:

public static float RealDamage(SpellData attack, Entity target, Entity caster)
    {
        float max_variation = attack.damage_variation / 2f;
        float min_variation = max_variation - attack.damage_variation;
        float resist_percent = 0f;
        float dmg = DamageIncrease(attack, caster) + Random.Range(min_variation, max_variation);
        switch (attack.element)
        {
            case (Element.Fire):
                resist_percent = target.GetFireResistance();
                break;
            case (Element.Earth):
                resist_percent = target.GetEarthResistance();
                break;
            case (Element.Water):
                resist_percent = target.GetWaterResistance();
                break;
            case (Element.Wind):
                resist_percent = target.GetWindResistance();
                break;
            case (Element.Impact):
                resist_percent = target.GetPhysicalResistance();
                break;
        }

        dmg -= (dmg * resist_percent / 100f);

        if (dmg < 1f)
            dmg = 0f;

        return dmg;
    }

and this one is called when player casts a spell:

float damage = DamageHelper.RealDamage(data, e, spellCaster);
dmg.GetComponent<DamageText>().MakeTooltip(data.element, damage, e.transform.position);
e.TakeDamage(damage);

It's pretty simple, but I would like to know if there are any ways it can be improved. Also, I will probably add more formulas, since I have to get the bonus from the Entity equipment. In this case, where should I put these?

\$\endgroup\$
6
  • \$\begingroup\$ I am not sure that helper class should contain game logic ... \$\endgroup\$ May 26, 2017 at 12:06
  • \$\begingroup\$ Helper is just the name, maybe it was a bad choice D: because it was intended to contain all the formulas to calculate damage \$\endgroup\$ May 26, 2017 at 12:07
  • \$\begingroup\$ @ArturoTemplário can you show how you cast spells? \$\endgroup\$ May 26, 2017 at 12:26
  • \$\begingroup\$ @SergeyBerezovskiy sure, one second \$\endgroup\$ May 26, 2017 at 12:27
  • \$\begingroup\$ " was intended to contain all the formulas to calculate damage". So, DamageCalculator then? \$\endgroup\$
    – radarbob
    May 27, 2017 at 0:15

1 Answer 1

1
\$\begingroup\$

Define some data structures.

  • Data definitions are clear, complete, obvious, in one place
  • DRY principle - a single place to change data values
  • client code does not change if data values change
  • Declared "Default" Element to keep code consistent
  • Reusable

.

public DamageFactor {
   public float Wisdom    { get; protected set; }
   public float Domanince { get; protected set; }
   public float Control   { get; protected set; }

   public DamageFactor (int wisdom, int domanince, int control) { ... }
}

public Dictionary<Element, DamageFactor> Damage = new Dictionary<Element, DamageFactor>() {  
    { Element.Fire,    new DamageFactor { Wisdom = 0.3f, Dominance = 0.1f, Control = 0.1f },
    { Element.Earth,   new DamageFactor { Wisdom = 0.3f, Dominance = 0.1f, Control = 0.1f },
    { Element.Wind,    new DamageFactor { Wisdom = 0.3f, Dominance = 0.1f, Control = 0.1f },
    { Element.Water,   new DamageFactor { Wisdom = 0.3f, Dominance = 0.1f, Control = 0.1f },
    { Element.Impact,  new DamageFactor { Wisdom = 0.1f, Dominance = 0.3f, Control = 0.1f },
    { Element.Default, new DamageFactor { Wisdom = 0.1f, Dominance = 0.1f, Control = 0.3f }
};

public Dictionary<Element, int> Resistance = new Dictionary<Element, int>() {
    { Element.Fire, 12 },
    { Element.Earth, 9 },
    { Element.Water, 7 },
    { Element.Wind, 5 }, 
    { Element.Impact, 3 },
    { Element.Default, 1 }
};

public static float DamageIncrease(SpellData attack, Entity caster) {
//Elemental damage is more increased by wisdom attribute
//Impact damage is more increased by dominance attribute 
//summon level is increased by control attribute

float bonus = 0f;
bonus = caster.wisdom    * Damage[attack.element].Wisdom +
        caster.dominance * Damage[attack.element].Dominance +
        caster.control   * Damage[attach.element].Control;

return attack.damage + bonus;
}

public static float RealDamage(SpellData attack, Entity target, Entity caster) {
    float max_variation = attack.damage_variation / 2f;
    float min_variation = max_variation - attack.damage_variation;
    float resist_percent = 0f;
    float dmg = DamageIncrease(attack, caster) + Random.Range(min_variation, max_variation);

    resist_percent = Resistance[attack.element];
    dmg -= (dmg * resist_percent / 100f);

    if (dmg < 1f)
        dmg = 0f;

    return dmg;
}
\$\endgroup\$
2
  • \$\begingroup\$ The resist_percent is a property of the Entity (target), based on the attack element. A static dictionary won't work. \$\endgroup\$ May 27, 2017 at 1:34
  • \$\begingroup\$ This actually helped me, mainly the Dictionary<Element, int> part, thank you! \$\endgroup\$ Jun 1, 2017 at 19:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.