华容道开发02---角色类的设计与数据读取

来源:互联网 发布:java加密解密的艺术 编辑:程序博客网 时间:2024/04/30 14:58

游戏中会出现的角色有曹操,将军(赵云等),兵三种类型,但是,将军有多种贴图,而且,将军还有横竖之分。

属性:

IDid 贴图image_name 类型type

四种类型

boss,曹操

general_hor将军横向,

general_ver,将军竖向

soldier士兵宽度width横向占的格子数高度height竖向占的格子数


 xml文档截图:

代码:

Role.h

#ifndef _ROLE_H_#define _ROLE_H_#include "cocos2d.h"#include "tinyxml2/tinyxml2.h"#define  IF_NULL_RETURN_FALSE(_x_) if(_x_ == nullptr) return false using namespace tinyxml2 ;USING_NS_CC ;typedef enum {kRoleTypeNone = 0,kRoleTypeBoss,//<曹操kRoleTypeArmyGeneralHorizontal,//<横向的将军kRoleTypeArmyGeneralVertical,//<竖直的将军kRoleTypeSoldier,//<士兵}RoleType;class Role : public Ref{public:static  cocos2d::Vector<Role*> s_roleVec ;static const char * XML_FILE_NAME ;static bool initStatic();static bool parseData(XMLElement * pElement) ;static void finalizeStatic();public:Role();~Role();bool init(XMLElement * pElement);RoleType getTypeByChar(const char * pType);private:CC_SYNTHESIZE_READONLY(int ,m_id,ID) ;CC_SYNTHESIZE_READONLY(int ,m_width,Width) ;CC_SYNTHESIZE_READONLY(int ,m_height,Height) ;CC_SYNTHESIZE_READONLY(RoleType,m_type,Type);CC_SYNTHESIZE_READONLY(cocos2d::__String *,m_pImageName,ImageName) ;};#endif

Role.cpp

#include "Role.h"const char * Role::XML_FILE_NAME = "roles.xml" ;Vector<Role*> Role::s_roleVec ;bool Role::initStatic(){std::string filePath = FileUtils::getInstance()->fullPathForFilename(XML_FILE_NAME) ;tinyxml2::XMLDocument pDoc;FileUtils::getInstance()->setPopupNotify(false) ;ssize_t fileSize = 0 ;std::string data = FileUtils::getInstance()->getStringFromFile(filePath.c_str()); FileUtils::getInstance()->setPopupNotify(true) ;pDoc.Parse(data.c_str()) ;XMLElement * pEle = pDoc.RootElement() ;return parseData(pEle) ;}bool Role::parseData(XMLElement * pElement){s_roleVec.clear() ;XMLElement * child = pElement->FirstChildElement() ;for (;child;child = child->NextSiblingElement()){if (strcmp(child->Value(),"role") == 0){Role * pRol = new Role() ;if (!pRol->init(child)){CC_SAFE_RELEASE_NULL(pRol); return false ;}s_roleVec.pushBack(pRol) ;pRol->release() ;}}return true ;}void Role::finalizeStatic(){s_roleVec.clear() ;}Role::Role():m_id(-1),m_pImageName(nullptr),m_type(kRoleTypeNone),m_width(0),m_height(0){}Role::~Role(){CC_SAFE_RELEASE_NULL(m_pImageName) ;}bool Role::init(XMLElement * pElement){const char * pId = pElement->Attribute("id") ;IF_NULL_RETURN_FALSE(pId) ;m_id = atoi(pId) ;const char * pImageName = pElement->Attribute("image_name") ;IF_NULL_RETURN_FALSE(pImageName) ;m_pImageName = new __String(pImageName) ;const char* pType = pElement->Attribute("type") ;IF_NULL_RETURN_FALSE(pType);m_type = getTypeByChar(pType) ;const char * pWidth = pElement->Attribute("width") ;IF_NULL_RETURN_FALSE(pWidth);m_width = atoi(pWidth); const char * pHeight = pElement->Attribute("height") ;IF_NULL_RETURN_FALSE(pHeight);m_height = atoi(pHeight); log("Role:%d----%d-----%d",m_id,m_width,m_height);return true;}RoleType Role::getTypeByChar(const char * pType){if (strcmp("boss",pType) == 0){return kRoleTypeBoss ;}else if (strcmp("soldier",pType) == 0){return kRoleTypeSoldier ;}else if (strcmp("general_ver",pType) == 0){return kRoleTypeArmyGeneralVertical;}else if (strcmp("general_hor",pType) == 0){return kRoleTypeArmyGeneralHorizontal;}return kRoleTypeNone ;}



 

 

0 0