I have been puttering around with the fundamentals of a Java game engine, and I've reached the point where I'm ready to add in an Event Manager system.
I know, in theory, what an Event Manager should do: allow objects to "register" for certain events, and whenever the Event Manager gets notified of an event, broadcast the event to the "registered" listeners. What I'm stumped on is how to start implementing it.
I haven't been able to find anything, online, about implementing an event system from scratch, so I'm looking for input on what the best practices are in this case - what I should and should not be doing.
For instance, is it really necessary for each of my game objects to have an EventManager
field? Since all of my game objects inherit from a single, abstract parent class, I think I should be able to use a static reference so that there's only one instance of the Event Manager, shared between all the game objects. I do something similar, with the Applet, that I use to render each object, already.
I suppose I'd have to maintain a collection of some sort for each possible subscribed event - adding and removing game objects from the list, as needed. I think it should be possible to make a queue of events that need to be broadcast, in which case I could simply add "EventManager.Update()" to the main game loop, and have the Update()
method broadcast the events that occurred at the end of each frame. Finally, each object would have a HandleEvent(Event e)
method, that they could then parse and respond to, appropriately.
Does this sound like the appropriate direction towards implementing such a system, or am I way off track and/or missing something quite obvious?