#include "stdafx.h" #include "tinyxml2/tinyxml2.h" #include "LuaCfgHelper.h" #include #include #include "M_hero.h" std::auto_ptr M_hero::msSingleton(nullptr); int M_hero::GetCount() { return (int)mMapData.size(); } const M_heroData* M_hero::GetData(int ID) { auto it = mMapData.find(ID); if (it != mMapData.end()) { return &it->second; } return NULL; } const std::map& M_hero::GetMapData() { return mMapData; } void M_hero::Load() { tinyxml2::XMLDocument xmlDoc; std::string content = FileUtils::getInstance()->getStringFromFile("Config/M_hero.xml"); auto result = xmlDoc.Parse(content.c_str(), content.length()); if (result != tinyxml2::XML_SUCCESS) { CCLOGERROR("Result:%d", result); CCASSERT(false, "result != tinyxml2::XML_SUCCESS"); return; } auto root = xmlDoc.RootElement(); if (root == NULL) { CCASSERT(false, "root == NULL"); return; } auto element = root->FirstChildElement("Data"); while (element != NULL) { M_heroData data; data.mID = element->IntAttribute("ID"); data.mname = element->Attribute("name"); data.mgrade_type = element->IntAttribute("grade_type"); data.mdefault_have = element->IntAttribute("default_have"); data.mskill_id = element->IntAttribute("skill_id"); data.mattribute = element->IntAttribute("attribute"); data.matk = element->IntAttribute("atk"); data.micon = element->Attribute("icon"); data.maspect = element->Attribute("aspect"); data.mspeed = element->FloatAttribute("speed"); data.mzoom_factor = element->FloatAttribute("zoom_factor"); if (mMapData.find(data.mID) != mMapData.end())std::cout <<"data refind:" << data.mID << std::endl; CCASSERT(mMapData.find(data.mID) == mMapData.end(), "data.mID is exists"); mMapData.insert(std::make_pair(data.mID, data)); element = element->NextSiblingElement(); } CCLOG("M_hero Loaded. Load Data:%u", mMapData.size()); } void M_hero::LoadLua() { LuaEngine::getInstance()->executeScriptFile("config/M_hero"); lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState(); lua_getglobal(L, "M_hero"); CCASSERT(lua_istable(L, -1) == 1, "is not table"); lua_pushstring(L, "datas"); lua_gettable(L, -2); CCASSERT(lua_istable(L, -1) == 1, "is not table"); lua_pushnil(L); while(lua_next(L, 2)) { CCASSERT(lua_istable(L, -1) == 1, "is not table"); M_heroData data; LuaCfgHelper::readInt(L, "ID", data.mID); LuaCfgHelper::readString(L, "name", data.mname); LuaCfgHelper::readInt(L, "grade_type", data.mgrade_type); LuaCfgHelper::readInt(L, "default_have", data.mdefault_have); LuaCfgHelper::readInt(L, "skill_id", data.mskill_id); LuaCfgHelper::readInt(L, "attribute", data.mattribute); LuaCfgHelper::readInt(L, "atk", data.matk); LuaCfgHelper::readString(L, "icon", data.micon); LuaCfgHelper::readString(L, "aspect", data.maspect); LuaCfgHelper::readFloat(L, "speed", data.mspeed); LuaCfgHelper::readFloat(L, "zoom_factor", data.mzoom_factor); if (mMapData.find(data.mID) != mMapData.end())std::cout <<"data refind:" << data.mID << std::endl; CCASSERT(mMapData.find(data.mID) == mMapData.end(), "data.mID is exists"); mMapData.insert(std::make_pair(data.mID, data)); lua_pop(L, 1); } lua_settop(L, 0); CCLOG("M_hero Loaded. Load Data:%u", mMapData.size()); } void M_hero::Reload() { mMapData.clear(); Load(); } M_hero* M_hero::GetSingleton() { if (msSingleton.get() == nullptr) { msSingleton.reset(new M_hero()); } return msSingleton.get(); } void M_hero::Release() { msSingleton.reset(nullptr); }