CGameConfig类
#ifndef __GAMECONFIG_H__
#define __GAMECONFIG_H__ #include "GameFrameHead.h"
#include "GameParam.h" //生物配置
struct BiontInfo
{
int nId;
int nType; //生物类型
CCRect rArea; //生物活动区域
int nBulk; //体积
int nWeight; //重量 string strFile; //贴图文件
string strAudio; //声音文件
string strAction; //动作
int nTag; //标签
string strLap;
int nDirect; //方向 enum DirectType
{
_Normal = ,
_Reverse =,
_Both = ,
}; }; struct MapInfo
{
int nId;
vector<CCPoint> foundationPos;
vector<CCPoint> path; }; class CGameConfig
{
public:
~CGameConfig(); static CGameConfig* getInstance();
static void destroy();
void release(); //设置资源路径 一开始就要设定
void setResourcePath(const char* psPath);
string getResourcePath();
public: bool loadBiontInfo(); //生物配置
bool loadMapInfo(); //地图配置 vector<BiontInfo>* getBiontInfo();
BiontInfo* getBiontInfoByType(int nType); map<int, MapInfo>* getMapInfo();
MapInfo* getMapInfoById(int nId); private:
CGameConfig();
private:
static CGameConfig* g_pGameConfig;
string m_strResourcePath; private: //生物配置标识
vector<BiontInfo> m_vecBiontInfo; //地图配置
map<int, MapInfo> m_mapMapInfo; }; #endif //__GAMECONFIG_H__
#include "GameConfig.h"
#include "XXmlReader.h"
#include "XEncryptAccess.h"
#include "XCommon.h" CGameConfig* CGameConfig::g_pGameConfig = NULL; CGameConfig::CGameConfig()
{ } CGameConfig::~CGameConfig()
{ } CGameConfig* CGameConfig::getInstance()
{
if (!g_pGameConfig)
{
g_pGameConfig = new CGameConfig();
}
return g_pGameConfig;
} void CGameConfig::destroy()
{
SAFE_DELETE(g_pGameConfig);
} void CGameConfig::release()
{ } void CGameConfig::setResourcePath( const char* psPath )
{
m_strResourcePath = psPath;
} string CGameConfig::getResourcePath()
{
return m_strResourcePath;
} bool CGameConfig::loadBiontInfo()
{
string strFile = m_strResourcePath;
strFile += "/config/biont.xml"; //读取文档
xmlDocPtr pDoc = NULL;
LOAD_XML_DOC(strFile.c_str(), pDoc); if (NULL == pDoc)
{
CCLog("can not read %s", strFile.c_str());
return false;
} do
{
xmlNodePtr pRootNode = xmlDocGetRootElement(pDoc);
if (NULL == pRootNode)
{
break;
} if( != xmlStrcmp(BAD_CAST "bionts", pRootNode->name))
{
break;
}
//读取节点
xmlNodePtr pCurNode = pRootNode->xmlChildrenNode;
while (NULL != pCurNode)
{
if ( != xmlStrcmp(pCurNode->name, BAD_CAST "biont"))
{
pCurNode = pCurNode->next;
continue;
} BiontInfo info;
info.nType = CCXmlReader::getXMLNodeAttribInt(&pCurNode, "type");
info.strFile = CCXmlReader::getXMLNodeAttribStrs(&pCurNode, "file");
info.strAudio = CCXmlReader::getXMLNodeAttribStrs(&pCurNode, "audio");
info.nBulk = CCXmlReader::getXMLNodeAttribInt(&pCurNode, "bulk");
string strBuf = CCXmlReader::getXMLNodeAttribStrs(&pCurNode,"area");
int nX, nY, nW, nH;
sscanf(strBuf.c_str(),"%d %d %d %d",&nX, &nY, &nW, &nH);
info.rArea = CCRect(nX,nY,nW,nH);
info.strAction = CCXmlReader::getXMLNodeAttribStrs(&pCurNode, "action");
info.nTag = CCXmlReader::getXMLNodeAttribInt(&pCurNode, "tag");
info.strLap = CCXmlReader::getXMLNodeAttribStrs(&pCurNode, "lap");
info.nDirect = CCXmlReader::getXMLNodeAttribInt(&pCurNode, "direct"); m_vecBiontInfo.push_back(info); pCurNode = pCurNode->next;
} xmlFreeDoc(pDoc);
return true; } while (); xmlFreeDoc(pDoc);
CCLog("read xml error : %s", strFile.c_str());
return false;
} vector<BiontInfo>* CGameConfig::getBiontInfo()
{
return &m_vecBiontInfo;
} BiontInfo* CGameConfig::getBiontInfoByType( int nType )
{
for (vector<BiontInfo>::iterator it = m_vecBiontInfo.begin(); it != m_vecBiontInfo.end(); it++)
{
if (nType == it->nType)
{
return &(*it);
}
}
CCLog("error: CGameConfig::getBiontInfoByType");
return NULL;
} bool CGameConfig::loadMapInfo()
{
string strFile = m_strResourcePath;
strFile += "/config/map.xml"; //读取文档
xmlDocPtr pDoc = NULL;
LOAD_XML_DOC(strFile.c_str(), pDoc); if (NULL == pDoc)
{
CCLog("can not read %s", strFile.c_str());
return false;
} do
{
xmlNodePtr pRootNode = xmlDocGetRootElement(pDoc);
if (NULL == pRootNode)
{
break;
} if( != xmlStrcmp(BAD_CAST "maps", pRootNode->name))
{
break;
}
//读取节点
xmlNodePtr pElement = pRootNode->xmlChildrenNode;
while (NULL != pElement)
{
if ( == xmlStrcmp(pElement->name, BAD_CAST "map"))
{
MapInfo mapInfo;
mapInfo.nId = CCXmlReader::getXMLNodeAttribInt(&pElement, "id"); vector<string> vecData;
CXCommon::split(CCXmlReader::getXMLNodeAttribStrs(&pElement, "data"), string(";"), vecData);
for (unsigned int i = ; i < vecData.size(); i++)
{
vector<string> vecPos;
CXCommon::split(vecData[i], string(","), vecPos);
if (!vecPos.empty())
{
mapInfo.foundationPos.push_back(CCPoint(atof(vecPos[].c_str()), atof(vecPos[].c_str())));
}
} vector<string> vecPath;
CXCommon::split(CCXmlReader::getXMLNodeAttribStrs(&pElement, "path"), string(";"), vecPath);
for (unsigned int i = ; i < vecPath.size(); i++)
{
vector<string> vecPos;
CXCommon::split(vecPath[i], string(","), vecPos);
if (!vecPos.empty())
{
mapInfo.path.push_back(CCPoint(atof(vecPos[].c_str()), atof(vecPos[].c_str())));
}
} m_mapMapInfo[mapInfo.nId] = mapInfo;
}
pElement = pElement->next;
}
xmlFreeDoc(pDoc);
return true;
} while (); xmlFreeDoc(pDoc);
CCLog("read xml error : %s", strFile.c_str());
return false;
} map<int, MapInfo>* CGameConfig::getMapInfo()
{
return &m_mapMapInfo;
} MapInfo* CGameConfig::getMapInfoById( int nId )
{
for (map<int, MapInfo>::iterator it = m_mapMapInfo.begin(); it != m_mapMapInfo.end(); it++)
{
if (nId == it->first)
{
return &(it->second);
}
}
CCLog("error: CGameConfig::getMapInfoById");
return NULL;
}
CGameConfig类的更多相关文章
- Java类的继承与多态特性-入门笔记
相信对于继承和多态的概念性我就不在怎么解释啦!不管你是.Net还是Java面向对象编程都是比不缺少一堂课~~Net如此Java亦也有同样的思想成分包含其中. 继承,多态,封装是Java面向对象的3大特 ...
- C++ 可配置的类工厂
项目中常用到工厂模式,工厂模式可以把创建对象的具体细节封装到Create函数中,减少重复代码,增强可读和可维护性.传统的工厂实现如下: class Widget { public: virtual i ...
- Android请求网络共通类——Hi_博客 Android App 开发笔记
今天 ,来分享一下 ,一个博客App的开发过程,以前也没开发过这种类型App 的经验,求大神们轻点喷. 首先我们要创建一个Andriod 项目 因为要从网络请求数据所以我们先来一个请求网络的共通类. ...
- ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之第二章:利用模型类创建视图、控制器和数据库
在这一章中,我们将直接进入项目,并且为产品和分类添加一些基本的模型类.我们将在Entity Framework的代码优先模式下,利用这些模型类创建一个数据库.我们还将学习如何在代码中创建数据库上下文类 ...
- ASP.NET Core 折腾笔记二:自己写个完整的Cache缓存类来支持.NET Core
背景: 1:.NET Core 已经没System.Web,也木有了HttpRuntime.Cache,因此,该空间下Cache也木有了. 2:.NET Core 有新的Memory Cache提供, ...
- .NET Core中间件的注册和管道的构建(2)---- 用UseMiddleware扩展方法注册中间件类
.NET Core中间件的注册和管道的构建(2)---- 用UseMiddleware扩展方法注册中间件类 0x00 为什么要引入扩展方法 有的中间件功能比较简单,有的则比较复杂,并且依赖其它组件.除 ...
- Java基础Map接口+Collections工具类
1.Map中我们主要讲两个接口 HashMap 与 LinkedHashMap (1)其中LinkedHashMap是有序的 怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...
- PHP-解析验证码类--学习笔记
1.开始 在 网上看到使用PHP写的ValidateCode生成验证码码类,感觉不错,特拿来分析学习一下. 2.类图 3.验证码类部分代码 3.1 定义变量 //随机因子 private $char ...
- C# 多种方式发送邮件(附帮助类)
因项目业务需要,需要做一个发送邮件功能,查了下资料,整了整,汇总如下,亲测可用- QQ邮箱发送邮件 #region 发送邮箱 try { MailMessage mail = new MailMess ...
随机推荐
- linux服务器之间的文件同步;rsync+inotifywait;同步多个目录
1.双向同步:unison+inotify 2.单向同步:rsync+inotify python版的pyinotify 本文介绍第二种方法: 1.Inotify 是一个 Linux特性,它监控文件系 ...
- Qt Creator怎样更改默认构建目录
用过VS的朋友都知道,用VS编译工程时会将生成的可执行文件放在当前工程目录下,使每个工程独立地成为一个整体,管理起来颇为方便:而Qt Creator则不同,编译程序时会创建一个与当前工程目录同级的构建 ...
- [Ubuntu]明明白白安装中文字体
Ubuntu 6.06 dapper对中文的支持应该说是一向不错的,包括现在默认安装就包含了scim输入法.然而,一般安装的ubuntu虽然能够显示.输入中文了,但总觉 得所显示的中文像歪瓜裂枣,很是 ...
- poj 1035 Spell checker ( 字符串处理 )
Spell checker Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 16675 Accepted: 6087 De ...
- Unity3D新手教学,让你十二小时,从入门到掌握!(三 ) [转]
版权声明:本文为Aries原创文章,转载请标明出处.如有不足之处欢迎提出意见或建议,联系QQ531193915 这一讲,我会教大家如何写碰撞检测的代码,然后还会教大家如何使用我介绍给大家的第一个Uni ...
- ScaleIO 1.2 基础
The ScaleIO virtual SAN consists of 3 software components =================== Meta Data Manager (MDM ...
- Hadoop中Partition深度解析
本文地址:http://www.cnblogs.com/archimedes/p/hadoop-partitioner.html,转载请注明源地址. 旧版 API 的 Partitioner 解析 P ...
- keytool命令总结
keytool 命令总结 一.创建数字证书 交互模式 使用默认的密钥库.keystore(目录是c: Documents and Setting用户名)和算法(DSA) keytool -genkey ...
- 排序算法及其java实现
各种排序算法:冒择路(入)兮(稀)快归堆,桶式排序,基数排序 冒泡排序,选择排序,插入排序,稀尔排序,快速排序,归并排序,堆排序,桶式排序,基数排序 一.冒泡排序(BubbleSort) 1. 基本思 ...
- UNIX网络编程卷2进程间通信读书笔记(二)—管道 (1)
一.管道 管道的名称很形象,它就像是一个水管,我们从一端到水然后水从令一端流出.不同的是这里说的管道的两边都是进程.从一端往管道里写数据,其它进程可以从管道的另一端的把数据读出,从而实现了进程间通信的 ...