Fxed an issue with random encounters, added a repeatable property, add event for abandoning encounters
This commit is contained in:
@@ -2,6 +2,10 @@ namespace Jovian.EncounterSystem {
|
||||
/// <summary>Per-resolution scratch object passed to every event handler. Extend with fields as handlers need them.</summary>
|
||||
public class EncounterContext {
|
||||
public IEncounter CurrentEncounter { get; }
|
||||
public EncounterRegistry Registry { get; set; }
|
||||
|
||||
/// <summary>Set to <c>true</c> to halt processing of remaining events.</summary>
|
||||
public bool ShouldStopEvents { get; set; }
|
||||
|
||||
public EncounterContext(IEncounter currentEncounter) {
|
||||
CurrentEncounter = currentEncounter;
|
||||
|
||||
@@ -11,7 +11,9 @@ namespace Jovian.EncounterSystem {
|
||||
|
||||
private readonly Dictionary<string, IEncounter> encounters = new();
|
||||
|
||||
public Dictionary<string, IEncounter> GetEncounters() => encounters;
|
||||
public Dictionary<string, IEncounter> GetEncounters() {
|
||||
return encounters;
|
||||
}
|
||||
|
||||
public void RegisterEncounter(IEncounter encounter) {
|
||||
var id = encounter?.EncounterDefinition?.internalId;
|
||||
@@ -38,19 +40,19 @@ namespace Jovian.EncounterSystem {
|
||||
return;
|
||||
}
|
||||
|
||||
for(int c = 0; c < encounterCollections.Length; c++) {
|
||||
for(var c = 0; c < encounterCollections.Length; c++) {
|
||||
var collection = encounterCollections[c];
|
||||
if(collection?.encounterTables == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for(int t = 0; t < collection.encounterTables.Length; t++) {
|
||||
for(var t = 0; t < collection.encounterTables.Length; t++) {
|
||||
var table = collection.encounterTables[t];
|
||||
if(table?.encounters == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for(int i = 0; i < table.encounters.Count; i++) {
|
||||
for(var i = 0; i < table.encounters.Count; i++) {
|
||||
RegisterEncounter(table.encounters[i]);
|
||||
}
|
||||
}
|
||||
@@ -67,12 +69,12 @@ namespace Jovian.EncounterSystem {
|
||||
return null;
|
||||
}
|
||||
|
||||
public IEncounter GetRandomEncounter(string encounterTableId) {
|
||||
var table = GetEncounterTable(encounterTableId);
|
||||
if(!table) {
|
||||
return null;
|
||||
}
|
||||
var ec = table.encounters;
|
||||
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)];
|
||||
}
|
||||
|
||||
@@ -86,7 +88,7 @@ namespace Jovian.EncounterSystem {
|
||||
|
||||
IEncounter selected = null;
|
||||
var seen = 0;
|
||||
for(var i = 0; i < encounters.Count; i++) {
|
||||
for(var i = 0; i < table.encounters.Count; i++) {
|
||||
var encounter = table.encounters[i];
|
||||
if(encounter == null || encounter.GetType() != encounterKind.GetType()) {
|
||||
continue;
|
||||
|
||||
@@ -2,13 +2,12 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Jovian.EncounterSystem {
|
||||
/// <summary>Dispatches <see cref="IEncounterEvent"/> instances to per-type handlers. Unknown types are skipped.</summary>
|
||||
/// <summary>Dispatches <see cref="IEncounterEvent"/> instances to per-type handlers.
|
||||
/// Handlers return <c>true</c> to stop processing remaining events.</summary>
|
||||
public class EncounterResolver {
|
||||
private readonly Dictionary<Type, Action<IEncounterEvent, EncounterContext>> handlers = new();
|
||||
private readonly Dictionary<Type, Func<IEncounterEvent, EncounterContext, bool>> handlers = new();
|
||||
|
||||
public void Register<T>(Action<T, EncounterContext> handler) where T : IEncounterEvent {
|
||||
// Wrap the typed delegate so the dictionary can hold handlers for any event type uniformly.
|
||||
// Cast is safe because the wrapper is only invoked via lookup under typeof(T).
|
||||
public void Register<T>(Func<T, EncounterContext, bool> handler) where T : IEncounterEvent {
|
||||
handlers[typeof(T)] = (evt, ctx) => handler((T)evt, ctx);
|
||||
}
|
||||
|
||||
@@ -17,22 +16,26 @@ namespace Jovian.EncounterSystem {
|
||||
}
|
||||
|
||||
/// <summary>Indexed iteration over <paramref name="events"/> — avoids the boxed enumerator
|
||||
/// that an <c>IEnumerable<T></c> parameter would force. <see cref="EncounterDialogOption.events"/>
|
||||
/// is a <c>List</c>, which implements <c>IReadOnlyList</c>, so call sites just pass it directly.</summary>
|
||||
/// that an <c>IEnumerable<T></c> parameter would force. Stops when a handler
|
||||
/// returns <c>true</c> or <see cref="EncounterContext.ShouldStopEvents"/> is set.</summary>
|
||||
public void Resolve(IReadOnlyList<IEncounterEvent> events, EncounterContext context) {
|
||||
if(events == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
var count = events.Count;
|
||||
for(int i = 0; i < count; i++) {
|
||||
for(var i = 0; i < count; i++) {
|
||||
var evt = events[i];
|
||||
if(evt == null) {
|
||||
if(evt == null || context.ShouldStopEvents) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if(handlers.TryGetValue(evt.GetType(), out var handler)) {
|
||||
handler(evt, context);
|
||||
if(!handlers.TryGetValue(evt.GetType(), out var handler)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if(handler(evt, context)) {
|
||||
context.ShouldStopEvents = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,8 +46,9 @@ namespace Jovian.EncounterSystem {
|
||||
public Sprite encounterArt;
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
[Serializable]
|
||||
public class EncounterProperties {
|
||||
public int difficulty;
|
||||
public bool isRepeatable = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,4 +24,8 @@ namespace Jovian.EncounterSystem {
|
||||
public class GiveRewardEvent : IEncounterEvent {
|
||||
public Reward reward;
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class AbandonEncounterEvent : IEncounterEvent {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ namespace Jovian.EncounterSystem {
|
||||
public event Action<IEncounter> QuestStarted;
|
||||
public event Action<IEncounter, IEncounter> QuestAdvanced;
|
||||
public event Action<IEncounter> QuestCompleted;
|
||||
public event Action<IEncounter> QuestAbandoned;
|
||||
|
||||
public IReadOnlyCollection<string> ResolvedIds => resolvedIds;
|
||||
|
||||
@@ -76,6 +77,19 @@ namespace Jovian.EncounterSystem {
|
||||
}
|
||||
}
|
||||
|
||||
public void OnEncounterAbandoned(IEncounter encounter) {
|
||||
if(encounter?.EncounterDefinition.Kind is not QuestKind questKind) {
|
||||
return;
|
||||
}
|
||||
|
||||
var id = encounter.EncounterDefinition?.internalId;
|
||||
if(string.IsNullOrEmpty(id) || !resolvedIds.Remove(id)) {
|
||||
return;
|
||||
}
|
||||
|
||||
QuestAbandoned?.Invoke(encounter);
|
||||
}
|
||||
|
||||
public void LoadResolvedIds(IEnumerable<string> ids) {
|
||||
resolvedIds.Clear();
|
||||
if(ids == null) {
|
||||
|
||||
Reference in New Issue
Block a user