#include <iostream> #include <time.h> #include "PM.h" using namespace std; void main() { srand(time(0)); PM::run(); system("pause"); }
/* * 文件名: 经理 * 创建人: 陈泽丹/Clark * 创建时间: 20120321 * 文件描述: 负责NPC的使用 */ #pragma once #include "NPC.h" #include "NPC_Interface.h" class PM { public: static void run(); static void doWord(); private: template<int s, int e> class CmdSwitchCase { public: static bool SwitchCase(NPC_Interface<NPC>* _pNPC, int i); }; template<int v> class CmdSwitchCase<v, v> { public: static bool SwitchCase(NPC_Interface<NPC>* _pNPC, int i); }; };
#include "PM.h" #include "CMD.h" #include "CMD_SET.h" #include "Girl_Sword_NPC.h" #include "HR.h" template<int s, int e> bool PM::CmdSwitchCase<s, e>::SwitchCase(NPC_Interface<NPC>* _pNPC, int i) { if( i<s || i>=e) return false; const int POS = (s+e)/2; if( POS == i) { _pNPC->RecvCmd(CMD<POS>()); return true; } else if( POS > i){ return CmdSwitchCase<s,POS>::SwitchCase(_pNPC, i); } else{ return CmdSwitchCase<POS,e>::SwitchCase(_pNPC, i); } } template<int v> bool PM::CmdSwitchCase<v, v>::SwitchCase(NPC_Interface<NPC>* _pNPC, int i) { return false; } void PM::doWord() { for(int i=0; i < HR::g_NPCSet.size(); i++) { int iRandCmd = rand()%(G_DEFINE::G_TO_CLIENT_CMD::TO_CLIENT_CMD_END); CmdSwitchCase<G_TO_CLIENT_CMD::TO_CLIENT_CMD_START, G_TO_CLIENT_CMD::TO_CLIENT_CMD_END>::SwitchCase(HR::g_NPCSet[i], iRandCmd); } } void PM::run(void) { Girl_Sword_NPC buildGirl; buildGirl.Create_Girl_Sword_NPC(); do { doWord(); } while (HR.g_NPCSet.size() > 0); HR::Clear(); }
#pragma once template<class TMP_NPC> class State_Interface { public: virtual ~State_Interface(){} virtual void Execute(TMP_NPC* _pNPC) = 0; const int STATE_ID; protected: State_Interface(const int _STATE_ID):STATE_ID(_STATE_ID){} };
#pragma once template<class TMP_NPC> class NPC_Interface: private TMP_NPC { public: template<class T> int RecvCmd(T _Cmd){ return _Cmd.Execute( (TMP_NPC*)this ); } private: NPC_Interface(const int _UID):TMP_NPC(_UID){} ~NPC_Interface(){} friend class HR; };
#pragma once namespace G_DEFINE { struct G_TO_CLIENT_CMD { enum { //开始指令 TO_CLIENT_CMD_START = 1, //死亡指令 TO_CLIENT_DIE_ING_CMD = TO_CLIENT_CMD_START, //移动指令 TO_CLIENT_MOVE_ING_CMD, //攻击指令 TO_CLIENT_ATTACK_ING_CMD, TO_CLIENT_ATTACK_ED_CMD, //火焰术指令 TO_CLIENT_FIRE_MAGIC_ING_CMD, TO_CLIENT_FIRE_MAGIC_ED_CMD, //结束指令 TO_CLIENT_CMD_END = TO_CLIENT_FIRE_MAGIC_ED_CMD, }; }; struct G_GAME_STATE { enum { //女剑士 ------------------------------- //女剑士_死亡态 GIRL_SWORD_DIE_ING_STATE = 0, //女剑士_移动态 GIRL_SWORD_MOVE_ING_STATE, //女剑士_攻击态 GIRL_ATTACK_ING_STATE, GIRL_ATTACK_ED_STATE, //女剑士_火焰术态 GIRL_SWORD_FIRE_MAGIC_ING_STATE, GIRL_SWORD_FIRE_MAGIC_ED_STATE, }; }; } enum G_GAME_STATE { };
#pragma once template<class TMP_NPC> class CMD_Interface { public: virtual ~CMD_Interface(){} virtual int Execute(TMP_NPC* _pNPC) = 0; const int CMD_ID; protected: CMD_Interface(const int _CMD_ID):CMD_ID(_CMD_ID){} };
/* * 文件名: 人力 * 创建人: 陈泽丹/Clark * 创建时间: 20120321 * 文件描述: 负责NPC的资源管理 */ #pragma once #include "NPC.h" #include "NPC_Interface.h" #include <vector> using namespace std; class HR { public: static vector< NPC_Interface<NPC>* > g_NPCSet; static NPC_Interface<NPC>* Create_NPC_Interface(); static void DeleteNPC(const int _uID); static void Clear(); };
#include "HR.h" vector< NPC_Interface<NPC>* > HR::g_NPCSet; NPC_Interface<NPC>* HR::Create_NPC_Interface() { return new NPC_Interface<NPC>( g_NPCSet.size() ); } void HR::DeleteNPC(const int _uID) { for(int i=0; i<g_NPCSet.size(); i++) { if( _uID == g_NPCSet[i]->UID) { delete g_NPCSet[i]; g_NPCSet[i] = g_NPCSet[g_NPCSet.size()-1]; g_NPCSet.pop_back(); break; } } } void HR::Clear() { for(int i=0; i<g_NPCSet.size(); i++) { delete g_NPCSet[i]; } g_NPCSet.clear(); }
#pragma once #include "NPC.h" #include "NPC_Interface.h" class NPCBuilder {//建造者,对客户屏蔽目标的内部生成功能,暂时提供外部的生成选择,过后只提供目标的使用功能的权限。 public: //创建NPC void Build_NPC(); void Add_State(int _CMD_ID, State_Interface<NPC>* _pState_Interface); void Del_State(int _CMD_ID); //更新NPC数据 void Update_NPC(); private: NPC_Interface<NPC>* m_pNPC; };
#include "NPCBuilder.h" #include "HR.h" //创建NPC void NPCBuilder::Build_NPC() { m_pNPC = HR::Create_NPC_Interface(); } void NPCBuilder::Add_State(int _CMD_ID, State_Interface<NPC>* _pState_Interface) { NPC* pNPC = (NPC*)m_pNPC; pNPC->m_dStateSet[_CMD_ID] = _pState_Interface; } void NPCBuilder::Del_State(int _CMD_ID) { NPC* pNPC = (NPC*)m_pNPC; pNPC->m_dStateSet[_CMD_ID] = 0; } //更新NPC数据 void NPCBuilder::Update_NPC() { HR::g_NPCSet.push_back(m_pNPC); m_pNPC = NULL; }
#pragma once #include <map> #include "State_Interface.h" using namespace std; class NPC { public: NPC(const int _UID); virtual ~NPC(void); const int UID; int m_iPosX; int m_iPosY; map<int, State_Interface<NPC>* > m_dStateSet; State_Interface<NPC>* m_pCurState; private: friend class NPCBuilder; };
#include "NPC.h" NPC::NPC(const int _UID):UID(_UID),m_pCurState(0) { m_iPosX = rand()%800; m_iPosY = rand()%600; } NPC::~NPC(void) { }
#pragma once #include "NPC.h" #include "State_Interface.h" class NPC; template<int v> class State: public State_Interface<NPC> { public: State(const int _STATE_ID): State_Interface<NPC>(_STATE_ID){} virtual ~State(void){} };
#pragma once #include "State.h" #include "NPC.h" #include "G_DEFINE.h" using namespace G_DEFINE; //女剑士死亡态 template<> class State<G_DEFINE::G_GAME_STATE::GIRL_SWORD_DIE_ING_STATE>: public State_Interface<NPC> { public: State():State_Interface<NPC>(G_DEFINE::G_GAME_STATE::GIRL_SWORD_DIE_ING_STATE){} ~State(); void Execute(NPC* _pNPC); }; //女剑士移动态 template<> class State<G_DEFINE::G_GAME_STATE::GIRL_SWORD_MOVE_ING_STATE>: public State_Interface<NPC> { public: State():State_Interface<NPC>(G_DEFINE::G_GAME_STATE::GIRL_SWORD_MOVE_ING_STATE){} void Execute(NPC* _pNPC); }; //女剑士_攻击态 template<> class State<G_DEFINE::G_GAME_STATE::GIRL_ATTACK_ING_STATE>: public State_Interface<NPC> { public: State():State_Interface<NPC>(G_DEFINE::G_GAME_STATE::GIRL_ATTACK_ING_STATE){} void Execute(NPC* _pNPC); }; template<> class State<G_DEFINE::G_GAME_STATE::GIRL_ATTACK_ED_STATE>: public State_Interface<NPC> { public: State():State_Interface<NPC>(G_DEFINE::G_GAME_STATE::GIRL_ATTACK_ED_STATE){} void Execute(NPC* _pNPC); }; //女剑士_火焰术态 template<> class State<G_DEFINE::G_GAME_STATE::GIRL_SWORD_FIRE_MAGIC_ING_STATE>: public State_Interface<NPC> { public: State():State_Interface<NPC>(G_DEFINE::G_GAME_STATE::GIRL_SWORD_FIRE_MAGIC_ING_STATE){} void Execute(NPC* _pNPC); }; template<> class State<G_DEFINE::G_GAME_STATE::GIRL_SWORD_FIRE_MAGIC_ED_STATE>: public State_Interface<NPC> { public: State():State_Interface<NPC>(G_DEFINE::G_GAME_STATE::GIRL_SWORD_FIRE_MAGIC_ED_STATE){} void Execute(NPC* _pNPC); };
#include "Girl_Sword_State.h" #include "HR.h" #include <iostream> using namespace std; //女剑士死亡态 State<G_DEFINE::G_GAME_STATE::GIRL_SWORD_DIE_ING_STATE>::~State() { cout<<"女剑士死亡态进行析构!"<<endl; } void State<G_DEFINE::G_GAME_STATE::GIRL_SWORD_DIE_ING_STATE>::Execute(NPC* _pNPC) { cout<<"run -> 女剑士死亡态"<<endl; HR::DeleteNPC(_pNPC->UID); } //女剑士移动态 void State<G_DEFINE::G_GAME_STATE::GIRL_SWORD_MOVE_ING_STATE>::Execute(NPC* _pNPC) { cout<<"run -> 女剑士移动态"<<endl; } //女剑士_攻击态 void State<G_DEFINE::G_GAME_STATE::GIRL_ATTACK_ING_STATE>::Execute(NPC* _pNPC) { cout<<"run -> 女剑士进行攻击"<<endl; } void State<G_DEFINE::G_GAME_STATE::GIRL_ATTACK_ED_STATE>::Execute(NPC* _pNPC) { cout<<"run -> 女剑士被攻击"<<endl; } //女剑士_火焰术态 void State<G_DEFINE::G_GAME_STATE::GIRL_SWORD_FIRE_MAGIC_ING_STATE>::Execute(NPC* _pNPC) { cout<<"run -> 女剑士施放火焰术"<<endl; } void State<G_DEFINE::G_GAME_STATE::GIRL_SWORD_FIRE_MAGIC_ED_STATE>::Execute(NPC* _pNPC) { cout<<"run -> 女剑士受到火焰术"<<endl; }
#include "NPCBuilder.h" #include "Girl_Sword_State.h" #include "G_DEFINE.h" #include "State.h" #include "Girl_Sword_State.h" using namespace G_DEFINE; //女剑士_NPC class Girl_Sword_NPC { public: static void Create_Girl_Sword_NPC() { NPCBuilder builder; builder.Build_NPC(); builder.Add_State(G_DEFINE::G_TO_CLIENT_CMD::TO_CLIENT_DIE_ING_CMD, new State<G_DEFINE::G_GAME_STATE::GIRL_SWORD_DIE_ING_STATE>); builder.Add_State(G_DEFINE::G_TO_CLIENT_CMD::TO_CLIENT_MOVE_ING_CMD, new State<G_DEFINE::G_GAME_STATE::GIRL_SWORD_MOVE_ING_STATE>); builder.Add_State(G_DEFINE::G_TO_CLIENT_CMD::TO_CLIENT_ATTACK_ING_CMD, new State<G_DEFINE::G_GAME_STATE::GIRL_ATTACK_ING_STATE>); builder.Add_State(G_DEFINE::G_TO_CLIENT_CMD::TO_CLIENT_ATTACK_ED_CMD, new State<G_DEFINE::G_GAME_STATE::GIRL_ATTACK_ED_STATE>); builder.Add_State(G_DEFINE::G_TO_CLIENT_CMD::TO_CLIENT_FIRE_MAGIC_ING_CMD, new State<G_DEFINE::G_GAME_STATE::GIRL_SWORD_FIRE_MAGIC_ING_STATE>); builder.Add_State(G_DEFINE::G_TO_CLIENT_CMD::TO_CLIENT_FIRE_MAGIC_ED_CMD, new State<G_DEFINE::G_GAME_STATE::GIRL_SWORD_FIRE_MAGIC_ED_STATE>); builder.Update_NPC(); } };
#pragma once #include "CMD.h" #include "NPC.h" #include "G_DEFINE.h" using namespace G_DEFINE; //死亡指令 template<> class CMD<G_TO_CLIENT_CMD::TO_CLIENT_DIE_ING_CMD>: public CMD_Interface<NPC> { public: CMD():CMD_Interface<NPC>(G_TO_CLIENT_CMD::TO_CLIENT_DIE_ING_CMD){} ~CMD(); virtual int Execute(NPC* _pNPC); }; //移动指令 template<> class CMD<G_TO_CLIENT_CMD::TO_CLIENT_MOVE_ING_CMD>: public CMD_Interface<NPC> { public: CMD():CMD_Interface<NPC>(G_TO_CLIENT_CMD::TO_CLIENT_MOVE_ING_CMD){} virtual int Execute(NPC* _pNPC); }; //攻击指令 template<> class CMD<G_TO_CLIENT_CMD::TO_CLIENT_ATTACK_ING_CMD>: public CMD_Interface<NPC> { public: CMD():CMD_Interface<NPC>(G_TO_CLIENT_CMD::TO_CLIENT_ATTACK_ING_CMD){} virtual int Execute(NPC* _pNPC); }; template<> class CMD<G_TO_CLIENT_CMD::TO_CLIENT_ATTACK_ED_CMD>: public CMD_Interface<NPC> { public: CMD():CMD_Interface<NPC>(G_TO_CLIENT_CMD::TO_CLIENT_ATTACK_ED_CMD){} virtual int Execute(NPC* _pNPC); }; //火焰术指令 template<> class CMD<G_TO_CLIENT_CMD::TO_CLIENT_FIRE_MAGIC_ING_CMD>: public CMD_Interface<NPC> { public: CMD():CMD_Interface<NPC>(G_TO_CLIENT_CMD::TO_CLIENT_FIRE_MAGIC_ING_CMD){} virtual int Execute(NPC* _pNPC); }; template<> class CMD<G_TO_CLIENT_CMD::TO_CLIENT_FIRE_MAGIC_ED_CMD>: public CMD_Interface<NPC> { public: CMD():CMD_Interface<NPC>(G_TO_CLIENT_CMD::TO_CLIENT_FIRE_MAGIC_ED_CMD){} virtual int Execute(NPC* _pNPC); };
#include "CMD_SET.h" #include <iostream> #include "NPC.h" #include "State_Interface.h" using namespace std; //死亡指令 CMD<G_TO_CLIENT_CMD::TO_CLIENT_DIE_ING_CMD>::~CMD() { cout<<"死亡指令指令进行析构!"<<endl; } int CMD<G_TO_CLIENT_CMD::TO_CLIENT_DIE_ING_CMD>::Execute(NPC* _pNPC) { _pNPC->m_pCurState = _pNPC->m_dStateSet[this->CMD_ID]; _pNPC->m_pCurState->Execute(_pNPC); return 0; } //移动指令 int CMD<G_TO_CLIENT_CMD::TO_CLIENT_MOVE_ING_CMD>::Execute(NPC* _pNPC) { _pNPC->m_pCurState = _pNPC->m_dStateSet[this->CMD_ID]; _pNPC->m_pCurState->Execute(_pNPC); return 0; } //攻击指令 int CMD<G_TO_CLIENT_CMD::TO_CLIENT_ATTACK_ING_CMD>::Execute(NPC* _pNPC) { _pNPC->m_pCurState = _pNPC->m_dStateSet[this->CMD_ID]; _pNPC->m_pCurState->Execute(_pNPC); return 0; } int CMD<G_TO_CLIENT_CMD::TO_CLIENT_ATTACK_ED_CMD>::Execute(NPC* _pNPC) { _pNPC->m_pCurState = _pNPC->m_dStateSet[this->CMD_ID]; _pNPC->m_pCurState->Execute(_pNPC); return 0; } //火焰术指令 int CMD<G_TO_CLIENT_CMD::TO_CLIENT_FIRE_MAGIC_ING_CMD>::Execute(NPC* _pNPC) { _pNPC->m_pCurState = _pNPC->m_dStateSet[this->CMD_ID]; _pNPC->m_pCurState->Execute(_pNPC); return 0; } int CMD<G_TO_CLIENT_CMD::TO_CLIENT_FIRE_MAGIC_ED_CMD>::Execute(NPC* _pNPC) { _pNPC->m_pCurState = _pNPC->m_dStateSet[this->CMD_ID]; _pNPC->m_pCurState->Execute(_pNPC); return 0; }
#pragma once #include "NPC.h" #include "CMD_Interface.h" class NPC; template<int v> class CMD: public CMD_Interface<NPC> { public: CMD(const int _CMD_ID): CMD_Interface<NPC>(_CMD_ID){} virtual ~CMD(void){} };