GameMapReader.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /****************************************************************************
  2. Copyright (c) 2014 cocos2d-x.org
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #include "editor-support/cocostudio/WidgetReader/GameMapReader/GameMapReader.h"
  21. #include "2d/CCLabel.h"
  22. #include "2d/CCTMXXMLParser.h"
  23. #include "2d/CCTMXTiledMap.h"
  24. #include "platform/CCFileUtils.h"
  25. #include "deprecated/CCString.h"
  26. #include "editor-support/cocostudio/CSParseBinary_generated.h"
  27. #include "editor-support/cocostudio/WidgetReader/NodeReader/NodeReader.h"
  28. #include "tinyxml2.h"
  29. #include "flatbuffers/flatbuffers.h"
  30. USING_NS_CC;
  31. using namespace flatbuffers;
  32. namespace cocostudio
  33. {
  34. IMPLEMENT_CLASS_NODE_READER_INFO(GameMapReader)
  35. GameMapReader::GameMapReader()
  36. {
  37. }
  38. GameMapReader::~GameMapReader()
  39. {
  40. }
  41. static GameMapReader* _instanceTMXTiledMapReader = nullptr;
  42. GameMapReader* GameMapReader::getInstance()
  43. {
  44. if (!_instanceTMXTiledMapReader)
  45. {
  46. _instanceTMXTiledMapReader = new (std::nothrow) GameMapReader();
  47. }
  48. return _instanceTMXTiledMapReader;
  49. }
  50. void GameMapReader::destroyInstance()
  51. {
  52. CC_SAFE_DELETE(_instanceTMXTiledMapReader);
  53. }
  54. Offset<Table> GameMapReader::createOptionsWithFlatBuffers(const tinyxml2::XMLElement *objectData,
  55. flatbuffers::FlatBufferBuilder *builder)
  56. {
  57. auto temp = NodeReader::getInstance()->createOptionsWithFlatBuffers(objectData, builder);
  58. auto nodeOptions = *(Offset<WidgetOptions>*)(&temp);
  59. std::string path = "";
  60. std::string plistFile = "";
  61. int resourceType = 0;
  62. // child elements
  63. const tinyxml2::XMLElement* child = objectData->FirstChildElement();
  64. while (child)
  65. {
  66. std::string name = child->Name();
  67. if (name == "FileData")
  68. {
  69. const tinyxml2::XMLAttribute* attribute = child->FirstAttribute();
  70. while (attribute)
  71. {
  72. name = attribute->Name();
  73. std::string value = attribute->Value();
  74. if (name == "Path")
  75. {
  76. path = value;
  77. }
  78. else if (name == "Type")
  79. {
  80. resourceType = 0;
  81. }
  82. else if (name == "Plist")
  83. {
  84. plistFile = value;
  85. }
  86. attribute = attribute->Next();
  87. }
  88. }
  89. child = child->NextSiblingElement();
  90. }
  91. auto options = CreateGameMapOptions(*builder,
  92. nodeOptions,
  93. CreateResourceData(*builder,
  94. builder->CreateString(path),
  95. builder->CreateString(plistFile),
  96. resourceType));
  97. return *(Offset<Table>*)(&options);
  98. }
  99. void GameMapReader::setPropsWithFlatBuffers(cocos2d::Node *node,
  100. const flatbuffers::Table *gameMapOptions)
  101. {
  102. auto options = (GameMapOptions*)gameMapOptions;
  103. auto nodeReader = NodeReader::getInstance();
  104. nodeReader->setPropsWithFlatBuffers(node, (Table*)options->nodeOptions());
  105. }
  106. Node* GameMapReader::createNodeWithFlatBuffers(const flatbuffers::Table *gameMapOptions)
  107. {
  108. TMXTiledMap* tmx = nullptr;
  109. auto options = (GameMapOptions*)gameMapOptions;
  110. auto fileNameData = options->fileNameData();
  111. bool fileExist = false;
  112. std::string errorFilePath = "";
  113. std::string path = fileNameData->path()->c_str();
  114. int resourceType = fileNameData->resourceType();
  115. switch (resourceType)
  116. {
  117. case 0:
  118. {
  119. if (FileUtils::getInstance()->isFileExist(path))
  120. {
  121. fileExist = true;
  122. }
  123. else
  124. {
  125. errorFilePath = path;
  126. fileExist = false;
  127. }
  128. break;
  129. }
  130. default:
  131. break;
  132. }
  133. if (fileExist)
  134. {
  135. /* Whether tileset is valid. */
  136. auto mapInfo = TMXMapInfo::create(path);
  137. auto& layers = mapInfo->getLayers();
  138. bool valid = false;
  139. std::string layerName = "";
  140. for (const auto &layerInfo : layers)
  141. {
  142. valid = false;
  143. if (layerInfo->_visible)
  144. {
  145. Size size = layerInfo->_layerSize;
  146. auto& tilesets = mapInfo->getTilesets();
  147. if (tilesets.size()>0)
  148. {
  149. TMXTilesetInfo* tileset = nullptr;
  150. for (auto iter = tilesets.crbegin(); iter != tilesets.crend(); ++iter)
  151. {
  152. tileset = *iter;
  153. if (tileset)
  154. {
  155. for( int y=0; y < size.height; y++ )
  156. {
  157. for( int x=0; x < size.width; x++ )
  158. {
  159. int pos = static_cast<int>(x + size.width * y);
  160. int gid = layerInfo->_tiles[ pos ];
  161. if( gid != 0 )
  162. {
  163. if( (gid & kTMXFlippedMask) >= tileset->_firstGid )
  164. {
  165. valid = true;
  166. break;
  167. }
  168. }
  169. }
  170. if (valid)
  171. {
  172. break;
  173. }
  174. }
  175. }
  176. }
  177. }
  178. if (!valid)
  179. {
  180. layerName = layerInfo->_name;
  181. break;
  182. }
  183. }
  184. else
  185. {
  186. valid = true;
  187. }
  188. }
  189. if (!valid)
  190. {
  191. Node* node = Node::create();
  192. setPropsWithFlatBuffers(node, (Table*)gameMapOptions);
  193. auto label = Label::create();
  194. label->setString(__String::createWithFormat("Some error of gid are in TMX Layer '%s'", layerName.c_str())->getCString());
  195. node->setScale(1.0f);
  196. node->addChild(label);
  197. return node;
  198. }
  199. /**/
  200. tmx = TMXTiledMap::create(path);
  201. if (tmx)
  202. {
  203. //prevent that editor's data does not match in size and resources
  204. Size fileSize = tmx->getContentSize();
  205. setPropsWithFlatBuffers(tmx, (Table*)gameMapOptions);
  206. tmx->setContentSize(fileSize);
  207. }
  208. }
  209. else
  210. {
  211. Node* node = Node::create();
  212. setPropsWithFlatBuffers(node, (Table*)gameMapOptions);
  213. return node;
  214. }
  215. return tmx;
  216. }
  217. }