1
\$\begingroup\$

How can i divide points based on highest damage? For e.g i have monster which drop 50 experience points and 4 different players has dealt damage to this monster. But i will like to split the experience points based on the players damage.

So far i got this:

private Dictionary<Player, int> damageMap = new Dictionary<Player, int>();

public void TakeDamage(Player attacker, int damage)
{
    damageMap.Add(attacker, damage);
}

private void OnDeath()
{
    foreach (KeyValuePair<Player, int> entry in damageMap)
    {
        Player player = entry.Key;
        int damage = entry.Value;

        player.addExperience(50);
    }
}
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

If you have experience as a float

float totalExperience = 50.0f;
foreach (KeyValuePair<Player, int> entry in damageMap)
{
    Player player = entry.Key;
    int damage = entry.Value;

    float playerDamageFraction = (float)damage/(float)totalMonsterHealth;

    player.addExperience(totalExperience * playerDamageFraction);
}

Should work

If your experience is an int then do the same thing but round to an int at the last step

player.addExperience(Mathf.RoundToInt(totalExperience * playerDamageFraction));

This is beyond the scope of your question but I noticed that you may find you have a problem with your TakeDamage() function

public void TakeDamage(Player attacker, int damage)
{
    damageMap.Add(attacker, damage);
}

Probably needs to be

public void TakeDamage(Player attacker, int damage)
{
    if (dict.ContainsKey(player)){ 
        damageMap[player] += damage;
    } else{
        damageMap.Add(attacker, damage);
    }
}

To get the behavior you want

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Thanks Jake, math is not my strongest side. Good catch about the TakeDamage \$\endgroup\$ Commented Apr 30, 2020 at 18:48

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.