标签:cocos2d-x3.4 游戏 华容道
关卡类中需要有没关初始的地图数据,即每个角色的位置。
属性:
ID:
所有RoleID:用到哪个Role写哪个,每个ID用‘,’分开
对应RoleID的行列:读取的时候有一个Row数据和一个Col数据,将这两个数据合并为1个Pos数据。
Level.h
#ifndef _LEVEL_H_
#define _LEVEL_H_
#include "cocos2d.h"
USING_NS_CC ;
#include "tinyxml2/tinyxml2.h"
using namespace tinyxml2 ;
class Level : public Ref
{
public:
static cocos2d::Vector<Level*> s_levelVec ;
static const char * XML_FILE_NAME ;
static bool initStatic();
static bool parseData(XMLElement * pElement) ;
static void finalizeStatic();
static std::vector<std::string> splitString(std::string str,std::string parttern) ;
static std::vector<int> splitInt(std::string str,std::string parttern) ;
public:
Level();
~Level();
bool init(XMLElement * pElement) ;
private:
CC_SYNTHESIZE(int, m_id ,ID);
CC_SYNTHESIZE(std::vector<int> ,m_roleID ,RoleID);
CC_SYNTHESIZE(std::vector<Vec2> ,m_rolePos ,RolePos);
};
#endif
Level.cpp
#include "Level.h"
#define IF_NULL_RETURN_FALSE(_x_) if(_x_ == nullptr) return false
const char * Level::XML_FILE_NAME = "level.xml" ;
Vector<Level*> Level::s_levelVec ;
bool Level::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 Level::parseData(XMLElement * pElement)
{
s_levelVec.clear() ;
XMLElement * child = pElement->FirstChildElement() ;
for (;child;child = child->NextSiblingElement())
{
if (strcmp(child->Value(),"level") == 0)
{
Level * pRol = new Level() ;
if (!pRol->init(child))
{
CC_SAFE_RELEASE_NULL(pRol);
return false ;
}
s_levelVec.pushBack(pRol) ;
pRol->release() ;
}
}
return true ;
}
void Level::finalizeStatic()
{
s_levelVec.clear() ;
}
std::vector<std::string> Level::splitString(std::string str,std::string parttern)
{
std::string::size_type pos;
std::vector<std::string> result ;
str+=parttern;
unsigned int size=str.size();
for(unsigned int i=0; i<size; i++)
{
pos=str.find(parttern,i);
if(pos<size)
{
std::string s=str.substr(i,pos-i);
result.push_back(s);
i=pos+parttern.size()-1;
}
}
return result;
}
std::vector<int> Level::splitInt(std::string str,std::string parttern)
{
std::vector<std::string> strVec = splitString(str,parttern);
std::vector<int> result ;
std::vector<std::string>::iterator iter;
for ( iter = strVec.begin() ; iter != strVec.end() ; iter++ )
{
int value = atoi((*iter).c_str());
result.push_back(value) ;
}
return result ;
}
Level::Level()
:m_id(-1)
{
}
Level::~Level()
{
}
bool Level::init(XMLElement * pElement)
{
IF_NULL_RETURN_FALSE(pElement->Attribute("id")) ;
const char * pId = pElement->Attribute("id") ;
m_id = atoi(pId) ;
IF_NULL_RETURN_FALSE(pElement->Attribute("role_id")) ;
m_roleID = splitInt(pElement->Attribute("role_id"),",") ;
std::vector<int> rowVec ;
IF_NULL_RETURN_FALSE(pElement->Attribute("row")) ;
rowVec = splitInt(pElement->Attribute("row"),",") ;
std::vector<int> colVec ;
IF_NULL_RETURN_FALSE(pElement->Attribute("col")) ;
colVec = splitInt(pElement->Attribute("col"),",") ;
for(unsigned int i = 0; i < m_roleID.size() ; i++)
{
auto pos = Vec2(colVec.at(i),rowVec.at(i)) ;
m_rolePos.push_back(pos) ;
}
CCLOG("ID:%d",m_id) ;
return true ;
}
数据类的使用:
Role::initStatic() ;
Level::initStatic();
在进入的游戏的时候调用,最好在AppDelegate中调用。标签:cocos2d-x3.4 游戏 华容道
原文地址:http://blog.csdn.net/c_boy_lu/article/details/45047325