Files
encounter-system/Runtime/EncounterTable.cs
2026-04-19 12:25:49 +02:00

64 lines
2.8 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
namespace Jovian.EncounterSystem {
/// <summary>
/// A ScriptableObject asset holding a named list of encounters. Encounters inside the list are
/// authored via <c>[SerializeReference]</c> so different <see cref="IEncounter"/> implementations
/// and <see cref="IEncounterKind"/> payloads can coexist.
/// </summary>
[CreateAssetMenu(fileName = "EncounterTable", menuName = "Jovian/Encounter System/Encounter Table", order = 1)]
public class EncounterTable : ScriptableObject {
/// <summary>Designer-facing table identifier (free-form).</summary>
public string id;
/// <summary>Ordered encounter list. Each element is a concrete <see cref="Encounter"/> whose
/// type-specific payload is carried by its <see cref="IEncounterKind"/> (set in the inspector
/// via the SubclassSelector dropdown on <see cref="Encounter.Kind"/>).</summary>
public List<Encounter> encounters;
/// <summary>Pick a uniformly random encounter, or <c>null</c> if the table is empty.</summary>
public IEncounter GetRandomEncounter() {
if(encounters == null || encounters.Count == 0) {
return null;
}
return encounters[UnityEngine.Random.Range(0, encounters.Count)];
}
/// <summary>Pick a uniformly random encounter whose runtime type matches <paramref name="type"/>,
/// or <c>null</c> if none match.</summary>
/// <param name="type">The concrete <see cref="IEncounter"/> runtime type to filter on.</param>
public IEncounter GetRandomEncounter(Type type) {
if(encounters == null || encounters.Count == 0) {
return null;
}
var encountersOfType = encounters.FindAll(encounter => encounter.GetType() == type);
if(encountersOfType.Count > 0) {
return encountersOfType[UnityEngine.Random.Range(0, encountersOfType.Count)];
}
return null;
}
/// <summary>Pick a uniformly random encounter matching <paramref name="filter"/>, or <c>null</c>
/// if no element passes. Used by <see cref="QuestProgress.IsGated"/> integration to exclude
/// gated quest encounters from rolls.</summary>
/// <param name="filter">Predicate applied to each encounter before rolling.</param>
public IEncounter GetRandomEncounter(Predicate<IEncounter> filter) {
if(encounters == null || encounters.Count == 0 || filter == null) {
return GetRandomEncounter();
}
var pool = encounters.FindAll(filter);
if(pool.Count == 0) {
return null;
}
return pool[UnityEngine.Random.Range(0, pool.Count)];
}
}
}