57 lines
1.2 KiB
JavaScript
57 lines
1.2 KiB
JavaScript
/**
|
|
* Lightweight EventBus for pub/sub communication between systems
|
|
*/
|
|
export const Events = {
|
|
// Combat Events
|
|
DAMAGE_DEALT: 'combat:damage_dealt',
|
|
ENTITY_DIED: 'combat:entity_died',
|
|
|
|
// Evolution Events
|
|
EVOLVED: 'evolution:evolved',
|
|
MUTATION_GAINED: 'evolution:mutation_gained',
|
|
|
|
// Leveling Events
|
|
EXP_GAINED: 'stats:exp_gained',
|
|
LEVEL_UP: 'stats:level_up',
|
|
|
|
// Skill Events
|
|
SKILL_LEARNED: 'skills:learned',
|
|
SKILL_COOLDOWN_STARTED: 'skills:cooldown_started'
|
|
};
|
|
|
|
export class EventBus {
|
|
constructor() {
|
|
this.listeners = new Map();
|
|
}
|
|
|
|
/**
|
|
* Subscribe to an event
|
|
*/
|
|
on(event, callback) {
|
|
if (!this.listeners.has(event)) {
|
|
this.listeners.set(event, []);
|
|
}
|
|
this.listeners.get(event).push(callback);
|
|
return () => this.off(event, callback);
|
|
}
|
|
|
|
/**
|
|
* Unsubscribe from an event
|
|
*/
|
|
off(event, callback) {
|
|
if (!this.listeners.has(event)) return;
|
|
const callbacks = this.listeners.get(event);
|
|
const index = callbacks.indexOf(callback);
|
|
if (index > -1) {
|
|
callbacks.splice(index, 1);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Emit an event
|
|
*/
|
|
emit(event, data) {
|
|
if (!this.listeners.has(event)) return;
|
|
this.listeners.get(event).forEach(callback => callback(data));
|
|
}
|
|
}
|