public Enemy GetEnemy() { // If the list is empty return null if (!EnemiesHere.Any()) { return(null); } // Add up encounter chance of every enemy in the locations int totalChance = EnemiesHere.Sum(e => e.EncounterChance); // Select random number from 1 to total encounter chance based off all enemies in location int random = RNG.Generator(1, totalChance); int runningTotal = 0; // Get to understand this more... foreach (EnemyEncounter enemyEncounter in EnemiesHere) { runningTotal += enemyEncounter.EncounterChance; if (random <= runningTotal) { return(EnemyFactory.CreateEnemy(enemyEncounter.EnemyID)); } } // If ID couldn't be found, return the last correct ID return(EnemyFactory.CreateEnemy(EnemiesHere.Last().EnemyID)); }
// Dont get this too well public void AddEnemy(int enemyID, int encounterChance) { // Get the first matching ID of enemy if (EnemiesHere.Exists(e => e.EnemyID == enemyID)) { // If enemy of this ID exists, override the encounter chance EnemiesHere.First(e => e.EnemyID == enemyID).EncounterChance = encounterChance; } else { // If enemy of this ID does not exist, add it to the property EnemiesHere.Add(new EnemyEncounter(enemyID, encounterChance)); } }