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 = 0,
        _Reverse =1,
        _Both   = 2,
    };

};


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(0 != xmlStrcmp(BAD_CAST "bionts", pRootNode->name))
        {
            break;
        }
        //读取节点
        xmlNodePtr pCurNode = pRootNode->xmlChildrenNode;
        while (NULL != pCurNode)
        {
            if (0 != 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 (0);

    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(0 != xmlStrcmp(BAD_CAST "maps", pRootNode->name))
        {
            break;
        }
        //读取节点
        xmlNodePtr pElement = pRootNode->xmlChildrenNode;
        while (NULL != pElement)
        {
            if (0 == 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 = 0; i < vecData.size(); i++)
                {
                    vector<string> vecPos;
                    CXCommon::split(vecData[i], string(","), vecPos);
                    if (!vecPos.empty())
                    {
                        mapInfo.foundationPos.push_back(CCPoint(atof(vecPos[0].c_str()), atof(vecPos[1].c_str())));
                    }
                }
                
                vector<string> vecPath;
                CXCommon::split(CCXmlReader::getXMLNodeAttribStrs(&pElement, "path"), string(";"), vecPath);
                for (unsigned int i = 0; i < vecPath.size(); i++)
                {
                    vector<string> vecPos;
                    CXCommon::split(vecPath[i], string(","), vecPos);
                    if (!vecPos.empty())
                    {
                        mapInfo.path.push_back(CCPoint(atof(vecPos[0].c_str()), atof(vecPos[1].c_str())));
                    }
                }

                m_mapMapInfo[mapInfo.nId] = mapInfo;
            }
            pElement = pElement->next;
        }
        xmlFreeDoc(pDoc);
        return true;
    } while (0);

    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;
}

 

你可能感兴趣的:(config)