加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

华容道03---关卡类的设计和数据读取

发布时间:2020-12-14 21:05:08 所属栏目:百科 来源:网络整理
导读:关卡类的主要作用就是要存储每个关卡中各个角色的初始位置,方便棋盘的排布。 属性: ID id 棋盘中出现的每个Role的ID role_id 对应上面角色所在的行 row 对应上面角色所在的列 col 为了减少数据的数量, row_id 存储一个字符串,字符串的格式是 ” id,id,id

关卡类的主要作用就是要存储每个关卡中各个角色的初始位置,方便棋盘的排布。

属性:

ID id
棋盘中出现的每个Role的ID role_id
对应上面角色所在的行 row
对应上面角色所在的列 col

为了减少数据的数量,row_id存储一个字符串,字符串的格式是” id,id,id”,rowcol也用这种存储方式。

Xml截图:

代码:

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中调用。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读