Added encounter registry
This commit is contained in:
@@ -26,61 +26,6 @@ namespace Jovian.EncounterSystem {
|
||||
idCache = null;
|
||||
}
|
||||
|
||||
public IEncounter GetRandomEncounter() {
|
||||
if(encounters == null || encounters.Count == 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return encounters[UnityEngine.Random.Range(0, encounters.Count)];
|
||||
}
|
||||
|
||||
/// <summary>Random pick restricted to encounters whose runtime type matches <paramref name="type"/>.
|
||||
/// Zero-alloc — uses reservoir sampling instead of building an intermediate filtered list.</summary>
|
||||
public IEncounter GetRandomEncounter(Type type) {
|
||||
if(encounters == null || encounters.Count == 0 || type == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
IEncounter selected = null;
|
||||
var seen = 0;
|
||||
for(var i = 0; i < encounters.Count; i++) {
|
||||
var encounter = encounters[i];
|
||||
if(encounter == null || encounter.GetType() != type) {
|
||||
continue;
|
||||
}
|
||||
seen++;
|
||||
if(UnityEngine.Random.Range(0, seen) == 0) {
|
||||
selected = encounter;
|
||||
}
|
||||
}
|
||||
return selected;
|
||||
}
|
||||
|
||||
/// <summary>Random pick restricted by <paramref name="filter"/>. Used with
|
||||
/// <see cref="QuestProgress.IsGated"/> to exclude gated encounters. Zero-alloc via reservoir sampling.</summary>
|
||||
public IEncounter GetRandomEncounter(Predicate<IEncounter> filter) {
|
||||
if(encounters == null || encounters.Count == 0) {
|
||||
return null;
|
||||
}
|
||||
if(filter == null) {
|
||||
return GetRandomEncounter();
|
||||
}
|
||||
|
||||
IEncounter selected = null;
|
||||
var seen = 0;
|
||||
for(var i = 0; i < encounters.Count; i++) {
|
||||
var encounter = encounters[i];
|
||||
if(encounter == null || !filter(encounter)) {
|
||||
continue;
|
||||
}
|
||||
seen++;
|
||||
if(UnityEngine.Random.Range(0, seen) == 0) {
|
||||
selected = encounter;
|
||||
}
|
||||
}
|
||||
return selected;
|
||||
}
|
||||
|
||||
private void EnsureCache() {
|
||||
if(idCache != null) {
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user