M_hero.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include "stdafx.h"
  2. #include "tinyxml2/tinyxml2.h"
  3. #include "LuaCfgHelper.h"
  4. #include <iostream>
  5. #include <boost/algorithm/string.hpp>
  6. #include "M_hero.h"
  7. std::auto_ptr<M_hero> M_hero::msSingleton(nullptr);
  8. int M_hero::GetCount()
  9. {
  10. return (int)mMapData.size();
  11. }
  12. const M_heroData* M_hero::GetData(int ID)
  13. {
  14. auto it = mMapData.find(ID);
  15. if (it != mMapData.end())
  16. {
  17. return &it->second;
  18. }
  19. return NULL;
  20. }
  21. const std::map<int, M_heroData>& M_hero::GetMapData()
  22. {
  23. return mMapData;
  24. }
  25. void M_hero::Load()
  26. {
  27. tinyxml2::XMLDocument xmlDoc;
  28. std::string content = FileUtils::getInstance()->getStringFromFile("Config/M_hero.xml");
  29. auto result = xmlDoc.Parse(content.c_str(), content.length());
  30. if (result != tinyxml2::XML_SUCCESS)
  31. {
  32. CCLOGERROR("Result:%d", result);
  33. CCASSERT(false, "result != tinyxml2::XML_SUCCESS");
  34. return;
  35. }
  36. auto root = xmlDoc.RootElement();
  37. if (root == NULL)
  38. {
  39. CCASSERT(false, "root == NULL");
  40. return;
  41. }
  42. auto element = root->FirstChildElement("Data");
  43. while (element != NULL)
  44. {
  45. M_heroData data;
  46. data.mID = element->IntAttribute("ID");
  47. data.mname = element->Attribute("name");
  48. data.mgrade_type = element->IntAttribute("grade_type");
  49. data.mdefault_have = element->IntAttribute("default_have");
  50. data.mskill_id = element->IntAttribute("skill_id");
  51. data.mattribute = element->IntAttribute("attribute");
  52. data.matk = element->IntAttribute("atk");
  53. data.micon = element->Attribute("icon");
  54. data.maspect = element->Attribute("aspect");
  55. data.mspeed = element->FloatAttribute("speed");
  56. data.mzoom_factor = element->FloatAttribute("zoom_factor");
  57. if (mMapData.find(data.mID) != mMapData.end())std::cout <<"data refind:" << data.mID << std::endl;
  58. CCASSERT(mMapData.find(data.mID) == mMapData.end(), "data.mID is exists");
  59. mMapData.insert(std::make_pair(data.mID, data));
  60. element = element->NextSiblingElement();
  61. }
  62. CCLOG("M_hero Loaded. Load Data:%u", mMapData.size());
  63. }
  64. void M_hero::LoadLua()
  65. {
  66. LuaEngine::getInstance()->executeScriptFile("config/M_hero");
  67. lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
  68. lua_getglobal(L, "M_hero");
  69. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  70. lua_pushstring(L, "datas");
  71. lua_gettable(L, -2);
  72. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  73. lua_pushnil(L);
  74. while(lua_next(L, 2))
  75. {
  76. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  77. M_heroData data;
  78. LuaCfgHelper::readInt(L, "ID", data.mID);
  79. LuaCfgHelper::readString(L, "name", data.mname);
  80. LuaCfgHelper::readInt(L, "grade_type", data.mgrade_type);
  81. LuaCfgHelper::readInt(L, "default_have", data.mdefault_have);
  82. LuaCfgHelper::readInt(L, "skill_id", data.mskill_id);
  83. LuaCfgHelper::readInt(L, "attribute", data.mattribute);
  84. LuaCfgHelper::readInt(L, "atk", data.matk);
  85. LuaCfgHelper::readString(L, "icon", data.micon);
  86. LuaCfgHelper::readString(L, "aspect", data.maspect);
  87. LuaCfgHelper::readFloat(L, "speed", data.mspeed);
  88. LuaCfgHelper::readFloat(L, "zoom_factor", data.mzoom_factor);
  89. if (mMapData.find(data.mID) != mMapData.end())std::cout <<"data refind:" << data.mID << std::endl;
  90. CCASSERT(mMapData.find(data.mID) == mMapData.end(), "data.mID is exists");
  91. mMapData.insert(std::make_pair(data.mID, data));
  92. lua_pop(L, 1);
  93. }
  94. lua_settop(L, 0);
  95. CCLOG("M_hero Loaded. Load Data:%u", mMapData.size());
  96. }
  97. void M_hero::Reload()
  98. {
  99. mMapData.clear();
  100. Load();
  101. }
  102. M_hero* M_hero::GetSingleton()
  103. {
  104. if (msSingleton.get() == nullptr)
  105. {
  106. msSingleton.reset(new M_hero());
  107. }
  108. return msSingleton.get();
  109. }
  110. void M_hero::Release()
  111. {
  112. msSingleton.reset(nullptr);
  113. }