jms*_*jms 2 c++ events game-engine
我正在计划一个事件驱动的游戏引擎.基本的想法是,不是让所有事情都与所有事情交谈,一切都会与事件系统进行对话,事件系统会将消息转发给收件人,而不会将收件人与通知者联系起来,反之亦然.
.
class Registration
{
public:
void callback(void){ callback_(); }
void setCallback((*callback)(void));
void addToEventSystem(int ID, EventSystem &eventSystem);
private:
void (*callback_)(void);
};
class EventSystem
{
public:
void register(int ID, Registration* registration);
void unRegister(int ID, Registration* registration);
void addNotificationToQueue(int ID);
void addNotificationToSchedule(int ID, int notificationTime);
void processQueuedNotifications(void);
void processNextScheduled(void);
int getCurrentTime(void);
private:
//placeholder types
<list> notificationQueue;
<binaryheap> notificationSchedule;
};
//------------Use:------------------
class ReceiverObject
{
public:
void doStuff(void);
void initialize(void){
keyPressRegistration.setCallback(doStuff);
//multiple registrations with different ID:s to same eventsystem possible
keyPressRegistration.addToEventSystem(1234,eventSystem);
keyPressRegistration.addToEventSystem(42,eventSystem);};
private:
Registration keyPressRegistration;
};
int main()
{
ReceiverObject receiverObject;
EventSystem eventSystem;
receiverObject.initialize();
eventSystem.addNotificationToQueue(1234);
eventSystem.processQueuedNotifications();
}
Run Code Online (Sandbox Code Playgroud)
但是我对这个解决方案并不完全满意,主要是因为系统不允许将参数轻松传递给收件人,而且我对成员函数的回调持怀疑态度,这是一个很好的设计实践吗?我想出的方法/类/变量名称怎么样?对这个问题的建设性批评,指导和替代方法是值得欢迎的.
它与你的问题没有严格的关系,但谈到设计,我会避免使用全球通知系统"为所有事情",因为我看到过去的不良后果.你只是倾向于在一个对象只能在另一个对象上调用某个方法的地方使用繁重的事件系统.
专业的模板化系统工作得更好,即允许您控制对象寿命的系统,以及用于处理特定类型事件和其中已知参数的系统.
在任何情况下,你都会发现很难解决诸如等待交付给已经被杀死的收件人的待处理事件之类的问题.
| 归档时间: |
|
| 查看次数: |
7578 次 |
| 最近记录: |