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)); } /// Indexed iteration over — avoids the boxed enumerator /// that an IEnumerable<T> parameter would force. /// is a List, which implements IReadOnlyList, so call sites just pass it directly. public void Resolve(IReadOnlyList events, EncounterContext context) { if(events == null) { return; } var count = events.Count; for(int i = 0; i < count; i++) { var evt = events[i]; if(evt == null) { continue; } if(handlers.TryGetValue(evt.GetType(), out var handler)) { handler(evt, context); } } } } }