@zoand
2015-09-20T14:57:08.000000Z
字数 3250
阅读 1353
HGE
/** CopyRight 2009 - 2010 GDE工作室* 游戏UI系统 - HGE GUI控件 - 角色管理器* ===================================* 提供角色的资源管理、角色图片信息管理等功能** 2010/01/07 cg create*/#ifndef GDE_UI_ROLE_MANAGER_H_#define GDE_UI_ROLE_MANAGER_H_#include "GDE_UI_BasicClasses.h"using namespace GDE;//角色渲染资料单元struct RoleGuiUnit{RoleGuiUnit( int id , std::string filename , int px, int py ,HGE* pgHGE ){role_id = id;img_filename = filename;x = px;y = py;pHGE = pgHGE;////读取图片tex = pHGE->Texture_Load( filename.c_str() ); //装载纹理w = pHGE->Texture_GetWidth( tex );h = pHGE->Texture_GetHeight( tex );spr = new hgeSprite( tex, 0, 0, w, h );}~RoleGuiUnit(){if( tex )pHGE->Texture_Free( tex );if( spr )delete spr;}void SetAttackInfo( std::string txt ){attackinfo = txt;attackinfo_show_count = 200; //此处和FPS相关}int role_id; //角色IDstd::string img_filename;//角色图片文件名int x,y; //角色坐标(绝对坐标)Direction direct; //角色朝向HTEXTURE tex;hgeSprite* spr;int w,h; //角色图片宽高HGE* pHGE;std::string attackinfo;//攻击掉血数int attackinfo_show_count;};struct RoleGuiUnitAutoPtr{RoleGuiUnitAutoPtr( RoleGuiUnit* r ){ptr = r;}~RoleGuiUnitAutoPtr(){delete ptr;}RoleGuiUnit* GetPtr(){return ptr;}private:RoleGuiUnit* ptr;};//人物角色管理器GUIclass GDE_GUIRoleManager : public GDE_BASIC_GUIChineseFont{public:GDE_GUIRoleManager( int id , HGE* pgHGE ): pHGE( pgHGE ){this->id = id;this->rect.Set( 0,0,0,0 ); //该GUI控件只用于显示不用于输入}virtual ~GDE_GUIRoleManager(){for( int i = 0; i < roles_.size(); i++ ){delete roles_[i];}roles_.clear();}//增加角色void AddRole( int x, int y, int role_id, std::string filename ){RoleGuiUnit* tmp = new RoleGuiUnit( role_id, filename, x, y, pHGE );//RoleGuiUnitAutoPtr ptr( tmp );roles_.push_back(tmp);}//移除角色void RemoveRole( int role_id ){std::vector<RoleGuiUnit*>::iterator iter;for(iter = roles_.begin(); iter != roles_.end(); ++iter ){if( (*iter)->role_id == role_id ){RoleGuiUnit* ptr = *iter;roles_.erase( iter );delete ptr;return;}}}//设置人物位置void SetPos( int role_id , int x, int y ){std::vector<RoleGuiUnit*>::iterator iter;for(iter = roles_.begin(); iter != roles_.end(); ++iter ){if( (*iter)->role_id == role_id ){(*iter)->x = x;(*iter)->y = y;return;}}}//设置人物方向void SetDirection( int role_id , Direction d ){std::vector<RoleGuiUnit*>::iterator iter;for(iter = roles_.begin(); iter != roles_.end(); ++iter ){if( (*iter)->role_id == role_id ){(*iter)->direct = d;return;}}}//是否提示角色信息void RoleInfo( bool is_enable ){info_enable = is_enable;}//处理角色掉血信息void AttackInfo( int role_id, std::string txt ){int x,y;std::vector<RoleGuiUnit*>::iterator iter;for(iter = roles_.begin(); iter != roles_.end(); ++iter ){if( (*iter)->role_id == role_id ){(*iter)->SetAttackInfo( txt );break;}}}//渲染所有角色virtual void Render(){std::vector<RoleGuiUnit*>::iterator iter;for(iter = roles_.begin(); iter != roles_.end(); ++iter ){if( (*iter)->spr ){(*iter)->spr->RenderStretch( (*iter)->x, (*iter)->y,(*iter)->x + (*iter)->w,(*iter)->y + (*iter)->h );}if( (*iter)->attackinfo_show_count > 0 ){// TO DO 增加掉血值的alpha变化和位置变化,可以做的更绚int x = (*iter)->x + 10;int y = (*iter)->y - 20;font->Print( x,y,(*iter)->attackinfo.c_str() );(*iter)->attackinfo_show_count--;}}}//virtual bool MouseLButton(bool bDown);//virtual void MouseOver( bool bOver );//virtual bool MouseMove(float x, float y);private:HGE* pHGE;/*by CG 2010-1-7 这个问题调了我半个小时~总结教训中。。此处容器模板成员使用指针的原因:vector模板成员RoleGuiUnit内涉及到无法复制的内容,如果不使用指针的话,在vector的push_back操作中会出现run-time error,所以使用指针容器。使用指针容器的时候需要注意,在erase或者clear其成员的时候,需要手动delete成员指针。(因为vector默认在erase或者delete的时候调用该类的析构函数,若是指针,则无法释放其指向的内容。*/std::vector<RoleGuiUnit*> roles_;bool info_enable;//是否提示角色信息};#endif