using System;
using System.Collections.Generic;
namespace Jovian.EncounterSystem {
/// Dispatches instances to per-type handlers. Unknown types are skipped.
public class EncounterResolver {
private readonly Dictionary> handlers = new();
public void Register(Action 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);
}
public void Unregister() where T : IEncounterEvent {
handlers.Remove(typeof(T));
}
public void Resolve(IEnumerable events, EncounterContext context) {
if(events == null) {
return;
}
foreach(var evt in events) {
if(evt == null) {
continue;
}
if(handlers.TryGetValue(evt.GetType(), out var handler)) {
handler(evt, context);
}
}
}
}
}