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>
|
/// <summary>Per-resolution scratch object passed to every event handler. Extend with fields as handlers need them.</summary>
|
||||||
public class EncounterContext {
|
public class EncounterContext {
|
||||||
public IEncounter CurrentEncounter { get; }
|
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) {
|
public EncounterContext(IEncounter currentEncounter) {
|
||||||
CurrentEncounter = currentEncounter;
|
CurrentEncounter = currentEncounter;
|
||||||
|
|||||||
@@ -11,7 +11,9 @@ namespace Jovian.EncounterSystem {
|
|||||||
|
|
||||||
private readonly Dictionary<string, IEncounter> encounters = new();
|
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) {
|
public void RegisterEncounter(IEncounter encounter) {
|
||||||
var id = encounter?.EncounterDefinition?.internalId;
|
var id = encounter?.EncounterDefinition?.internalId;
|
||||||
@@ -38,19 +40,19 @@ namespace Jovian.EncounterSystem {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
for(int c = 0; c < encounterCollections.Length; c++) {
|
for(var c = 0; c < encounterCollections.Length; c++) {
|
||||||
var collection = encounterCollections[c];
|
var collection = encounterCollections[c];
|
||||||
if(collection?.encounterTables == null) {
|
if(collection?.encounterTables == null) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
for(int t = 0; t < collection.encounterTables.Length; t++) {
|
for(var t = 0; t < collection.encounterTables.Length; t++) {
|
||||||
var table = collection.encounterTables[t];
|
var table = collection.encounterTables[t];
|
||||||
if(table?.encounters == null) {
|
if(table?.encounters == null) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
for(int i = 0; i < table.encounters.Count; i++) {
|
for(var i = 0; i < table.encounters.Count; i++) {
|
||||||
RegisterEncounter(table.encounters[i]);
|
RegisterEncounter(table.encounters[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -86,7 +88,7 @@ namespace Jovian.EncounterSystem {
|
|||||||
|
|
||||||
IEncounter selected = null;
|
IEncounter selected = null;
|
||||||
var seen = 0;
|
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];
|
var encounter = table.encounters[i];
|
||||||
if(encounter == null || encounter.GetType() != encounterKind.GetType()) {
|
if(encounter == null || encounter.GetType() != encounterKind.GetType()) {
|
||||||
continue;
|
continue;
|
||||||
|
|||||||
@@ -2,13 +2,12 @@ using System;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace Jovian.EncounterSystem {
|
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 {
|
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 {
|
public void Register<T>(Func<T, EncounterContext, bool> 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).
|
|
||||||
handlers[typeof(T)] = (evt, ctx) => handler((T)evt, ctx);
|
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
|
/// <summary>Indexed iteration over <paramref name="events"/> — avoids the boxed enumerator
|
||||||
/// that an <c>IEnumerable<T></c> parameter would force. <see cref="EncounterDialogOption.events"/>
|
/// that an <c>IEnumerable<T></c> parameter would force. Stops when a handler
|
||||||
/// is a <c>List</c>, which implements <c>IReadOnlyList</c>, so call sites just pass it directly.</summary>
|
/// returns <c>true</c> or <see cref="EncounterContext.ShouldStopEvents"/> is set.</summary>
|
||||||
public void Resolve(IReadOnlyList<IEncounterEvent> events, EncounterContext context) {
|
public void Resolve(IReadOnlyList<IEncounterEvent> events, EncounterContext context) {
|
||||||
if(events == null) {
|
if(events == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var count = events.Count;
|
var count = events.Count;
|
||||||
for(int i = 0; i < count; i++) {
|
for(var i = 0; i < count; i++) {
|
||||||
var evt = events[i];
|
var evt = events[i];
|
||||||
if(evt == null) {
|
if(evt == null || context.ShouldStopEvents) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(handlers.TryGetValue(evt.GetType(), out var handler)) {
|
if(!handlers.TryGetValue(evt.GetType(), out var handler)) {
|
||||||
handler(evt, context);
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(handler(evt, context)) {
|
||||||
|
context.ShouldStopEvents = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,5 +49,6 @@ namespace Jovian.EncounterSystem {
|
|||||||
[Serializable]
|
[Serializable]
|
||||||
public class EncounterProperties {
|
public class EncounterProperties {
|
||||||
public int difficulty;
|
public int difficulty;
|
||||||
|
public bool isRepeatable = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,4 +24,8 @@ namespace Jovian.EncounterSystem {
|
|||||||
public class GiveRewardEvent : IEncounterEvent {
|
public class GiveRewardEvent : IEncounterEvent {
|
||||||
public Reward reward;
|
public Reward reward;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Serializable]
|
||||||
|
public class AbandonEncounterEvent : IEncounterEvent {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ namespace Jovian.EncounterSystem {
|
|||||||
public event Action<IEncounter> QuestStarted;
|
public event Action<IEncounter> QuestStarted;
|
||||||
public event Action<IEncounter, IEncounter> QuestAdvanced;
|
public event Action<IEncounter, IEncounter> QuestAdvanced;
|
||||||
public event Action<IEncounter> QuestCompleted;
|
public event Action<IEncounter> QuestCompleted;
|
||||||
|
public event Action<IEncounter> QuestAbandoned;
|
||||||
|
|
||||||
public IReadOnlyCollection<string> ResolvedIds => resolvedIds;
|
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) {
|
public void LoadResolvedIds(IEnumerable<string> ids) {
|
||||||
resolvedIds.Clear();
|
resolvedIds.Clear();
|
||||||
if(ids == null) {
|
if(ids == null) {
|
||||||
|
|||||||
Reference in New Issue
Block a user