CGameConfig类
阅读原文时间:2023年07月14日阅读:1

#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 foundationPos;
vector 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* CGameConfig::getBiontInfo()
{
return &m_vecBiontInfo;
}

BiontInfo* CGameConfig::getBiontInfoByType( int nType )
{
for (vector::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* CGameConfig::getMapInfo()
{
return &m_mapMapInfo;
}

MapInfo* CGameConfig::getMapInfoById( int nId )
{
for (map::iterator it = m_mapMapInfo.begin(); it != m_mapMapInfo.end(); it++)
{
if (nId == it->first)
{
return &(it->second);
}
}
CCLog("error: CGameConfig::getMapInfoById");
return NULL;
}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章