Created
December 15, 2010 13:51
-
-
Save bakkdoor/741957 to your computer and use it in GitHub Desktop.
rough idea.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PluginManager; | |
enum EVENT_TYPE { | |
EVENT_BLOCK_PLACED, | |
EVENT_BLOCK_DESTROYED, | |
EVENT_PLAYER_MOVED | |
// etc... | |
} | |
// base event struct. | |
struct Event | |
{ | |
public: | |
Event(EVENT_TYPE type) | |
: type(type) | |
{ | |
} | |
}; | |
// struct for EVENT_BLOCK_PLACED | |
struct BlockPlacedEvent : Event | |
{ | |
public: | |
BlockPlacedEvent(sint32 x, sint32 y, sint32 z /* other params necessary go here */ ) | |
: Event(EVENT_BLOCK_PLACED) | |
{ | |
} | |
sint32 x, y, z; | |
/* more stuff here */ | |
}; | |
// add more event structs for each event type here ... | |
class Plugin | |
{ | |
public: | |
Plugin(std::vector<EVENT_TYPE> registeredEvents) | |
{ | |
PluginManager::get()->registerPlugin(registeredEvents, this); | |
} | |
void handleEvent(Event* e) | |
{ | |
// either switch over e->type and call the correct on.. methods (see below) | |
// or we don't have those methods at all and simply let the user decide what to do by overriding handleEvent() | |
} | |
// by default do nothing | |
virtual void onBlockPlaced( ... ) {} | |
virtual void onBlockDestroyed( ... ) {} | |
virtual void onPlayerMoved( ... ) {} | |
// more here | |
}; | |
class PluginManager | |
{ | |
public: | |
static PluginManager* get() | |
{ | |
static PluginManager _instance; | |
return &_instance; | |
} | |
void registerPlugin(std::vector<EVENT_TYPE> events, Plugin* p) | |
{ | |
for(std::vector<EVENT_TYPE>::iterator it = events.begin(); | |
it != events.end(); | |
it++) | |
{ | |
m_plugins[*it] = p; | |
} | |
} | |
// unregister either whole plugin or just for specific events.. | |
void unRegisterPlugin(/* ... */) | |
{ | |
} | |
void fireEvent(Event* e) | |
{ | |
std::list<Plugin*> plugins = m_plugins[e->type]; | |
for(std::list<Plugin*>::iterator it = plugins.begin(); it != plugins.end(); it++) | |
{ | |
it->handleEvent(e); | |
} | |
} | |
private: | |
std::map<EVENT_TYPE, std::list<Plugin*> > m_plugins; | |
}; | |
// concrete implementation for plugin writers: | |
// this plugin only listens for EVENT_BLOCK_DESTROYED & EVENT_PLAYER_MOVED | |
// note the call to Plugin's constructor. It registers this instance of a Plugin only for the mentioned events. | |
class MyPlugin : Plugin | |
{ | |
MyPlugin() | |
: Plugin(std::vector(EVENT_BLOCK_DESTROYED, EVENT_PLAYER_MOVED)) | |
{ | |
} | |
// only override the ones we're interested in: | |
void onBlockDestroyed( ... ) | |
{ | |
// do stuff here | |
} | |
void onPlayerMoved( .. ) | |
{ | |
// here too. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Alternatively, we could overload the handleEvent() method for each event type (structs). Dunno.