public static void ExecuteCommand(string command, Hero[] heroes, List<EnemyClass> enemies) { string[] commandParts = command.Split(' '); switch (commandParts[0].ToLower()) { case "start": { StartGame(); break; } case "help": { PrintHelp(); break; } default: // since the code for all heroes is the same we can just put it in default and let the try and catch pick up unregonised commands { if(commandParts[1].Equals("info")) { PrintInfo(commandParts[0]); break; } TryToUseAbility(heroes, enemies, commandParts); break; } } }
public static void EquipItem(Hero[] heroes, string[] commandParts = null, string itemName = "", int currentHeroIndex = 0) { try { if (commandParts != null) { currentHeroIndex = GameConsole.GetHeroIndex(commandParts[0]); //Get the current hero index for (int i = 2; i < commandParts.Length; i++) // Since the item name may contain more than 1 word we need to join all the words and make a name of them { if (i == commandParts.Length - 1) { itemName += commandParts[i]; } else { itemName += commandParts[i] + " "; } } } foreach (var item in ItemPool.inventory) // Go through the whole inventory to look for the item { if (item.name.ToLower().Equals(itemName.ToLower())) { FieldInfo[] fields = heroes[currentHeroIndex].GetType().GetFields(); // Gets all of fields of the current hero foreach (var field in fields) { var type = field.GetValue(heroes[currentHeroIndex]).GetType(); if (type == typeof(ItemPool.Item)) // We need only the fields that are with a type of item { string slotName = Regex.Match(field.Name, @"^.*?(?=[A-Z])").Value; //Match the part of the field name that is before a CapitalLetter if (slotName.ToLower().Equals(item.slot.ToString().ToLower())) { foreach (var classAbleToWearItem in item.classesAbleToWearItem) // Go through all the classes able to wear the item { string className = Regex.Match(heroes[currentHeroIndex].GetType().Name, @"^[A-Za-z](.*?(?=[A-Z]))").Value; //Match the part of the class name that is before a CapitalLetter and Starts with a capital letter if (classAbleToWearItem.ToString().ToLower().Equals(className.ToLower())) //Check if the current class being checked is equal to the hero class name { field.SetValue(heroes[currentHeroIndex], item); GameConsole.currentLog.Append("Item has been equipped: "); PrintItem(item); ItemPool.inventory.Remove(item); return; } } GameConsole.currentLog.Append("Hero cannot equip item \n"); return; } } } } } GameConsole.currentLog.Append("Item not present in inventory \n"); } catch (Exception) // Every unregonised commands is basically a different exception { UnrecognizedCommand(); } }
private static void AddExperience(Hero[] heroes, int experienceGained) { for (int i = 0; i < heroes.Length; i++) { if(heroes[i].experience < Test.levels[heroes[i].level - 1]) { heroes[i].experience += experienceGained; if (heroes[i].experience > Test.levels[heroes[i].level]) { heroes[i].level++; } if (heroes[i].level == Test.maxLevel) { heroes[i].experience = Test.levels[heroes[i].level - 1]; } } } }
private static bool HeroHasMana(Hero hero, MethodInfo ability, EnemyClass enemy = null) { int currentMana = hero.mana; //Check if the hero has mana for the ability if(enemy == null) { ability.Invoke(hero, null); } else { ability.Invoke(hero, new object[] { enemy }); } if (hero.mana < 0 && hero.mana >= -100) { Console.WriteLine("No mana for that ability"); return false; } else if(hero.mana < - 100) { Console.WriteLine("Need a higher level to use that ability"); return false; } hero.mana = currentMana; return true; }
private static void AddExperience(Hero[] heroes, int experienceGained) { for (int i = 0; i < heroes.Length; i++) // Go through each hero and add the experience to all of them not just one { if (heroes[i].level <= GameConsole.maxLevel && heroes[i].health > 0) //If the hero's experience is less than the max level { heroes[i].experience += experienceGained; if (heroes[i].experience > GameConsole.levels[heroes[i].level]) // If the hero's experience is more than the current level required { heroes[i].OnLevelUp(); } if (heroes[i].level == GameConsole.maxLevel) { heroes[i].experience = GameConsole.levels[heroes[i].level - 1]; } } } }
public static void PrintStatus(Hero[] heroes, List<EnemyClass> enemies) { List<string[,]> heroesResult = new List<string[,]>(); List<string[,]> enemiesResult = new List<string[,]>(); for (int i = 0; i < heroes.Length; i++) { string heroClass = Regex.Match(heroes[i].GetType().Name, @"^[A-Za-z](.*?(?=[A-Z]))").Value; //Match the part of the class name that is before a CapitalLetter and Starts with a capital letter heroesResult.Add(Character(new string[] {heroClass + ": " + heroes[i].name, "Health: " + heroes[i].health + "/" + heroes[i].maxHealth, "Mana: " + heroes[i].mana + "/" + heroes[i].maxMana, "Acted: " + GameConsole.heroesActedThisTurn[i], "Experience: " + heroes[i].experience + "/" + GameConsole.levels[heroes[i].level], "Level: " + heroes[i].level})); } for (int i = 0; i < enemies.Count; i++) { enemiesResult.Add(Character(new string[] { enemies[i].name, "Health: " + enemies[i].health + "/" + enemies[i].maxHealth, "Mana: " + enemies[i].mana + "/" + enemies[i].maxMana })); } PrintResult(heroesResult); Console.WriteLine(); PrintResult(enemiesResult); Console.WriteLine(); }
public Hero GetHeroWithHighestIntelligence() { Hero best = this.heroes.OrderByDescending(x => x.Item.Intelligence).FirstOrDefault(); return(best); }
public void Add(Hero hero) { this.heroes.Add(hero); }
//• Method Remove(string name) – removes an entity by given hero name. public void Remove(string name) { Hero nameHero = this.data.FirstOrDefault(x => x.Name == name); this.data.Remove(nameHero); }
private static void UnEquipItem(Hero[] heroes, string[] commandParts) { try { int currentHeroIndex = GameConsole.GetHeroIndex(commandParts[0]); //Get the current hero index string itemName = ""; for (int i = 2; i < commandParts.Length; i++) // Since the item name may contain more than 1 word we need to join all the words and make a name of them { if (i == commandParts.Length - 1) { itemName += commandParts[i]; } else { itemName += commandParts[i] + " "; } } FieldInfo[] fields = heroes[currentHeroIndex].GetType().GetFields(); // Gets all of fields of the current hero foreach (var field in fields) { var type = field.GetValue(heroes[currentHeroIndex]).GetType(); if (type == typeof(ItemPool.Item)) // We need only the fields that are with a type of item { FieldInfo[] itemFields = type.GetFields(); // Gets all of fields of the current item slot foreach (var itemField in itemFields) { if (itemField.GetValue(field.GetValue(heroes[currentHeroIndex])) == null) { break; } if (itemField.GetValue(field.GetValue(heroes[currentHeroIndex])).ToString().ToLower().Equals(itemName.ToLower())) { GameConsole.currentLog.Append("Item has been unequipped: "); PrintItem((ItemPool.Item)field.GetValue(heroes[currentHeroIndex])); ItemPool.inventory.Add((ItemPool.Item)field.GetValue(heroes[currentHeroIndex])); field.SetValue(heroes[currentHeroIndex], new ItemPool.Item()); return; } break; } } } GameConsole.currentLog.Append("Item not equipped on hero \n"); } catch (Exception) // Every unregonised commands is basically a different exception { UnrecognizedCommand(); } }
private static void TryToUseAbility(Hero[] heroes, List<EnemyClass> enemies, string[] commandParts) { try { int currentHeroIndex = Test.GetHeroIndex(commandParts[0]); //Get the current hero index if (Test.heroesActedThisTurn[currentHeroIndex]) { Console.WriteLine("Hero has already acted this turn"); return; } if(commandParts[1].ToLower().Equals("skip")) { Test.heroesActedThisTurn[currentHeroIndex] = true; return; } MethodInfo ability = heroes[currentHeroIndex].GetType().GetMethod(commandParts[1].ToLower(), BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy ); //By passing a string of the name of the method we can get it as a variable int enemyIndex; if (int.TryParse(commandParts[2], out enemyIndex)) //Try to parse the third string as a number if it is a number we use an ability on an enemy { enemyIndex--; // Because the left most enemy is not with index 0 instead of 1 in the array if(!HeroHasMana(heroes[currentHeroIndex], ability, enemies[enemyIndex])) { return; } enemies[enemyIndex].health = (enemies[enemyIndex].health - (int)ability.Invoke(heroes[currentHeroIndex], new object[] { enemies[enemyIndex] } )); if(enemies[enemyIndex].health < 0) { enemies[enemyIndex].health = 0; AddExperience(heroes, enemies[enemyIndex].experienceWorth); } } else // Else we use an ability on an ally { int heroTargetedIndex = Test.GetHeroIndex(commandParts[2]); if (!HeroHasMana(heroes[currentHeroIndex], ability)) { return; } if (ability.Name.ToLower().Equals("revive")) { ability.Invoke(heroes[currentHeroIndex], new object[] { heroes[heroTargetedIndex] }); } else { heroes[heroTargetedIndex].health = (heroes[heroTargetedIndex].health + (int)ability.Invoke(heroes[currentHeroIndex], null)); if (heroes[heroTargetedIndex].health > heroes[heroTargetedIndex].maxHealth) { heroes[heroTargetedIndex].health = heroes[heroTargetedIndex].maxHealth; } } } Test.heroesActedThisTurn[currentHeroIndex] = true; } catch (Exception) // Every unregonised commands is basically a diffrent exception { UnrecognizedCommand(); } }
public Hero GetHeroWithHighestStrength() { Hero hero = this.heroes.OrderByDescending(x => x.Item.Intelligence).FirstOrDefault(); return(hero); }
public void Remove(string name) { Hero targetHero = this.heroes.FirstOrDefault(x => x.Name == name); this.heroes.Remove(targetHero); }
public void Add(Hero hero) { this.data.Add(hero); }
private static bool HeroHasMana(Hero hero, MethodInfo ability, EnemyClass enemy = null) { int currentMana = (int)hero.mana; //Check if the hero has mana for the ability if (enemy == null) //If there is no enemy the ability is a void { ability.Invoke(hero, null); } else { ability.Invoke(hero, new object[] { enemy }); } if (hero.mana < 0 && hero.mana >= -100) // If after being invoked the hero's mana is between 0 and -100 he doesn't have the mana for the ability { GameConsole.currentLog.Append("No mana for that ability \n"); hero.mana = currentMana; return false; } else if (hero.mana < -100)// If after being invoked the hero's level is not enough for the ability he loses 1000 mana temporarily { GameConsole.currentLog.Append("Need a higher level to use that ability \n"); hero.mana = currentMana; return false; } hero.mana = currentMana; return true; }
private static void TryToUseAbility(Hero[] heroes, List<EnemyClass> enemies, string[] commandParts) { try { int currentHeroIndex = GameConsole.GetHeroIndex(commandParts[0]); //Get the current hero index if(GameConsole.heroes[currentHeroIndex].health <= 0) { GameConsole.currentLog.Append("Hero is dead. Revive him first \n"); return; } if (GameConsole.heroesActedThisTurn[currentHeroIndex]) // Check if the hero has acted this turn { GameConsole.currentLog.Append("Hero has already acted this turn \n"); return; } if (commandParts[1].ToLower().Equals("skip") && commandParts.Length == 2) { GameConsole.heroesActedThisTurn[currentHeroIndex] = true; return; } MethodInfo ability = heroes[currentHeroIndex].GetType().GetMethod(commandParts[1].ToLower(), BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy); //By passing a string of the name of the method we can get it as a variable int enemyIndex; if (int.TryParse(commandParts[2], out enemyIndex)) //Try to parse the third string as a number if it is a number we use an ability on an enemy { enemyIndex--; // Because the left most enemy is not with index 0 instead of 1 in the array if (!HeroHasMana(heroes[currentHeroIndex], ability, enemies[enemyIndex])) { return; } int startingHealth = enemies[enemyIndex].health; enemies[enemyIndex].health = (enemies[enemyIndex].health - (int)ability.Invoke(heroes[currentHeroIndex], new object[] { enemies[enemyIndex] })); GameConsole.currentLog.Append("You hit " + enemies[enemyIndex].name + " for " + (startingHealth - enemies[enemyIndex].health) + " damage\n"); if (enemies[enemyIndex].health < 0) { if (GameConsole.vanquishedEnemies.ContainsKey(enemies[enemyIndex].GetType().Name)) { GameConsole.vanquishedEnemies[enemies[enemyIndex].GetType().Name]++; } else { GameConsole.vanquishedEnemies.Add(enemies[enemyIndex].GetType().Name, 1); } enemies[enemyIndex].OnDeath(); GameConsole.currentLog.Append("You received: "); PrintItem(ItemPool.inventory[ItemPool.inventory.Count - 1]); AddExperience(heroes, enemies[enemyIndex].experienceWorth); enemies.RemoveAt(enemyIndex); } } else // Else we use an ability on an ally { int heroTargetedIndex = GameConsole.GetHeroIndex(commandParts[2]); if (GameConsole.heroes[heroTargetedIndex].health <= 0) { GameConsole.currentLog.Append("Hero is dead. Revive him first \n"); return; } if (!HeroHasMana(heroes[currentHeroIndex], ability)) { return; } if (ability.Name.ToLower().Equals("revive")) // We need to check if the ability is revive since it is a void function and is invoked in a diffrent way { ability.Invoke(heroes[currentHeroIndex], new object[] { heroes[heroTargetedIndex] }); } else { float startingHealth = heroes[heroTargetedIndex].health; heroes[heroTargetedIndex].health = (heroes[heroTargetedIndex].health + (int)ability.Invoke(heroes[currentHeroIndex], null)); if (heroes[heroTargetedIndex].health > heroes[heroTargetedIndex].maxHealth) { heroes[heroTargetedIndex].health = heroes[heroTargetedIndex].maxHealth; } GameConsole.currentLog.Append("You healed " + heroes[heroTargetedIndex].name + " for " + (startingHealth - heroes[heroTargetedIndex].health) + " health\n"); } } GameConsole.heroesActedThisTurn[currentHeroIndex] = true; // If everything passed without an exception then the move was succesful and the current hero has acted } catch (Exception) // Every unregonised commands is basically a different exception { UnrecognizedCommand(); } }
public void Add(Hero hero) { data.Add(hero.Name, hero); }
public static void ExecuteCommand(string command, Hero[] heroes, List<EnemyClass> enemies) { string[] commandParts = command.Split(' '); switch (commandParts[0].ToLower()) //Check if the first word of the command matches any of the basics { case "start": { StartGame(); break; } case "help": { PrintHelp(); break; } case "inventory": { PrintInventory(); break; } case "classes": { PrintClasses(); break; } case "status": { PrintStatus(GameConsole.vanquishedEnemies); break; } case "log": { GameConsole.currentLog.Append(GameConsole.log); break; } case "highscore": { GameConsole.LoadHighScore(); break; } case "music": { if(commandParts[1] == "on") { GameConsole.backgroundMusic.startSong(); } else if(commandParts[1] == "off") { GameConsole.backgroundMusic.stopSong(); } break; } default: // since the code for all heroes is the same we can just put it in default and let the try and catch pick up unregonised commands { try { if(commandParts[0] == "all" && commandParts[1] == "attack") { for (int i = 0; i < heroes.Length; i++) { string attackCommand = heroes[i].name + " attack " + commandParts[2]; ExecuteCommand(attackCommand, heroes, enemies); } break; } if (commandParts[1].Equals("info") && commandParts.Length == 2) //If the second part is info and the command has 2 parts { PrintInfo(heroes, commandParts[0]); break; } else if (commandParts[1].Equals("equip")) { EquipItem(heroes, commandParts); // Check if the command is an ability command and uses it break; } else if (commandParts[1].Equals("unequip")) { UnEquipItem(heroes, commandParts); // Check if the command is an ability command and uses it break; } else { TryToUseAbility(heroes, enemies, commandParts); // Check if the command is an ability command and uses it break; } } catch(Exception) { UnrecognizedCommand(); } break; } } }
public static void ExecuteCommand(string command, Hero[] heroes, List<EnemyClass> enemies) { string[] commandParts = command.Split(' '); switch (commandParts[0].ToLower()) //Check if the first word of the command matches any of the basics { case "start": { StartGame(); break; } case "help": { PrintHelp(); break; } case "inventory": { PrintInventory(); break; } case "classes": { for (int i = 0; i < GameConsole.allHeroes.Count; i++) { PrintInfo(GameConsole.allHeroes.ToArray(), GameConsole.allHeroes[i].name.ToLower()); } break; } case "status": { PrintStatus(GameConsole.vanquishedEnemies); break; } case "log": { GameConsole.currentLog.Append(GameConsole.log); break; } default: // since the code for all heroes is the same we can just put it in default and let the try and catch pick up unregonised commands { try { if (commandParts[1].Equals("info") && commandParts.Length == 2) //If the second part is info and the command has 2 parts { PrintInfo(heroes, commandParts[0]); } else if (commandParts[1].Equals("equip")) { EquipItem(heroes, commandParts); // Check if the command is an ability command and uses it } else if (commandParts[1].Equals("unequip")) { UnEquipItem(heroes, commandParts); // Check if the command is an ability command and uses it } else { TryToUseAbility(heroes, enemies, commandParts); // Check if the command is an ability command and uses it } } catch(Exception) { UnrecognizedCommand(); } break; } } }