//Checks if an encounter with an enemy will be triggered public bool EncounterTriggered() { if (RandomNumberGenerator.RandomNumberBetween(0, 99) < EncounterRate) { return(true); } else { return(false); } }
private int CastDamageSpell(Player player, DamageSpell damageSpell, ref GameSession.BattleResult battleResult) { int spellDamage = 0; //If the enemy would dodge the attack do not calculate spell damage if (RandomNumberGenerator.RandomNumberBetween(1, 100) <= player.DodgeChanceRate) { battleResult = GameSession.BattleResult.Missed; CurrentMana -= damageSpell.ManaCost; return(0); } //If the player would critical strike the enemy then calculate the spell damage accordingly if (RandomNumberGenerator.RandomNumberBetween(1, 100) <= CriticalChanceRate) { //Increase damage by critical damage modifier battleResult = GameSession.BattleResult.Critical; if (player.TotalResistance <= -10) { spellDamage = (int)(((damageSpell.DamageValue + ((Intellect * Intellect) / (Intellect + 1))) * (Math.Abs((double)player.TotalResistance) / 10)) * CriticalDamageModifier); } else if (player.TotalResistance <= 0) { spellDamage = (int)((damageSpell.DamageValue + ((Intellect * Intellect) / (Intellect + 1))) * CriticalDamageModifier); } else { spellDamage = (int)((damageSpell.DamageValue + ((Intellect * Intellect) / (Intellect + player.TotalResistance))) * CriticalDamageModifier); } CurrentMana -= damageSpell.ManaCost; } //Else the player would normally strike the enemy then calculate the spell damage accordingly else { battleResult = GameSession.BattleResult.Normal; if (player.TotalResistance <= -10) { spellDamage = (int)((damageSpell.DamageValue + ((Intellect * Intellect) / (Intellect + 1))) * (Math.Abs((double)player.TotalResistance) / 10)); } else if (player.TotalResistance <= 0) { spellDamage = (int)(damageSpell.DamageValue + ((Intellect * Intellect) / (Intellect + 1))); } else { spellDamage = (int)(damageSpell.DamageValue + ((Intellect * Intellect) / (Intellect + player.TotalResistance))); } CurrentMana -= damageSpell.ManaCost; } return(spellDamage); }
private int Attack(Player player, ref GameSession.BattleResult battleResult) { int damage = 0; //If the player would dodge the attack do not calculate the damage if (RandomNumberGenerator.RandomNumberBetween(1, 100) <= player.DodgeChanceRate) { battleResult = GameSession.BattleResult.Missed; return(damage); } //If the enemy would critical strike the player then calculate the damage accordingly if (RandomNumberGenerator.RandomNumberBetween(1, 100) <= CriticalChanceRate) { //Increase damage by critical damage modifier battleResult = GameSession.BattleResult.Critical; if (player.TotalDefense <= -10) { damage = (int)(((((Strength * Strength) / (Strength + 1)) * 2) * (Math.Abs((double)player.TotalDefense) / 10)) * CriticalDamageModifier); } else if (player.TotalDefense <= 0) { damage = (int)((((Strength * Strength) / (Strength + 1)) * 2) * CriticalDamageModifier); } else { damage = (int)((((Strength * Strength) / (Strength + player.TotalDefense)) * 2) * CriticalDamageModifier); } } //Else the enemy would normally strike the player then calculate the damage accordingly else { battleResult = GameSession.BattleResult.Normal; if (player.TotalDefense <= -10) { damage = (int)((((Strength * Strength) / (Strength + 1)) * 2) * (Math.Abs((double)player.TotalDefense) / 10)); } else if (player.TotalDefense <= 0) { damage = (int)(((Strength * Strength) / (Strength + 1)) * 2); } else { damage = ((Strength * Strength) / (Strength + player.TotalDefense)) * 2; } } return(damage); }
//<----------Combat Actions----------> public int CombatAction(Player player, ref GameSession.BattleResult battleResult, ref GameSession.EnemyChoiceTaken enemyChoiceTaken, ref string spellName) { if (RandomNumberGenerator.RandomNumberBetween(1, 100) <= SpellCastRate) { Spell spellToBeCasted = PickRandomSpell(); int attempts = 0; //While the current spell selected cannot be cast due to insufficent mana keep looking for a different spell //I set 100 as the amount of attempts the enemy gets to cast a spell if they still cannot cast that spell //It means that most likely they don't have enough mana for any spell and they should attack instead while (CurrentMana < spellToBeCasted.ManaCost && attempts < 100) { spellToBeCasted = PickRandomSpell(); attempts++; } if (CurrentMana < spellToBeCasted.ManaCost) { enemyChoiceTaken = GameSession.EnemyChoiceTaken.Attack; return(Attack(player, ref battleResult)); } if (spellToBeCasted is DamageSpell) { enemyChoiceTaken = GameSession.EnemyChoiceTaken.CastSpell; spellName = spellToBeCasted.Name; return(CastDamageSpell(player, (DamageSpell)spellToBeCasted, ref battleResult)); } else if (spellToBeCasted is ReplenishSpell) { enemyChoiceTaken = GameSession.EnemyChoiceTaken.Replenish; spellName = spellToBeCasted.Name; return(CastReplenishSpell((ReplenishSpell)spellToBeCasted)); } else { throw new Exception("Somehow the spell isn't a damage or replenish spell"); } } else { enemyChoiceTaken = GameSession.EnemyChoiceTaken.Attack; return(Attack(player, ref battleResult)); } }
//Selects an enemy from the enemies in the dictionary based on the weights given to them public Enemy SelectEnemy() { if (BossInLocation != null && BossInLocation.CurrentHealth > 0) { return(BossInLocation); } if (BossInLocation != null) { return(null); } int totalWeight = 0; Enemy selectedEnemy = null; if (EnemiesInLocation == null) { return(selectedEnemy); } //Add up all the weights of the enemies foreach (KeyValuePair <Enemy, int> weightedEnemy in EnemiesInLocation) { totalWeight += weightedEnemy.Value; } int randomNum = RandomNumberGenerator.RandomNumberBetween(0, totalWeight - 1); //Selects the enemy based on the random number rolled foreach (KeyValuePair <Enemy, int> weightedEnemy in EnemiesInLocation) { if (randomNum < weightedEnemy.Value) { selectedEnemy = weightedEnemy.Key; break; } randomNum = randomNum - weightedEnemy.Value; } //Generate a clone of the selected enemy and return that clone return(CloneGenerator.CloneEnemy(selectedEnemy)); }
//Determines who goes first in the turn public void DetermineTurn(Player player, Enemy enemy) { if (player.Speed > enemy.Speed) { playerTurn = true; } else if (enemy.Speed > player.Speed) { playerTurn = false; } else { int turnDecider = RandomNumberGenerator.RandomNumberBetween(1, 100); if (turnDecider > 50) { playerTurn = true; } else { playerTurn = false; } } }
private Spell PickRandomSpell() { int spellChoice = RandomNumberGenerator.RandomNumberBetween(0, 1000000) % Spells.Count; return(Spells[spellChoice]); }