type Handler = (...args: unknown[]) => void;

/** Minimal event emitter shared by director & scenes */
export class EventBus {
  private listeners = new Map<string, Set<Handler>>();

  on(event: string, handler: Handler): void {
    if (!this.listeners.has(event)) this.listeners.set(event, new Set());
    this.listeners.get(event)!.add(handler);
  }

  off(event: string, handler: Handler): void {
    this.listeners.get(event)?.delete(handler);
  }

  emit(event: string, ...args: unknown[]): void {
    const set = this.listeners.get(event);
    if (!set) return;
    for (const h of set) h(...args);
  }

  clear(): void {
    this.listeners.clear();
  }
}

export const gameEvents = new EventBus();
