Odds2_Room.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include "stdafx.h"
  2. #include "tinyxml2/tinyxml2.h"
  3. #include "LuaCfgHelper.h"
  4. #include <iostream>
  5. #include <boost/algorithm/string.hpp>
  6. #include "Odds2_Room.h"
  7. std::auto_ptr<Odds2_Room> Odds2_Room::msSingleton(nullptr);
  8. int Odds2_Room::GetCount()
  9. {
  10. return (int)mMapData.size();
  11. }
  12. const Odds2_RoomData* Odds2_Room::GetData(int RoomID)
  13. {
  14. auto it = mMapData.find(RoomID);
  15. if (it != mMapData.end())
  16. {
  17. return &it->second;
  18. }
  19. return NULL;
  20. }
  21. const std::map<int, Odds2_RoomData>& Odds2_Room::GetMapData()
  22. {
  23. return mMapData;
  24. }
  25. void Odds2_Room::Load()
  26. {
  27. tinyxml2::XMLDocument xmlDoc;
  28. std::string content = FileUtils::getInstance()->getStringFromFile("Config/Odds2_Room.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. Odds2_RoomData data;
  46. data.mRoomID = element->IntAttribute("RoomID");
  47. data.mLaserAdd = element->IntAttribute("LaserAdd");
  48. data.mLaserReduce = element->IntAttribute("LaserReduce");
  49. data.mFormationAdd = element->IntAttribute("FormationAdd");
  50. data.mFormationReduce = element->IntAttribute("FormationReduce");
  51. if (mMapData.find(data.mRoomID) != mMapData.end())std::cout <<"data refind:" << data.mRoomID << std::endl;
  52. CCASSERT(mMapData.find(data.mRoomID) == mMapData.end(), "data.mRoomID is exists");
  53. mMapData.insert(std::make_pair(data.mRoomID, data));
  54. element = element->NextSiblingElement();
  55. }
  56. CCLOG("Odds2_Room Loaded. Load Data:%u", mMapData.size());
  57. }
  58. void Odds2_Room::LoadLua()
  59. {
  60. LuaEngine::getInstance()->executeScriptFile("config/Odds2_Room");
  61. lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
  62. lua_getglobal(L, "Odds2_Room");
  63. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  64. lua_pushstring(L, "datas");
  65. lua_gettable(L, -2);
  66. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  67. lua_pushnil(L);
  68. while(lua_next(L, 2))
  69. {
  70. CCASSERT(lua_istable(L, -1) == 1, "is not table");
  71. Odds2_RoomData data;
  72. LuaCfgHelper::readInt(L, "RoomID", data.mRoomID);
  73. LuaCfgHelper::readInt(L, "LaserAdd", data.mLaserAdd);
  74. LuaCfgHelper::readInt(L, "LaserReduce", data.mLaserReduce);
  75. LuaCfgHelper::readInt(L, "FormationAdd", data.mFormationAdd);
  76. LuaCfgHelper::readInt(L, "FormationReduce", data.mFormationReduce);
  77. if (mMapData.find(data.mRoomID) != mMapData.end())std::cout <<"data refind:" << data.mRoomID << std::endl;
  78. CCASSERT(mMapData.find(data.mRoomID) == mMapData.end(), "data.mRoomID is exists");
  79. mMapData.insert(std::make_pair(data.mRoomID, data));
  80. lua_pop(L, 1);
  81. }
  82. lua_settop(L, 0);
  83. CCLOG("Odds2_Room Loaded. Load Data:%u", mMapData.size());
  84. }
  85. void Odds2_Room::Reload()
  86. {
  87. mMapData.clear();
  88. Load();
  89. }
  90. Odds2_Room* Odds2_Room::GetSingleton()
  91. {
  92. if (msSingleton.get() == nullptr)
  93. {
  94. msSingleton.reset(new Odds2_Room());
  95. }
  96. return msSingleton.get();
  97. }
  98. void Odds2_Room::Release()
  99. {
  100. msSingleton.reset(nullptr);
  101. }