/****************************** * 文件名: Game_Define.h * 创建人: 陈泽丹/Clark * 创建时间: 20120330 * 文件描述: ******************************/ #pragma once namespace GAME_DEFINE { struct ENTITY_TYPE { enum { BASE_ENTITY = 0, }; }; }
/****************************** * 文件名: Res_Interface.h * 创建人: 陈泽丹/Clark * 创建时间: 20120330 * 文件描述: ******************************/ #pragma once #include <iostream> #include <map> #include "Entity_Interface.h" using namespace std; template<int TYPE_VAL> class Res_Interface { public: Res_Interface(const int _RES_ID):RES_ID(_RES_ID){ Clear(); } virtual ~Res_Interface(){ Clear(); } bool Login(int _UID, Entity_Interface<TYPE_VAL>* _pEntity) { if( 0 == m_vpEntitys[_UID]) { m_vpEntitys[_UID] = _pEntity; return true; } return false; } void Logout(int _UID) { m_vpEntitys[_UID] = 0; } void Clear() { for(map< int, Entity_Interface<TYPE_VAL>* >::iterator it = m_vpEntitys.begin(); it != m_vpEntitys.end(); it++) { if( 0 != it->second) { delete it->second; it->second = 0; } } m_vpEntitys.clear(); } int GetSize() { return m_vpEntitys.size(); } Entity_Interface<TYPE_VAL>* GetEntity(int _UID) { return m_vpEntitys[_UID]; } const int RES_ID; protected: map< int, Entity_Interface<TYPE_VAL>* > m_vpEntitys; };
/****************************** * 文件名: Entity_Interface.h * 创建人: 陈泽丹/Clark * 创建时间: 20120330 * 文件描述: ******************************/ #pragma once template<int TYPE_VAL> class Entity_Interface { public: virtual ~Entity_Interface(){} };
/****************************** * 文件名: BaseNPC_Entity.h * 创建人: 陈泽丹/Clark * 创建时间: 20120330 * 文件描述: ******************************/ #pragma once #include "../Game_Define.h" #include "../Entity_Interface.h" #include "../Res_Interface.h" using namespace GAME_DEFINE; template<> class Entity_Interface<ENTITY_TYPE::BASE_ENTITY> { public: void ToRes(Res_Interface<ENTITY_TYPE::BASE_ENTITY>* _pDestRes, const int _iPosX = 0, const int _iPosY = 0); const int UID; protected: Entity_Interface(int _UID, Res_Interface<ENTITY_TYPE::BASE_ENTITY>* _pRes); virtual ~Entity_Interface(); int m_iPosX; int m_iPosY; Res_Interface<ENTITY_TYPE::BASE_ENTITY>* m_pRes; friend class Res_Interface<ENTITY_TYPE::BASE_ENTITY>; };
#include "BaseNPC_Entity.h" Entity_Interface<ENTITY_TYPE::BASE_ENTITY>::Entity_Interface(int _UID, Res_Interface<ENTITY_TYPE::BASE_ENTITY>* _pRes): UID(_UID), m_pRes(_pRes), m_iPosX(0), m_iPosY(0) { m_pRes->Login( UID, this ); } Entity_Interface<ENTITY_TYPE::BASE_ENTITY>::~Entity_Interface() { m_pRes->Logout( UID ); }; void Entity_Interface<ENTITY_TYPE::BASE_ENTITY>::ToRes(Res_Interface<ENTITY_TYPE::BASE_ENTITY>* _pDestRes, const int _iPosX, const int _iPosY) { if( m_pRes != _pDestRes) { m_pRes->Logout( UID ); m_pRes = _pDestRes; m_pRes->Login( UID, this ); } m_iPosX = _iPosX; m_iPosY = _iPosY; }
/****************************** * 文件名: LogicNPC_Entity.h * 创建人: 陈泽丹/Clark * 创建时间: 20120330 * 文件描述: ******************************/ #pragma once #include "../Interface/Entity_Interface.h" #include "../Interface/Res_Interface.h" #include "../Interface/NPC/BaseNPC_Entity.h" class LogicNPC_Entity: public Entity_Interface<ENTITY_TYPE::BASE_ENTITY> { public: LogicNPC_Entity( int _UID, Res_Interface<ENTITY_TYPE::BASE_ENTITY>* _pRes_Logic); private: virtual ~LogicNPC_Entity(); };
#include "LogicNPC_Entity.h" #include <iostream> using namespace std; LogicNPC_Entity::LogicNPC_Entity( int _UID, Res_Interface<ENTITY_TYPE::BASE_ENTITY>* _pRes_Logic): Entity_Interface(_UID, _pRes_Logic) { m_iPosX = rand()%20; m_iPosY = rand()%20; cout<<"逻辑服"<<_pRes_Logic->RES_ID<<"号街道(地图块)创建NPC["<< UID <<"], 坐标("<<m_pRes->RES_ID<<","<<m_iPosX<<","<<m_iPosY<<")"<<endl; } LogicNPC_Entity::~LogicNPC_Entity() { cout<<"逻辑服"<<m_pRes->RES_ID<<"号街道(地图块)删除NPC["<< UID <<"], 坐标("<<m_pRes->RES_ID<<","<<m_iPosX<<","<<m_iPosY<<")"<<endl; }
/****************************** * 文件名: LogicMagic_Entity.h * 创建人: 陈泽丹/Clark * 创建时间: 20120330 * 文件描述: ******************************/ #pragma once #include "../Interface/Entity_Interface.h" #include "../Interface/Res_Interface.h" #include "../Interface/NPC/BaseNPC_Entity.h" class LogicMagic_Entity: public Entity_Interface<ENTITY_TYPE::BASE_ENTITY> { public: LogicMagic_Entity( int _UID, Res_Interface<ENTITY_TYPE::BASE_ENTITY>* _pRes_Logic); private: virtual ~LogicMagic_Entity(); };
#include "LogicMagic_Entity.h" #include <iostream> using namespace std; LogicMagic_Entity::LogicMagic_Entity( int _UID, Res_Interface<ENTITY_TYPE::BASE_ENTITY>* _pRes_Logic): Entity_Interface(_UID, _pRes_Logic) { m_iPosX = rand()%20; m_iPosY = rand()%20; cout<<"逻辑服"<<_pRes_Logic->RES_ID<<"号街道(地图块)创建火焰术["<< UID <<"], 坐标("<<m_pRes->RES_ID<<","<<m_iPosX<<","<<m_iPosY<<")"<<endl; } LogicMagic_Entity::~LogicMagic_Entity() { cout<<"逻辑服"<<m_pRes->RES_ID<<"号街道(地图块)删除火焰术["<< UID <<"], 坐标("<<m_pRes->RES_ID<<","<<m_iPosX<<","<<m_iPosY<<")"<<endl; }
/****************************** * 文件名: FreshmanWorld.h * 创建人: 陈泽丹/Clark * 创建时间: 20120330 * 文件描述: 新手村 ******************************/ #pragma once #include "LogicNPC_Entity.h" #include "LogicMagic_Entity.h" #include "Block_LogicMap.h" class FreshmanWorld { public: FreshmanWorld(void); virtual ~FreshmanWorld(void); void CreateMonster(); void MonsterEvent(); private: void Create(); void Delete(); enum { MAP_ROW = 2, MAP_LINE = 2, NPC_SIZE = 2, MAGIC_SIZE = 1, }; //新手世界由 (MAP_ROW * MAP_LINE) 个灯塔块(地图块)组成 Block_LogicMap* m_pMapBlock[MAP_ROW][MAP_LINE]; };
#include "FreshmanWorld.h" FreshmanWorld::FreshmanWorld(void) { Create(); } FreshmanWorld::~FreshmanWorld(void) { Delete(); } void FreshmanWorld::Create() { cout<<"---------- 初始化逻辑服务器地图块 ---------- "<<endl; for(int i=0; i<MAP_ROW; i++) { for(int j=0; j<MAP_LINE; j++) { m_pMapBlock[i][j] = new Block_LogicMap(i*MAP_LINE + j); } } cout<<"\n"<<endl; } void FreshmanWorld::CreateMonster() { cout<<"---------- 每个服务器地图块创建相应的怪 ---------- "<<endl; int UID = 0; for(int i=0; i<MAP_ROW; i++) { for(int j=0; j<MAP_LINE; j++) { for(int k=0; k<NPC_SIZE; k++) { new LogicNPC_Entity(++UID, m_pMapBlock[i][j]); } for(int k=0; k<MAGIC_SIZE; k++) { new LogicMagic_Entity(++UID, m_pMapBlock[i][j]); } } } cout<<"\n"<<endl; } void FreshmanWorld::MonsterEvent() { Entity_Interface<ENTITY_TYPE::BASE_ENTITY>* pEntity = 0; cout<<"---------- 怪1 从0号街道移动到"<<(MAP_ROW-1)*MAP_LINE+MAP_LINE-1<<"号街道 ---------- "<<endl; pEntity = m_pMapBlock[0][0]->GetEntity(1); if( 0 != pEntity) pEntity->ToRes(m_pMapBlock[MAP_ROW-1][MAP_LINE-1], rand()%20, rand()%20); cout<<"---------- 法术3 从0号街道移动到1号街道 ---------- "<<endl; pEntity = m_pMapBlock[0][0]->GetEntity(3); if( 0 != pEntity) pEntity->ToRes(m_pMapBlock[0][1], rand()%20, rand()%20); cout<<"\n"<<endl; } void FreshmanWorld::Delete() { cout<<"---------- 删除逻辑服务器地图块 ---------- "<<endl; for(int i=0; i<MAP_ROW; i++) { for(int j=0; j<MAP_LINE; j++) { if( 0 != m_pMapBlock[i][j] ) { delete m_pMapBlock[i][j]; m_pMapBlock[i][j] = 0; } } } cout<<"\n"<<endl; }
/****************************** * 文件名: Block_ProxyMap.h * 创建人: 陈泽丹/Clark * 创建时间: 20120330 * 文件描述: ******************************/ #pragma once #include <iostream> #include "../Interface/Res_Interface.h" using namespace std; class Block_ProxyMap:public Res_Interface<ENTITY_TYPE::BASE_ENTITY> { public: Block_ProxyMap(const int _BLOCK_ID): Res_Interface<ENTITY_TYPE::BASE_ENTITY>(_BLOCK_ID){} ~Block_ProxyMap(){ cout<<"删除代理服地图块: "<<RES_ID<<endl; } };
/****************************** * 文件名: ProxyMagic_Entity.h * 创建人: 陈泽丹/Clark * 创建时间: 20120330 * 文件描述: ******************************/ #pragma once #include "../Interface/Entity_Interface.h" #include "../Interface/Res_Interface.h" #include "../Interface/NPC/BaseNPC_Entity.h" class ProxyMagic_Entity: public Entity_Interface<ENTITY_TYPE::BASE_ENTITY> { public: ProxyMagic_Entity( int _UID, Res_Interface<ENTITY_TYPE::BASE_ENTITY>* _pRes_Logic); private: virtual ~ProxyMagic_Entity(); };
#include "ProxyMagic_Entity.h" #include <iostream> using namespace std; ProxyMagic_Entity::ProxyMagic_Entity( int _UID, Res_Interface<ENTITY_TYPE::BASE_ENTITY>* _pRes_Logic): Entity_Interface(_UID, _pRes_Logic) { m_iPosX = rand()%20; m_iPosY = rand()%20; cout<<"代理服"<<_pRes_Logic->RES_ID<<"号街道(地图块)创建火焰术["<< UID <<"], 坐标("<<m_pRes->RES_ID<<","<<m_iPosX<<","<<m_iPosY<<")"<<endl; } ProxyMagic_Entity::~ProxyMagic_Entity() { cout<<"代理服"<<m_pRes->RES_ID<<"号街道(地图块)删除火焰术["<< UID <<"], 坐标("<<m_pRes->RES_ID<<","<<m_iPosX<<","<<m_iPosY<<")"<<endl; }
/****************************** * 文件名: ProxyNPC_Entity.h * 创建人: 陈泽丹/Clark * 创建时间: 20120330 * 文件描述: ******************************/ #pragma once #include "../Interface/Entity_Interface.h" #include "../Interface/Res_Interface.h" #include "../Interface/NPC/BaseNPC_Entity.h" class ProxyNPC_Entity: public Entity_Interface<ENTITY_TYPE::BASE_ENTITY> { public: ProxyNPC_Entity( int _UID, Res_Interface<ENTITY_TYPE::BASE_ENTITY>* _pRes_Logic); private: virtual ~ProxyNPC_Entity(); };
#include "ProxyNPC_Entity.h" #include <iostream> using namespace std; ProxyNPC_Entity::ProxyNPC_Entity( int _UID, Res_Interface<ENTITY_TYPE::BASE_ENTITY>* _pRes_Logic): Entity_Interface(_UID, _pRes_Logic) { m_iPosX = rand()%20; m_iPosY = rand()%20; cout<<"代理服"<<_pRes_Logic->RES_ID<<"号街道(地图块)创建NPC["<< UID <<"], 坐标("<<m_pRes->RES_ID<<","<<m_iPosX<<","<<m_iPosY<<")"<<endl; } ProxyNPC_Entity::~ProxyNPC_Entity() { cout<<"代理服"<<m_pRes->RES_ID<<"号街道(地图块)删除NPC["<< UID <<"], 坐标("<<m_pRes->RES_ID<<","<<m_iPosX<<","<<m_iPosY<<")"<<endl; }
#include <iostream> #include "LogicServer/FreshmanWorld.h" using namespace std; FreshmanWorld* g_pFreshmanWorld = 0; void Start() { g_pFreshmanWorld = new FreshmanWorld(); } void Run() { if( 0 != g_pFreshmanWorld) { g_pFreshmanWorld->CreateMonster(); g_pFreshmanWorld->MonsterEvent(); } } void End() { delete g_pFreshmanWorld; } void main() { Start(); Run(); End(); system("pause"); }