利用HGE引擎设计的角色部分
<<返回我的日志 意见反馈
◆
◆
游戏中角色类的设计(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。有了这个类型标示 那么就可以很轻松的获取各种角色对象了。
以上是角色模块的初步设计,本人初C 大家对我的设计想法有什么好的意见请第一时间 提出 互相学习,qq:350544011.