七夜狐

博客园 首页 新随笔 联系 订阅 管理

  这一节写客户端的具体实现方式。

  为了演示,代码写的比较粗糙。

  我们用一个EventCenter作为事件ID和事件的路由表,即根据某ID,即可定位到某处理事件的具体类,并根据参数处理这个事件。

   前台应该有个类似这样的配置:

   <group id = 0>

      <event id =xx paramint1=xx paramint2=xx  paramstr= xx>

      ....

   </group >

   <group id  = 1>

  </group>

   我简单处理成了直接赋值,关于数据结构的定义,都较简单的使用了map,实际工作中,可根据情况做相应处理,这里仅说明一下大体原理

 

#include<map>
using namespace std;
struct Event
{
	int eventid;
	string parastr;
	int    paraint1;
	int	   paraint2;
};
struct Group
{
	enum { MAX_EVENT = 5};
	int eventnum ;
	Event events[MAX_EVENT];	
};
struct EventCenter;
class Client
{
public:
	enum EventType {
		EVENT_NOTICE = 0, //弹提示
		EVENT_CAMERA_MOVE =1 ,//移镜头
		EVENT_TRANSPORT = 2 //传送

	};
public:
	Client(void);
	~Client(void);
public:
	int loadfile();
	int run();
public:
	void setgroupid(int id ){ groupid = id;};
	void setcenter(EventCenter* c){ center = c;}

private:
	typedef map<int,Group> Groups;
	typedef map<int,Group>::iterator Iterator;
	Groups config;
	int	groupid;
	EventCenter* center;
};
//事件类的父类,通过虚特性,EventCenter将调用子类的handle
class EventHandle
{
public:
	virtual int handle(Event* event)=0;
};
//将事件陆由到各个事件处理类
struct EventCenter
{
	enum { MAX = 3};
public:
	void registerHandle(int id ,EventHandle* event){
		eventhandles[id] = event;
	}
	void handleEvent(int id,Event* e ){
		eventhandles[id]->handle(e);;
	}
private:
	EventHandle* eventhandles[MAX];//为了演示方便,这个路由表简单表述成数组下标为键的结构
};
///具体处理事件的类,可无限添加,只要在EventCenter中注册即可
//提示类
class NoticeEvent: public EventHandle
{
	int handle(Event* event){
		printf("新手指导员: %s\n",event->parastr.c_str());
		return 0;
	}
};
//移镜头
class MoveCameraEvent: public EventHandle
{
	int handle(Event* event){
		printf("镜头移动到x=%d   y=%d\n",event->paraint1,event->paraint2);
		return 0;
	}
};

//传送
class TransportEvent: public EventHandle
{
	int handle(Event* event){
		printf("将玩家传送至 场景 %d \n",event->paraint1);
		return 0;
	}
};

  

 

 1 #include "StdAfx.h"
 2 #include "Client.h"
 3 #include "luainc.h"
 4 
 5 Client::Client(void)
 6 {
 7 }
 8 
 9 
10 Client::~Client(void)
11 {
12 }
13 int Client::run()
14 {
15     Iterator it = config.find(groupid);
16 
17     if( it != config.end() ){
18         Group g = it->second;
19         int eventid = -1;
20         Event ev ;;
21         for(int i = 0; i < g.eventnum; i++ ){
22             eventid = g.events[i].eventid;
23             ev = g.events[i];
24             center->handleEvent(eventid,&ev);
25         }
26     }
27     return 0;
28 }
29 int Client::loadfile()
30 {
31     Group group0;
32     group0.events[0].eventid = EVENT_NOTICE;
33     group0.events[0].parastr = "少侠,欢迎来到新手村,最近野猪一直在骚扰村子,你去打几只吧";
34     
35     group0.events[1].eventid = EVENT_CAMERA_MOVE;
36     group0.events[1].paraint1 = 100;
37     group0.events[1].paraint2 = 200;
38     group0.eventnum = 2;
39 
40     Group group1;
41     group1.eventnum = 1;
42     group1.events[0].eventid = EVENT_TRANSPORT;
43     group1.events[0].paraint1 = 211314;
44 
45     config.insert(pair<int,Group>(0,group0));
46     config.insert(pair<int,Group>(1,group1));
47 
48     return 0;
49 }

 

posted on 2012-08-17 17:50  七夜狐  阅读(351)  评论(0编辑  收藏  举报