Ejemplo n.º 1
0
 public static void PrintInfo(IHero[] heroes, string character)
 {
     int currentHeroIndex = -1;
     for (int i = 0; i < heroes.Length; i++)
     {
         if(character.ToLower().Equals(heroes[i].name.ToLower()))
         {
             currentHeroIndex = i;
             break;
         }
     }
     Type type = heroes[currentHeroIndex].GetType(); // Get the class of the current hero
     FieldInfo[] properties = type.GetFields(BindingFlags.Public | BindingFlags.Instance); // Get all of the fields of the current hero
     GameConsole.currentLog.Append("\n");
     foreach (FieldInfo property in properties)
     {
         Type propertyType = property.GetValue(heroes[currentHeroIndex]).GetType();
         if (propertyType == typeof(ItemPool.Item)) // If the current field is an item we need to print it diffrently
         {
             ItemPool.Item item = FindItem(property.GetValue(heroes[currentHeroIndex]), propertyType);
             GameConsole.currentLog.Append("\t" + property.Name + ": ");
             PrintItem(item);
         }
         else
         {
             GameConsole.currentLog.Append("\t" + property.Name + ": " + property.GetValue(heroes[currentHeroIndex]).ToString() + "\n");
         }
     }
     var abilities = type.GetFields(BindingFlags.Public | BindingFlags.Static);
     GameConsole.currentLog.Append("\tAbilities: \n");
     foreach (var ability in abilities)
     {
         Type abilityType = ability.GetValue(abilities).GetType();
         var abilityInfos = abilityType.GetFields();
         GameConsole.currentLog.Append("\t\t|" + ability.Name + " | ");
         foreach (var abilityInfo in abilityInfos)
         {
             GameConsole.currentLog.Append(abilityInfo.Name + ": " + abilityInfo.GetValue(ability.GetValue(abilities)).ToString() + " | ");
         }
         GameConsole.currentLog.Append("\n");
     }
     GameConsole.currentLog.Append("\n");
 }
Ejemplo n.º 2
0
 private static void AIAction()
 {
     for (int i = 0; i < enemies.Count; i++)
     {
         Random random = new Random();
         int randomHeroIndex;
         do
         {
             randomHeroIndex = random.Next(0, heroes.Length);
         }
         while (heroes[randomHeroIndex].health <= 0); //Keep choosing a new random hero if the random hero we are attacking is dead
         var methodNames = enemies[i].GetType().GetMethods(BindingFlags.Public | BindingFlags.Instance |
             BindingFlags.DeclaredOnly).Select(x => x.Name).Distinct().OrderBy(x => x); // We take all of the methods the current enemy has that have not been inherited remove duplicates and order them
         List<MethodInfo> abilities = new List<MethodInfo>(); //The abilities the enemy has
         abilities.Add(enemies[i].GetType().GetMethod("attack")); //All enemies have attack by default
         foreach (var methodName in methodNames)
         {
             abilities.Add(enemies[i].GetType().GetMethod(methodName.ToLower())); //Add the method to the ability list
             int currentMana = enemies[i].mana;
             abilities[abilities.Count - 1].Invoke(enemies[i], new object[] { heroes[randomHeroIndex] }); //Try to use the ability by also draining mana
             if (enemies[i].mana < 0) //If after the enemy has used the ability the current enemy has less than 0 mana then he doesn't have mana for that ability
             {
                 abilities.RemoveAt(abilities.Count - 1); //We remove the ability from the list
             }
             enemies[i].mana = currentMana;
         }
         int randomAbilityIndex = random.Next(0, abilities.Count); //Use a random ability from the list
         float startingHealth = heroes[randomHeroIndex].health;
         IHero test = new IHero();
         heroes[randomHeroIndex].health = heroes[randomHeroIndex].health - //Reduce the hero's health with the damage from the ability
             (int)abilities[randomAbilityIndex].Invoke(enemies[i], new object[] { heroes[randomHeroIndex] }); //Invoke the ability which will return an int value of the damage it deals
         currentLog.Append(enemies[i].name + " hits " + heroes[randomHeroIndex].name + " for " + (startingHealth - heroes[randomHeroIndex].health) + " damage\n");
     }
 }