public Monster GetMonster() { if (!MonstersHere.Any()) { return(null); } //Total percentage od svih monstera u lokaciji int totalChances = MonstersHere.Sum(m => m.ChanceOfEncountering); //Selectaj random number izmedu 1 i percentage monstera u lokaciji int randomNumber = RandomNumberGenerator.NumberBetween(1, totalChances); int runningTotal = 0; foreach (MonsterEncounter monsterEncounter in MonstersHere) { runningTotal += monsterEncounter.ChanceOfEncountering; if (randomNumber <= runningTotal) { return(MonsterFactory.GetMonster(monsterEncounter.MonsterID)); } } return(MonsterFactory.GetMonster(MonstersHere.Last().MonsterID)); }
public Monster GetMonster() { if (!MonstersHere.Any()) { return(null); } //Total percentages of all monster encounter chances here int totalChances = MonstersHere.Sum(m => m.ChanceOfEncountering); //select random number between 1 and total int randomNumber = RandomNumberGenerator.NumberBetween(1, totalChances); //Iterate through monster list //Create a running total of monster percentages //Once the random number < running total //return the monster iterator is currently on int runningTotal = 0; foreach (MonsterEncounter monsterEncounter in MonstersHere) { runningTotal += monsterEncounter.ChanceOfEncountering; if (randomNumber <= runningTotal) { return(MonsterFactory.GetMonster(monsterEncounter.MonsterID)); } } //If something happened then return the last monster in the list return(MonsterFactory.GetMonster(MonstersHere.Last().MonsterID)); }