public Monster GetMonster() { if (!MonsterHere.Any()) { return(null); } // Total the percentages of all monsters at this location int totalChances = MonsterHere.Sum(m => m.ChanceOfEncounter); // Select a random number between 1 and the total (in case the total //chance is not 100 int randomNumber = RandomNumberGenerator.NumberBetween(1, totalChances); // Loop through the monster list // adding the monster's percentage chance of operating to the runningTotal variable // When the random number is lower than the runningTotal // That is the monster to return int runningTotal = 0; foreach (MonsterEncounter monsterEncounter in MonsterHere) { runningTotal += monsterEncounter.ChanceOfEncounter; if (randomNumber <= runningTotal) { return(MonsterFactory.GetMonster(monsterEncounter.MonsterID)); } } // If there was a problem, return the last monster in the list. return(MonsterFactory.GetMonster(MonsterHere.Last().MonsterID)); }
public Monster GetMonster() { if (!MonsterHere.Any()) { return(null); } int totalChances = MonsterHere.Sum(m => m.ChanceOfEncountering); int randomNumber = RandomNumberGenerator.NumberBetween(1, totalChances); int runningTotal = 0; foreach (MonsterEncounter monsterEncounter in MonsterHere) { runningTotal += monsterEncounter.ChanceOfEncountering; if (randomNumber <= runningTotal) { return(MonsterFactory.GetMonster(monsterEncounter.MonsterID)); } } return(MonsterFactory.GetMonster(MonsterHere.Last().MonsterID)); }
public Monster GetMonster() { if (!MonsterHere.Any()) { return(null); } // Procenten af monster på given location int totalChances = MonsterHere.Sum(m => m.ChanceOfEncountering); // Finder et nummer mellem 1 og maks int randomNumber = RandomNumberGenerator.NumberBetween(1, totalChances); int runningTotal = 0; foreach (MonsterEncounter monsterEncounter in MonsterHere) { runningTotal += monsterEncounter.ChanceOfEncountering; if (randomNumber <= runningTotal) { return(MonsterFactory.GetMonster(monsterEncounter.MonsterID)); } } // Hvis der er en fejl i listen, giver den sidste kendte id return(MonsterFactory.GetMonster(MonsterHere.Last().MonsterID)); }