Added part of the encounter triggering system

This commit is contained in:
Sebastian Bularca
2026-04-27 00:01:15 +02:00
parent f117ddb914
commit ea6c28bfba
28 changed files with 20336 additions and 185 deletions

View File

@@ -1,4 +1,3 @@
using Jovian.EncounterSystem;
using UnityEditor;
using UnityEngine;

View File

@@ -9,6 +9,5 @@ namespace Jovian.EncounterSystem {
public TextMeshProUGUI encounterDescription;
public Image encounterArt;
public Transform encounterOptionsContainer;
public Button submitButton;
}
}

View File

@@ -56,6 +56,73 @@ namespace Jovian.EncounterSystem {
}
}
}
public EncounterTable GetEncounterTable(string id) {
foreach(var collection in encounterCollections) {
foreach(var table in collection.encounterTables) {
if(table.id == id) {
return table;
}
}
}
return null;
}
public IEncounter GetRandomEncounter(string encounterTableId) {
var table = GetEncounterTable(encounterTableId);
if(!table) {
return null;
}
var ec = table.encounters;
return ec[UnityEngine.Random.Range(0, ec.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(string encounterTableId, IEncounterKind encounterKind) {
var table = GetEncounterTable(encounterTableId);
if(!table || encounterKind == null) {
return null;
}
IEncounter selected = null;
var seen = 0;
for(var i = 0; i < encounters.Count; i++) {
var encounter = table.encounters[i];
if(encounter == null || encounter.GetType() != encounterKind.GetType()) {
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, string encounterTableId) {
var table = GetEncounterTable(encounterTableId);
if(!table) {
return null;
}
if(filter == null) {
return GetRandomEncounter(encounterTableId);
}
IEncounter selected = null;
var seen = 0;
foreach(var encounter in table.encounters) {
if(encounter == null || !filter(encounter)) {
continue;
}
seen++;
if(UnityEngine.Random.Range(0, seen) == 0) {
selected = encounter;
}
}
return selected;
}
}
#if UNITY_EDITOR

View File

@@ -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;

View File

@@ -3,11 +3,10 @@ using System;
namespace Jovian.EncounterSystem {
/// <summary>Polymorphic payload on an <see cref="IEncounter"/>. Add a new kind by implementing
/// this interface; the SubclassSelector drawer surfaces it automatically.</summary>
public interface IEncounterKind {
}
public interface IEncounterKind { }
[Serializable]
public partial class QuestKind : IEncounterKind {
public class QuestKind : IEncounterKind {
public EncounterLink nextEncounter;
}
}