Skip to content
Snippets Groups Projects
Commit 813a8c28 authored by Susan Yuen's avatar Susan Yuen
Browse files
parents 36918dac 702fb359
No related branches found
No related tags found
No related merge requests found
......@@ -5,7 +5,76 @@ using System.Text;
namespace Model
{
//This class deals with all damage calculations dealt by a unit attacking another unit
class DamageCalculations
{
//passes in the 2 units, and a boolean on whether attack is physical (false), or magical (true), and returns damage dealt by taking into account an attacker's str/int, and defender def/res
public int getDamageDealt(Unit attacker, Unit defender, bool physOrMagic){
if(physOrMagic== false)
{
return attacker.getStr() - defender.getDefense(); //return physical damage dealt
}
else
{
return attacker.getInt() - defender.getResistance(); //else return magical damage dealt
}
}
//passes in the 2 units, and returns the hit rate as a percentage out of 100 by taking into account both unit's skill
public int getHitRate(Unit attacker, Unit defender)
{
return (int) Math.Round( ( ( (attacker.getSkill() / 10.0) - (defender.getSkill() / 10.0) + 1.0) * 0.8)*100.0);
}
//passes in the 2 units, and returns the crit rate as a percentage out of 100 by taking into account both unit's skill
public int getCritRate(Unit attacker, Unit defender)
{
return (int)Math.Round( ( ( (attacker.getSkill() / 3.0) - (defender.getSkill() / 3.0) + 1.0) * 0.8) * 100.0);
}
//passes in then 2 units, and determines how many attacks the attacker makes by factoring in both unit's relative speed
public int getHitCount(Unit attacker, Unit defender)
{
if (attacker.getSpeed() > (defender.getSpeed() + 4))
{
return 2; //return 2 attack if speed of attacker if 4 higher
}
else
{
return 1; //else return 1
}
}
//factors in damage dealt, hit rate, crit rate, and number of attacks (as in how above functions were calculated) to calculate actual damage dealt
public int finalDamage(Unit attacker, Unit defender, bool physOrMagic)
{
int rawDamage = getDamageDealt(attacker, defender, physOrMagic);
int hitRate = getHitRate(attacker, defender);
int critRate = getCritRate(attacker, defender);
int numOfAttacks = getHitCount(attacker, defender);
Random rnd = new Random();
int hitOrMiss = rnd.Next(0, 101); // creates a number between 0-100
int critOrNot = rnd.Next(0, 101); // creates a number between 0-100
if (hitOrMiss > hitRate) //if the random number is greater then the hitrate, the attack misses and 0 is returned
{
return 0;
}
else
{
if(critOrNot > critRate) //if attack doesn't crit
{
return rawDamage * numOfAttacks; //return the damage * number of attacks calculated from above
}
else //else if attack crits
{
return rawDamage * numOfAttacks * 2; //returns the damage * number of attacks *2 for critical for damage dealt elsewise
}
}
}
}
}
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment