private void StartFight(Enemy enemy, int enemyCurrentRow, int enemyCurrentCol) { var enemyDeffoultRow = enemyCurrentRow; var enemyDeffoultCol = enemyCurrentCol; Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("A fight is started between our Hero(health={0}, mana={1}) and Enemey(health={2}, mana={3}, damage={4}):", this.hero.GetHealth(), this.hero.GetMana(), enemy.GetHealth(), enemy.GetMana(), enemy.Attack(enemy.Weapon)); Console.WriteLine(); bool isChecked = false; while (true) { if (!this.hero.CanCast() && !isChecked) { isChecked = true; Console.ForegroundColor = ConsoleColor.DarkGreen; Console.WriteLine("Hero does not have mana for another {0}.", this.hero.Spell.Name); Console.ForegroundColor = ConsoleColor.Gray; } if ((this.hero.Spell.Damage >= this.hero.Weapon.Damage || (this.currentRow > enemyCurrentRow || this.currentRow < enemyCurrentRow || this.currentCol > enemyCurrentCol || this.currentCol < enemyCurrentCol)) && this.hero.CanCast()) { enemy.TakeDamage(hero.Attack(hero.Spell)); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Hero casts a {0}, hits enemy for {1} dmg. Enemy health is {2}", this.hero.Spell.Name, this.hero.Spell.Damage, enemy.GetHealth()); Console.ForegroundColor = ConsoleColor.Gray; } else { if (enemyCurrentRow < this.currentRow) { this.map[currentRow, currentCol] = '.'; this.currentRow--; this.map[currentRow, currentCol] = 'H'; Console.WriteLine("Hero moves one square to up in order to get to the enemy. This is hero move."); } else if (enemyCurrentRow > this.currentRow) { this.map[currentRow, currentCol] = '.'; this.currentRow++; this.map[currentRow, currentCol] = 'H'; Console.WriteLine("Hero moves one square to down in order to get to the enemy. This is hero move."); } else if (enemyCurrentCol < this.currentCol) { this.map[currentRow, currentCol] = '.'; this.currentCol--; this.map[currentRow, currentCol] = 'H'; Console.WriteLine("Hero moves one square to left in order to get to the enemy. This is hero move."); } else if (enemyCurrentCol > this.currentCol) { this.map[currentRow, currentCol] = '.'; this.currentCol++; this.map[currentRow, currentCol] = 'H'; Console.WriteLine("Hero moves one square to right in order to get to the enemy. This is hero move."); } else { Console.ForegroundColor = ConsoleColor.Green; enemy.TakeDamage(hero.Attack(hero.Weapon)); Console.WriteLine("Hero hits with {0} for {1} dmg. Enemy health is {2}", this.hero.Weapon.Name, this.hero.Weapon.Damage, enemy.GetHealth()); Console.ForegroundColor = ConsoleColor.Gray; } } if (!enemy.IsAlive()) { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Enemy is dead!"); if (this.map[enemyDeffoultRow, enemyDeffoultCol] != 'H') { this.map[enemyDeffoultRow, enemyDeffoultCol] = '.'; } Console.ForegroundColor = ConsoleColor.Gray; break; } Console.ForegroundColor = ConsoleColor.DarkYellow; if (enemyCurrentRow < this.currentRow) { if (enemy.Spell != null) { var distance = this.currentRow - enemyCurrentRow; if (enemy.CanCast() && distance <= enemy.Spell.CastRange) { hero.TakeDamage(enemy.Attack(enemy.Spell)); Console.ForegroundColor = ConsoleColor.DarkRed; Console.WriteLine("Enemy casts a {0}, hits hero for {1} dmg. Hero health is {2}", enemy.Spell.Name, enemy.Spell.Damage, this.hero.GetHealth()); } else { enemyCurrentRow++; Console.WriteLine("Enemy moves one square to down in order to get to the hero. This is his move."); } } else { enemyCurrentRow++; Console.WriteLine("Enemy moves one square to down in order to get to the hero. This is his move."); } } else if (enemyCurrentRow > this.currentRow) { if (enemy.Spell != null) { var distance = enemyCurrentRow - this.currentRow; if (enemy.CanCast() && distance <= enemy.Spell.CastRange) { hero.TakeDamage(enemy.Attack(enemy.Spell)); Console.ForegroundColor = ConsoleColor.DarkRed; Console.WriteLine("Enemy casts a {0}, hits hero for {1} dmg. Hero health is {2}", enemy.Spell.Name, enemy.Spell.Damage, this.hero.GetHealth()); } else { enemyCurrentRow--; Console.WriteLine("Enemy moves one square to up in order to get to the hero. This is his move."); } } else { enemyCurrentRow--; Console.WriteLine("Enemy moves one square to up in order to get to the hero. This is his move."); } } else if (enemyCurrentCol < this.currentCol) { if (enemy.Spell != null) { var distance = this.currentCol - enemyCurrentCol; if (enemy.CanCast() && distance <= enemy.Spell.CastRange) { hero.TakeDamage(enemy.Attack(enemy.Spell)); Console.ForegroundColor = ConsoleColor.DarkRed; Console.WriteLine("Enemy casts a {0}, hits hero for {1} dmg. Hero health is {2}", enemy.Spell.Name, enemy.Spell.Damage, this.hero.GetHealth()); } else { enemyCurrentCol++; Console.WriteLine("Enemy moves one square to the right in order to get to the hero. This is his move."); } } else { enemyCurrentCol++; Console.WriteLine("Enemy moves one square to the right in order to get to the hero. This is his move."); } } else if (enemyCurrentCol > this.currentCol) { if (enemy.Spell != null) { var distance = enemyCurrentCol - this.currentCol; if (enemy.CanCast() && distance <= enemy.Spell.CastRange) { hero.TakeDamage(enemy.Attack(enemy.Spell)); Console.ForegroundColor = ConsoleColor.DarkRed; Console.WriteLine("Enemy casts a {0}, hits hero for {1} dmg. Hero health is {2}", enemy.Spell.Name, enemy.Spell.Damage, this.hero.GetHealth()); } else { enemyCurrentCol--; Console.WriteLine("Enemy moves one square to the left in order to get to the hero. This is his move."); } } else { enemyCurrentCol--; Console.WriteLine("Enemy moves one square to the left in order to get to the hero. This is his move."); } } else { if (enemy.Spell != null) { if ((enemy.Spell.Damage >= enemy.Attack(enemy.Weapon) && enemy.CanCast())) { hero.TakeDamage(enemy.Attack(enemy.Spell)); Console.ForegroundColor = ConsoleColor.DarkRed; Console.WriteLine("Enemy casts a {0}, hits hero for {1} dmg. Hero health is {2}", enemy.Spell.Name, enemy.Spell.Damage, this.hero.GetHealth()); Console.ForegroundColor = ConsoleColor.Gray; } else { hero.TakeDamage(enemy.Attack(enemy.Weapon)); Console.ForegroundColor = ConsoleColor.DarkRed; Console.WriteLine("Enemy hits hero for {0} dmg. Hero health is {1}.", enemy.Attack(enemy.Weapon), this.hero.GetHealth()); Console.ForegroundColor = ConsoleColor.Gray; } } else { hero.TakeDamage(enemy.Attack(enemy.Weapon)); Console.ForegroundColor = ConsoleColor.DarkRed; Console.WriteLine("Enemy hits hero for {0} dmg. Hero health is {1}.", enemy.Attack(enemy.Weapon), this.hero.GetHealth()); Console.ForegroundColor = ConsoleColor.Gray; } } Console.ForegroundColor = ConsoleColor.Gray; if (!this.hero.IsAlive()) { Console.ForegroundColor = ConsoleColor.DarkYellow; Console.WriteLine("Hero is dead!"); this.map[this.currentRow, this.currentCol] = '.'; bool canRespawn = this.Spawn(this.hero); if (!canRespawn) { Console.WriteLine(); Console.ForegroundColor = ConsoleColor.DarkRed; Console.WriteLine("Game Over!"); } Console.ForegroundColor = ConsoleColor.Gray; break; } } Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine(); }
public void HeroAttack(string by) { if (this.hero.IsAlive() == true) { if (by.ToLower() == "spell" && this.hero.CanCast() && this.hero.Spell != null) { Enemy enemy = new Enemy(100, 100, 20); //If want enemy to cast spells uncommend next 2 lines: //var enemySpell = new Spell("Fire Storm", 20, 30, 2); //enemy.Spell = enemySpell; for (int i = 1; i <= this.hero.Spell.CastRange; i++) { if ((this.currentRow - i == '#') || (this.currentRow - i < 0)) { break; } else if (this.map[currentRow - i, currentCol] == 'E') { StartFight(enemy, this.currentRow - i, this.currentCol); return; } } for (int i = 1; i <= this.hero.Spell.CastRange; i++) { if ((this.currentRow + i == '#') || (this.currentRow + i > this.map.GetLength(0) - 1)) { break; } else if (this.map[currentRow + i, currentCol] == 'E') { StartFight(enemy, this.currentRow + i, this.currentCol); return; } } for (int i = 1; i <= this.hero.Spell.CastRange; i++) { if ((this.currentCol - i == '#') || (this.currentCol - i < 0)) { break; } else if (this.map[currentRow, currentCol - i] == 'E') { StartFight(enemy, this.currentRow, this.currentCol - i); return; } } for (int i = 1; i <= this.hero.Spell.CastRange; i++) { if ((this.currentCol + i == '#') || (this.currentCol + i > this.map.GetLength(1) - 1)) { Console.WriteLine("Nothing in casting range {0}", this.hero.Spell.CastRange); Console.WriteLine(); break; } else if (this.map[currentRow, currentCol + i] == 'E') { StartFight(enemy, this.currentRow, this.currentCol + i); return; } else if (i == this.hero.Spell.CastRange) { Console.WriteLine("Nothing in casting range {0}", this.hero.Spell.CastRange); Console.WriteLine(); } } } else if (this.hero.Spell == null) { Console.ForegroundColor = ConsoleColor.DarkRed; Console.WriteLine("No skill learned."); Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine(); } else { Console.ForegroundColor = ConsoleColor.DarkBlue; Console.WriteLine("No mana for skill."); Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine(); } } }
public bool MoveHero(Direction direction) { if (this.hero.IsAlive()) { Enemy enemy = new Enemy(100, 100, 20); //If want enemy to cast spells uncommend next 2 lines: //var enemySpell = new Spell("Fire Storm", 20, 30, 2); //enemy.Spell = enemySpell; this.hero.TakeMana(this.hero.ManaRegen); switch (direction) { case Direction.Up: { if (this.currentRow - 1 < 0 || this.map[this.currentRow - 1, this.currentCol] == '#' || this.map[this.currentRow - 1, this.currentCol] == 'S') { return false; } else if (this.map[this.currentRow - 1, this.currentCol] == 'T') { PickTreasure(); } else if (this.map[this.currentRow - 1, this.currentCol] == 'E') { StartFight(enemy, this.currentRow - 1, this.currentCol); return true; } else if (this.map[this.currentRow - 1, this.currentCol] == 'G') { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("You escaped from Dungen!"); Console.ForegroundColor = ConsoleColor.Gray; return true; } this.map[this.currentRow, this.currentCol] = '.'; this.map[this.currentRow - 1, this.currentCol] = 'H'; this.currentRow = this.currentRow - 1; return true; } case Direction.Down: { if (this.currentRow + 1 >= this.map.GetLongLength(0) || this.map[this.currentRow + 1, this.currentCol] == '#' || this.map[this.currentRow + 1, this.currentCol] == 'S') { return false; } else if (this.map[this.currentRow + 1, this.currentCol] == 'T') { PickTreasure(); } else if (this.map[this.currentRow + 1, this.currentCol] == 'E') { StartFight(enemy, this.currentRow + 1, this.currentCol); return true; } else if (this.map[this.currentRow + 1, this.currentCol] == 'G') { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("You escaped from Dungen!"); Console.ForegroundColor = ConsoleColor.Gray; return true; } this.map[this.currentRow, this.currentCol] = '.'; this.map[this.currentRow + 1, this.currentCol] = 'H'; this.currentRow = currentRow + 1; return true; } case Direction.Left: { if (this.currentCol - 1 > 0 || this.map[this.currentRow, this.currentCol - 1] == '#' || this.map[this.currentRow, this.currentCol - 1] == 'S') { return false; } else if (this.map[this.currentRow, this.currentCol - 1] == 'T') { PickTreasure(); } else if (this.map[this.currentRow, this.currentCol - 1] == 'E') { StartFight(enemy, this.currentRow, this.currentCol - 1); return true; } else if (this.map[this.currentRow, this.currentCol - 1] == 'G') { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("You escaped from Dungen!"); Console.ForegroundColor = ConsoleColor.Gray; return true; } this.map[this.currentRow, this.currentCol] = '.'; this.map[this.currentRow, this.currentCol - 1] = 'H'; this.currentCol = this.currentCol - 1; return true; } case Direction.Right: { if (this.currentCol + 1 > this.map.GetLength(1) - 1 || this.map[this.currentRow, this.currentCol + 1] == '#' || this.map[this.currentRow, this.currentCol + 1] == 'S') { return false; } else if (this.map[this.currentRow, this.currentCol + 1] == 'T') { PickTreasure(); } else if (this.map[this.currentRow, this.currentCol + 1] == 'E') { StartFight(enemy, this.currentRow, this.currentCol + 1); return true; } else if (this.map[this.currentRow, this.currentCol + 1] == 'G') { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("You escaped from Dungen!"); Console.ForegroundColor = ConsoleColor.Gray; return true; } this.map[this.currentRow, this.currentCol] = '.'; this.map[this.currentRow, this.currentCol + 1] = 'H'; this.currentCol = this.currentCol + 1; return true; } default: return false; } } else { return false; } }