游戲中角色類的設計(C 語言描述)
最近一段時間自己正在編寫一款RPG游戲,前幾天用空余的時間設計了角色模塊,給熱愛游戲編成的朋友們分享分享,代碼分為三部分:1.抽象角色類,2.玩家控制的角色類,3角色管理類.本游戲使用的是hge 2D引擎 代碼如何:
一:抽象角色類:
FGRoleObject.h文件:
class FGRoleObject { public:
static void Init(HGE* m_hge) { hge = m_hge; }
/ /邏輯 virtual void Logic() = 0;
//繪制 virtual void Render() = 0;
//獲取X坐標 float GetX(){return m_x;}
//獲取Y坐標 float GetY(){return m_y;}
/ /獲取ID int GetID(){return m_ID;}
//獲取名字 char* GetName(){return m_Name;}
//獲取等級 int GetLevel(){return m_Level;}
//獲取HP float GetHp(){return m_Hp;}
//獲取MaxHp float GetMaxHp(){return m_MaxHp;}
//獲取Ep float GetEp(){return m_Ep;}
//獲取MaxEp float GetMaxEp(){return m_MaxEp;}
//獲取CP float GetCp(){return m_CP;}
//獲取MaxCp float GetMaxCp(){return m_MaxCp;} //獲取STR float GetSTR(){return m_STR;}
//獲取DEF float GetDEF(){return m_DEF;}
//獲取ATS float GetATS(){return m_ATS;}
//獲取ATF float GetADF(){return m_ADF;}
//獲取SPD float GetSPD(){return m_SPD;}
//獲取DEX float GetDEX(){return m_DEX;}
//獲取ADL float GetAGL(){return m_AGL;}
//獲取MOV float GetMOV(){return m_MOV;}
//獲取RNG float GetRNG(){return m_RNG;}
//獲取EXP float GetEXP(){return m_Exp;}
//獲取MaxExp float GetMaxExp(){return m_MaxExp;}
//獲取陣營類型 TYPE_Camp GetCamp(){return m_Camp;}
//設置碰撞區域范圍 void SetRect(float ltx,float lty,float rdx,float rdy) { m_rect.Set(ltx,lty,rdx,rdy); }
//角色與角色之間的碰撞處理 bool Intersect(FGRoleObject& role) { if( m_rect.Intersect(&role. m_rect)) { return true; } else { return false; } }
//設置英雄屬性 void SetAttribute(int _level,float _MaxHp,float _Hp,float _MaxEp, float _Ep,float _STR,float _DEF,float _ATS, float _ADF,float _SPD,float _DEX,float _AGL,float _MOV,float _RNG,float _MaxCp,float _CP, float _MaxExp,float _Exp,float _NextExp); //設置狀態 void SetMode(AtionMode _mode) { m_ationmode = _mode; } protected: //1.編號 2.陣營標示 3.角色名稱 4.角色X坐標 5.角色Y坐標 FGRoleObject(short _id, TYPE_Camp _camp,const char* _name, float _x,float _y);
//析構 virtual ~FGRoleObject(void);
static HGE* hge;
//金錢 static float Mria;
//游戲時間 static float PlayTime;
//陣營的種類 TYPE_Camp m_Camp;
//字符 char m_Name[256];
//編號 short m_ID;
//角色x坐標 float m_x;
//角色y坐標 float m_y;
//碰撞區域對象 hgeRect m_rect;
//速度 float m_speed;
//角色屬性 int m_Level;
float m_MaxHp;
float m_Hp;
float m_MaxEp;
float m_Ep;
float m_STR; float m_DEF;
float m_ATS; float m_ADF; float m_SPD; float m_DEX; float m_AGL; float m_MOV;
float m_RNG;
float m_MaxCp;
float m_CP;
float m_MaxExp;
float m_Exp;
float m_NextExp;
//行走方向 Drection m_drection;
//角色狀態 AtionMode m_ationmode; };
FGRoleObject.cpp文件:
#include "FGRoleObject.h"
HGE* FGRoleObject:: hge = 0;
FGRoleObject::~FGRoleObject(void){
}
FGRoleObject::FGRoleObject(short _id, TYPE_Camp _camp,const char* _name, float _x,float _y){ m_ID = _id;
//賦值陣營-我方還是敵方 m_Camp = _camp;
strcpy( m_Name,_name);
m_x = _x;
m_y = _y;
SetMode(WAIT);
//初始化的時候沒有設置碰撞區域,碰撞為0 m_rect.Set(0,0,0,0);}
void FGRoleObject::SetAttribute(int _level,float _MaxHp,float _Hp,float _MaxEp, float _Ep,float _STR,float _DEF,float _ATS, float _ADF,float _SPD,float _DEX,float _AGL,float _MOV,float _RNG,float _MaxCp,float _CP, float _MaxExp,float _Exp,float _NextExp){ m_Level = _level;
m_MaxHp = _MaxHp;
m_Hp = _Hp;
m_MaxEp = _MaxEp;
m_Ep = _Ep;
m_STR = _STR; m_DEF = _DEF;
m_ATS = _ATS; m_ADF = _ADF; m_SPD = _SPD; m_DEX = _DEX; m_AGL = _AGL; m_MOV = _MOV;
m_RNG = _RNG;
m_MaxCp = _MaxCp;
m_CP = _CP;
m_MaxExp = _MaxExp;
m_Exp = _Exp;
m_NextExp = _NextExp; }
FGMyRole.h文件:
class FGMyRole :public FGRoleObject{public: //基類的參數:1.編號 2.陣營標示 3.角色名稱 4.角色X坐標 5.角色Y坐標 //hgeAnimation類需要參數: 6.貼圖中的x坐標 7.貼圖中的y坐標 8.貼圖中每一幀的寬 9.貼圖中每一幀的高 // 10.動畫播放的速度 11.動畫總共的幀數 12.動畫所需要的貼圖 //我方角色本身的參數: 13.UI中角色人物圖片 14.角色的移動的速度 FGMyRole(short _id, TYPE_Camp _camp, char* _name,float _x, float _y, float _tx, float _ty, float _tw, float _th, float _fps, int _nframe, HTEXTURE _tex,short _h_Index, float m_speed); virtual ~FGMyRole(void); void Logic();
void Render();
short GethSpri_UIIndex() { return hSpri_UIIndex; }
//初始化裝備槽 void InitEquips();
//裝備欄數組-次游戲擁有5個裝備槽 ItemObject* Equips[5];
private: //角色頭像圖片索引(UI ) short hSpri_UIIndex;
//動畫對象 hgeAnimation* spr[8];
//fps播放的速度 float FPS;
//幀數 int Nframe;
//貼圖中的X坐標 float tx;
//貼圖中的Y坐標 float ty;
//貼圖中的長 float tw;
//貼圖中的寬 float th; };
FGMyRole.cpp文件:
#include "FGMyRole.h"#include "FGGameScreen.h"
FGMyRole::FGMyRole(short _id, TYPE_Camp _camp, char* _name,float _x, float _y, float _tx, float _ty, float _tw, float _th, float _fps, int _nframe, HTEXTURE _tex,short _h_Index, float _speed):FGRoleObject(_id,_camp,_name,_x,_y) { SetAttribute(4,163,163,250,203,44,46,20,15,15,16,4,4,2,46,46,500,100,250);
m_speed = _speed;
FPS = _fps;
Nframe = _nframe;
tx = _tx;
ty = _ty;
tw = _tw;
th = _th;
hSpri_UIIndex = _h_Index;
for(int i=0;i<8;i ) { spr[i] = new hgeAnimation(_tex,Nframe,FPS,tx,ty 128*i,tw,th);
spr[i]->Play(); } m_drection = RIGHT; //初始化角色裝備 InitEquips(); }
FGMyRole::~FGMyRole(void){ for(int i=7;i<0;i--) { if(spr[i]!=NULL) { delete spr[i];
spr[i] = NULL; } }}
void FGMyRole::Logic(){ //動畫更新 spr[m_drection]->Update(hge->Timer_GetDelta());
//狀態 switch(m_ationmode) { case MOVE: //鍵盤響應 if(hge->Input_GetKeyState(HGEK_W)) //上 { if(hge->Input_GetKeyState(HGEK_D)) { m_y-=m_speed; m_x =m_speed; m_drection = RIGHTUP;
} else if(hge->Input_GetKeyState(HGEK_A)) { m_y-=m_speed; m_x-=m_speed; m_drection = LEFTUP; } else if(hge->Input_GetKeyState(HGEK_S)) { m_y =m_speed;
m_drection = DOWN; } else { m_y-=m_speed;
m_drection = UP; }
spr[m_drection]->SetTexture(FGResMangner::mangner->GetTexture("role2-move1")); } else if(hge->Input_GetKeyState(HGEK_S)) //下 { if(hge->Input_GetKeyState(HGEK_W)) { m_y-=m_speed;
m_drection = UP;
} else if(hge->Input_GetKeyState(HGEK_D)) { m_x =m_speed;
m_y =m_speed;
m_drection = RIGHTDOWN; } else if(hge->Input_GetKeyState(HGEK_A)) { m_x-=m_speed;
m_y =m_speed; m_drection = LEFTDOWN; } else { m_y =m_speed; m_drection = DOWN; } spr[m_drection]->SetTexture(FGResMangner::mangner->GetTexture("role2-move1")); } else if(hge->Input_GetKeyState(HGEK_D)) //右 { if(hge->Input_GetKeyState(HGEK_W)) { m_x =m_speed;
m_y-=m_speed;
m_drection = RIGHTUP; } else if(hge->Input_GetKeyState(HGEK_S)) { m_x =m_speed;
m_y =m_speed;
m_drection = RIGHTDOWN; } else if(hge->Input_GetKeyState(HGEK_A)) { m_x-=m_speed;
m_drection = LEFT; } else { m_x =m_speed; m_drection = RIGHT; } spr[m_drection]->SetTexture(FGResMangner::mangner->GetTexture("role2-move1")); } else if(hge->Input_GetKeyState(HGEK_A)) //左 { if(hge->Input_GetKeyState(HGEK_W)) { m_x-=m_speed;
m_y-=m_speed;
m_drection = LEFTUP; } else if(hge->Input_GetKeyState(HGEK_D)) { m_x =m_speed;
m_drection = RIGHT; } else if(hge->Input_GetKeyState(HGEK_S)) { m_x-=m_speed;
m_y =m_speed;
m_drection = LEFTDOWN; } else { m_x-=m_speed;
m_drection = LEFT; } spr[m_drection]->SetTexture(FGResMangner::mangner->GetTexture("role2-move1")); } else { //在沒有任何按鍵響應時把動畫幀數調整為0 spr[m_drection]->SetFrame(0); spr[m_drection]->SetTexture(FGResMangner::mangner->GetTexture("role2-waiting")); SetMode(WAIT); } if(hge->Input_GetKeyState(HGEK_CTRL)) { SetMode(ATTACT); spr[m_drection]->SetTexture(FGResMangner::mangner->GetTexture("role2-attact1")); spr[m_drection]->SetSpeed(15); spr[m_drection]->SetFrame(0); } break; case WAIT: //待機狀態 //鍵盤響應 if(hge->Input_GetKeyState(HGEK_W)) //上 { if(hge->Input_GetKeyState(HGEK_D)) { m_drection = RIGHTUP; } else if(hge->Input_GetKeyState(HGEK_A)) { m_drection = LEFTUP; } else if(hge->Input_GetKeyState(HGEK_S)) { m_drection = DOWN; } else { m_drection = UP; } spr[m_drection]->SetTexture(FGResMangner::mangner->GetTexture("role2-move1"));
SetMode(MOVE); } else if(hge->Input_GetKeyState(HGEK_S)) //下 { if(hge->Input_GetKeyState(HGEK_W)) { m_drection = UP; } else if(hge->Input_GetKeyState(HGEK_D)) { m_drection = RIGHTDOWN; } else if(hge->Input_GetKeyState(HGEK_A)) { m_drection = LEFTDOWN; } else { m_drection = DOWN; } spr[m_drection]->SetTexture(FGResMangner::mangner->GetTexture("role2-move1"));
SetMode(MOVE); } else if(hge->Input_GetKeyState(HGEK_D)) //右 { if(hge->Input_GetKeyState(HGEK_W)) { m_drection = RIGHTUP; } else if(hge->Input_GetKeyState(HGEK_S)) { m_drection = RIGHTDOWN; } else if(hge->Input_GetKeyState(HGEK_A)) { m_drection = LEFT; } else { m_drection = RIGHT; } spr[m_drection]->SetTexture(FGResMangner::mangner->GetTexture("role2-move1"));
SetMode(MOVE); } else if(hge->Input_GetKeyState(HGEK_A)) //左 { if(hge->Input_GetKeyState(HGEK_W)) { m_drection = LEFTUP; } else if(hge->Input_GetKeyState(HGEK_D)) { m_drection = RIGHT; } else if(hge->Input_GetKeyState(HGEK_S)) { m_drection = LEFTDOWN; } else { m_drection = LEFT; } spr[m_drection]->SetTexture(FGResMangner::mangner->GetTexture("role2-move1"));
SetMode(MOVE); } if(hge->Input_GetKeyState(HGEK_CTRL)) { SetMode(ATTACT); spr[m_drection]->SetTexture(FGResMangner::mangner->GetTexture("role2-attact1")); spr[m_drection]->SetSpeed(15); spr[m_drection]->SetFrame(0); }
break; case ATTACT: //攻擊狀態 if(spr[m_drection]->GetFrame()==7) { SetMode(WAIT); spr[m_drection]->SetTexture(FGResMangner::mangner->GetTexture("role2-waiting")); spr[m_drection]->SetSpeed(5); } break; case BATTACT: //被攻擊狀態 break; case DEATH: //死亡狀態 break; }
}
void FGMyRole::Render(){ spr[m_drection]->Render(m_x,m_y);}
void FGMyRole::InitEquips(){ for(int i=0;i<5;i ) { Equips[i] = NULL; }
switch(m_ID) { case 0://角色1 //武器 Equips[0] = new Weanpon(ItemObject::WEANPON,0,0,0,0); //防具 Equips[1] = new Clothes(ItemObject::CLOTHES,1,9,9,0); //鞋子 Equips[2] = new Clothes(ItemObject::CLOTHES,2,11,11,0); //飾品 Equips[3] = new Clothes(ItemObject::CLOTHES,3,12,12,0); //飾品 Equips[4] = new Clothes(ItemObject::CLOTHES,4,12,12,0); break; case 1://角色2 Equips[0] = new Weanpon(ItemObject::WEANPON,1,1,1,0); break; } }
FGRoleManager.h文件:
class FGRoleManager{public: //管理類對象.此對象只有一個 static FGRoleManager* mangner;
//當前選中的角色對象 static FGRoleObject* CurRole;
//角色的id號連表用來記錄整個游戲玩家 一共擁有多少個角色的ID list<short> listid;
list<short>::iterator iterid;
~FGRoleManager(void);
static FGRoleManager* Instance(HGE* _hge) { if(0 == mangner) { hge = _hge;
FGRoleObject::Init(hge);
return mangner = new FGRoleManager(); }
return mangner; }
void Logic();
void Render();
void AddRole(FGRoleObject& _object);
void DeleteRole(FGRoleObject& _object);
//獲取角色-根據ID來獲取 FGRoleObject* GetRole(short id);
//貨物角色id從list中 short GetIDFromeList(short index);
//獲取我方陣營人數 short GetMyRoleNums();
//獲取敵方陣營人數 short GetEnemyNums(); protected:
private: list<FGRoleObject*> listobject;
list<FGRoleObject*>::iterator iterobject;
FGRoleManager(void);
static HGE* hge; };
FGRoleManager.cpp文件:
HGE* FGRoleManager::hge = NULL;
FGRoleManager* FGRoleManager::mangner = NULL;
FGRoleObject* FGRoleManager::CurRole = NULL;
FGRoleManager::FGRoleManager(void){
}
FGRoleManager::~FGRoleManager(void){
} void FGRoleManager::Logic(){ //所有角色各自運行自己的邏輯 for(iterobject = listobject.begin();iterobject!= listobject.end();iterobject ) { (*iterobject)->Logic(); }}
void FGRoleManager::Render(){ //所有角色各自運行自己的繪制 for(iterobject = listobject.begin();iterobject!=listobject.end();iterobject ) { (*iterobject)->Render(); }}
void FGRoleManager::AddRole(FGRoleObject& _object){ //加入角色對象 listobject.push_front(&_object);
//加入角色id listid.push_back((&_object)->GetID());
}
void FGRoleManager::DeleteRole(FGRoleObject& _object){ if(listobject.empty()) return; listobject.remove(&_object);}
short FGRoleManager::GetMyRoleNums(){ short temp = 0;
for(iterobject = listobject.begin();iterobject!=listobject.end();iterobject ) { if((*iterobject)->GetCamp() == PLAYER) { temp ; } } return temp; }
short FGRoleManager::GetEnemyNums(){ short temp = 0;
for(iterobject = listobject.begin();iterobject!=listobject.end();iterobject ) { if((*iterobject)->GetCamp() == ENEMY) { temp ; } }
return temp; }
FGRoleObject* FGRoleManager::GetRole(short id){ for(iterobject = listobject.begin();iterobject!=listobject.end();iterobject ) { if(id == (*iterobject)->GetID() ) { return *iterobject; } }
return NULL;}
short FGRoleManager::GetIDFromeList(short index){ int tempindex = 0;
for(iterid = listid.begin();iterid!=listid.end(); iterid ) { if(tempindex == index) { return *iterid; } tempindex ; }
return 0;}
此設計運用了繼承和多狀.在管理類(FGRoleManager)裡有一個基類(FGRoleObjetct)的list 來存放子類對象,通過list來統一管理子類對象.
下面我寫下自己的一些設計想法:
基本類FGRoleObject:
類成員:
只允許寫游戲角色擁有的屬性:比如hp mp x y(角色的坐標)等
說明:為了讓此類更加的通用,何為通用呢?就是這個類可以適用於任何游戲,在編寫任何游戲的時候都可以拿來使用,由於制作游戲的引擎不一樣,那麼繪制的處理也會不一樣,因此基類的成員裡只能寫整個游戲角色所需要的屬性變量。
類成員函數:
游戲角色通用的行為,比如碰撞處理,屬性的設置等等 ,沒 個角色必須得用的上的。
子類FGMyRole:
此類裡的成員,可以加入角色的一些特有的變量,根據不同的引擎的繪制處理機制不同,對應不同的資源對象,比如在這個類的 采用 的是hge的動畫對象來繪制角色,那麼成員裡就可以加入動畫類的對象,另外角色還裝備系統,自然也要加上管理裝備的數據結構,這裡是用一個一緯數組。
整個角色的管理類 FGRoleManager:
此類利用list角色基類指針的連表來管理角色的每一個對象 成員方法主要實現整個游戲的角色的添加,刪除,通過編號來獲取某個角色等等。此類采用單件模式,整個游戲裡只有一個角色管理對象,次對象管理NPC 我放,敵方,中立等角色。這些角色可以全部加入到list連表裡統一管理,比如統一繪制,統一邏輯等等。由於連表裡存在不同類型的對象,那麼就必須得有一個種類的標示,此類裡用了一個種類的enum枚舉 目前只寫了我方陣營類型 和敵方陣營類型 後面還回有中立的類型,比如npc。有了這個類型標示 那麼就可以很輕松的獲取各種角色對象了。