mirror of
https://github.com/cliffe/BreakEscape.git
synced 2026-02-20 13:50:46 +00:00
- Created a generic NPC script with conversation handling. - Developed an Alice NPC script demonstrating branching dialogue and state tracking. - Implemented a test NPC script for development purposes. - Added JSON representations for the NPC scripts. - Created an HTML test interface for NPC integration testing. - Included event handling and bark systems for NPC interactions.
37 lines
1.3 KiB
JavaScript
37 lines
1.3 KiB
JavaScript
// Minimal event dispatcher for NPC events
|
|
// Exports default class NPCEventDispatcher with .on(pattern, cb) and .emit(type, data)
|
|
export default class NPCEventDispatcher {
|
|
constructor(opts = {}) {
|
|
this.debug = !!opts.debug;
|
|
this.listeners = new Map(); // map eventType -> [callbacks]
|
|
}
|
|
|
|
on(eventType, cb) {
|
|
if (!eventType || typeof cb !== 'function') return;
|
|
if (!this.listeners.has(eventType)) this.listeners.set(eventType, []);
|
|
this.listeners.get(eventType).push(cb);
|
|
}
|
|
|
|
off(eventType, cb) {
|
|
if (!this.listeners.has(eventType)) return;
|
|
if (!cb) { this.listeners.delete(eventType); return; }
|
|
const arr = this.listeners.get(eventType).filter(f => f !== cb);
|
|
this.listeners.set(eventType, arr);
|
|
}
|
|
|
|
emit(eventType, data) {
|
|
if (this.debug) console.log('[NPCEventDispatcher] emit', eventType, data);
|
|
// exact-match listeners
|
|
const exact = this.listeners.get(eventType) || [];
|
|
for (const fn of exact) try { fn(data); } catch (e) { console.error(e); }
|
|
|
|
// wildcard-style listeners where eventType is a prefix (e.g. 'npc:')
|
|
for (const [key, arr] of this.listeners.entries()) {
|
|
if (key.endsWith('*')) {
|
|
const prefix = key.slice(0, -1);
|
|
if (eventType.startsWith(prefix)) for (const fn of arr) try { fn(data); } catch (e) { console.error(e); }
|
|
}
|
|
}
|
|
}
|
|
}
|