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

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

概述关卡类的主要作用就是要存储每个关卡中各个角色的初始位置,方便棋盘的排布。 属性: ID id 棋盘中出现的每个Role的ID role_id 对应上面角色所在的行 row 对应上面角色所在的列 col   为了减少数据的数量,row_id存储一个字符串,字符串的格式是” id,id,id”,row和col也用这种存储方式。 Xml截图: 代码: Level.h #ifndef _LEVEL_H_

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

属性:

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

总结

以上是内存溢出为你收集整理的华容道03---关卡类的设计和数据读取全部内容,希望文章能够帮你解决华容道03---关卡类的设计和数据读取所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1054815.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存