/// <summary> /// Handles a fight between two Heroes. When the fight is lost, the game ends. When the fight is won, the player gets a random item and levels up. /// </summary> /// <param name="hero">The opponent</param> private static void Fight(Hero hero, Hero opponent, double herosLuck, double opponentsLuck) { GameWriter.OpponentDescriptionMessage(opponent.GetType().Name, opponent.DPS); GameWriter.PressKeyToContinue(); if (hero.DPS * herosLuck < opponent.DPS * opponentsLuck) { GameWriter.GameLostMessage(); GameWriter.EndGameMessage(hero.Name); Environment.Exit(0); return; } GameWriter.GameWonMessage(); GameWriter.PressKeyToContinue(); hero.LevelUp(1); GameWriter.LevelUpMessage(hero.Level); GameWriter.PressKeyToContinue(); HandleFoundItem(hero); }
/// <summary> /// Calls the actions the player specified. /// </summary> /// <param name="action">Action entered by player</param> /// <returns>True if game continues, false if game ended</returns> private bool PlayGameAction(int action) { switch (action) { case 1: Hero opponent = CreateRandomOpponent(PlayerHero.Level); // Characters get a luck bonus on their DPS between 0 and 20% var rand = new Random(); double herosLuck = rand.NextDouble() * (1.2 - 1.0) + 1.0; double opponentsLuck = rand.NextDouble() * (1.2 - 1.0) + 1.0; Fight(PlayerHero, opponent, herosLuck, opponentsLuck); break; case 2: PlayerHero.DisplayStats(); break; default: case 3: GameWriter.EndGameMessage(PlayerHero.Name); return(false); } return(true); }
/// <summary> /// Starts the game. /// </summary> public void Play() { GameWriter.WelcomeMessage(); int heroType = GameReader.GetHeroType(); string name = GameReader.GetHeroName(MaxLengthOfName); PlayerHero = CreateHero(heroType, name); int action; do { action = GameReader.GetGameAction(); } while (PlayGameAction(action)); }
/// <summary> /// Asks player for their hero name. /// </summary> /// <returns>Name of hero string</returns> public static string GetHeroName(int maxLengthOfName) { GameWriter.AskForHeroNameMessage(); string name = ReadLine(); while (!NameInputIsValid(name, maxLengthOfName)) { GameWriter.ClearScreen(); GameWriter.AskForHeroNameErrorMessage(maxLengthOfName); GameWriter.AskForHeroNameMessage(); name = ReadLine(); } GameWriter.ClearScreen(); return(name); }
/// <summary> /// Asks the player which action they want to perform. /// </summary> /// <returns>Action number</returns> public static int GetGameAction() { GameWriter.DisplayGameOptions(); var actionInput = ReadLine(); int action; while (!int.TryParse(actionInput, out action) || action < 1 || action > 4) { GameWriter.ClearScreen(); GameWriter.AskForNumberMessage(1, 3); GameWriter.DisplayGameOptions(); actionInput = ReadLine(); } GameWriter.ClearScreen(); return(action); }
/// <summary> /// Asks the player which type of hero they want to play with. /// </summary> /// <returns>Hero type number</returns> public static int GetHeroType() { GameWriter.DisplayHeroOptions(); var typeInput = ReadLine(); int heroType; while (!int.TryParse(typeInput, out heroType) || heroType < 1 || heroType > 4) { GameWriter.ClearScreen(); GameWriter.AskForNumberMessage(1, 4); GameWriter.DisplayHeroOptions(); typeInput = ReadLine(); } GameWriter.ClearScreen(); return(heroType); }
/// <summary> /// Creates a new random item and asks the player what they want to do with it. /// </summary> /// <param name="hero">The players hero</param> private static void HandleFoundItem(Hero hero) { Item item = CreateRandomItem(); string typeOfItem = item.GetType().Name; GameWriter.ItemFoundMessage(item.ItemDescription()); switch (GameReader.GetFoundItemAction()) { case 1: try { string typeOfWeapon = new Weapon().GetType().Name; if (typeOfItem.Equals(typeOfWeapon)) { hero.Equip((Weapon)item); } else { hero.Equip((Armor)item); } GameWriter.ItemEquippedMessage(typeOfItem); GameWriter.PressKeyToContinue(); } catch (Exception e) { GameWriter.ItemEquippedErrorMessage(e.Message); GameWriter.PressKeyToContinue(); } break; default: break; } }
/// <summary> /// Creates a Hero of a random type with a random weapon. /// </summary> /// <param name="level">Level of the hero</param> /// <returns>Random Hero</returns> private static Hero CreateRandomOpponent(int level) { var rand = new Random(); int type = rand.Next(1, 5); int damage = rand.Next(1, level); double attackSpeed = rand.NextDouble() * (level - 0.1) + 0.1; Weapon weapon = new() { ItemName = "Weapon", ItemLevel = level, ItemSlot = Slot.SLOT_WEAPON, WeaponAttributes = new WeaponAttributes() { Damage = damage, AttackSpeed = attackSpeed } }; Hero opponent; string name = "Opponent"; switch (type) { default: case 1: opponent = new Mage(name); weapon.WeaponType = WeaponType.WEAPON_WAND; break; case 2: opponent = new Ranger(name); weapon.WeaponType = WeaponType.WEAPON_BOW; break; case 3: opponent = new Rogue(name); weapon.WeaponType = WeaponType.WEAPON_DAGGER; break; case 4: opponent = new Warrior(name); weapon.WeaponType = WeaponType.WEAPON_SWORD; break; } if (level > 1) { opponent.LevelUp(level - 1); } try { opponent.Equip(weapon); } catch (Exception e) { GameWriter.ItemEquippedErrorMessage(e.Message); } opponent.CalculateTotalStats(); return(opponent); }