2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2014 cocos2d-x.org
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
https://axmolengine.github.io/
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-10-21 10:12:00 +08:00
|
|
|
#include "WidgetReader/GameMapReader/GameMapReader.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
#include "2d/CCLabel.h"
|
|
|
|
#include "2d/CCTMXXMLParser.h"
|
2020-08-28 15:42:40 +08:00
|
|
|
#include "2d/CCFastTMXTiledMap.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
2019-11-26 18:07:51 +08:00
|
|
|
#include "base/ccUTF8.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2020-10-21 10:12:00 +08:00
|
|
|
#include "CSParseBinary_generated.h"
|
|
|
|
#include "WidgetReader/NodeReader/NodeReader.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
#include "flatbuffers/flatbuffers.h"
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
USING_NS_AX;
|
2019-11-23 20:27:39 +08:00
|
|
|
using namespace flatbuffers;
|
|
|
|
|
|
|
|
namespace cocostudio
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
IMPLEMENT_CLASS_NODE_READER_INFO(GameMapReader)
|
|
|
|
|
|
|
|
GameMapReader::GameMapReader() {}
|
|
|
|
|
|
|
|
GameMapReader::~GameMapReader() {}
|
|
|
|
|
|
|
|
static GameMapReader* _instanceTMXTiledMapReader = nullptr;
|
|
|
|
|
|
|
|
GameMapReader* GameMapReader::getInstance()
|
|
|
|
{
|
|
|
|
if (!_instanceTMXTiledMapReader)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_instanceTMXTiledMapReader = new GameMapReader();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
return _instanceTMXTiledMapReader;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GameMapReader::destroyInstance()
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_DELETE(_instanceTMXTiledMapReader);
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Offset<Table> GameMapReader::createOptionsWithFlatBuffers(pugi::xml_node objectData,
|
|
|
|
flatbuffers::FlatBufferBuilder* builder)
|
|
|
|
{
|
|
|
|
auto temp = NodeReader::getInstance()->createOptionsWithFlatBuffers(objectData, builder);
|
|
|
|
auto nodeOptions = *(Offset<WidgetOptions>*)(&temp);
|
|
|
|
|
|
|
|
std::string path;
|
|
|
|
std::string plistFile;
|
|
|
|
int resourceType = 0;
|
|
|
|
|
|
|
|
// child elements
|
|
|
|
auto child = objectData.first_child();
|
|
|
|
while (child)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 15:49:45 +08:00
|
|
|
std::string_view name = child.name();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
if (name == "FileData")
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto attribute = child.first_attribute();
|
|
|
|
|
|
|
|
while (attribute)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
name = attribute.name();
|
2021-12-31 15:49:45 +08:00
|
|
|
std::string_view value = attribute.value();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
if (name == "Path")
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
path = value;
|
|
|
|
}
|
|
|
|
else if (name == "Type")
|
|
|
|
{
|
|
|
|
resourceType = 0;
|
|
|
|
}
|
|
|
|
else if (name == "Plist")
|
|
|
|
{
|
|
|
|
plistFile = value;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
attribute = attribute.next_attribute();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
child = child.next_sibling();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
auto options = CreateGameMapOptions(
|
|
|
|
*builder, nodeOptions,
|
|
|
|
CreateResourceData(*builder, builder->CreateString(path), builder->CreateString(plistFile), resourceType));
|
|
|
|
|
|
|
|
return *(Offset<Table>*)(&options);
|
|
|
|
}
|
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
void GameMapReader::setPropsWithFlatBuffers(ax::Node* node, const flatbuffers::Table* gameMapOptions)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
|
|
|
auto options = (GameMapOptions*)gameMapOptions;
|
|
|
|
auto nodeReader = NodeReader::getInstance();
|
|
|
|
nodeReader->setPropsWithFlatBuffers(node, (Table*)options->nodeOptions());
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* GameMapReader::createNodeWithFlatBuffers(const flatbuffers::Table* gameMapOptions)
|
|
|
|
{
|
|
|
|
FastTMXTiledMap* tmx = nullptr;
|
|
|
|
|
|
|
|
auto options = (GameMapOptions*)gameMapOptions;
|
|
|
|
auto fileNameData = options->fileNameData();
|
|
|
|
|
|
|
|
bool fileExist = false;
|
|
|
|
std::string errorFilePath;
|
|
|
|
std::string path = fileNameData->path()->c_str();
|
|
|
|
int resourceType = fileNameData->resourceType();
|
|
|
|
switch (resourceType)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
case 0:
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (FileUtils::getInstance()->isFileExist(path))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
fileExist = true;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
errorFilePath = path;
|
|
|
|
fileExist = false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (fileExist)
|
|
|
|
{
|
|
|
|
/* Whether tileset is valid. */
|
|
|
|
auto mapInfo = TMXMapInfo::create(path);
|
|
|
|
auto& layers = mapInfo->getLayers();
|
|
|
|
bool valid = false;
|
|
|
|
std::string layerName;
|
|
|
|
for (const auto& layerInfo : layers)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
valid = false;
|
|
|
|
|
|
|
|
if (layerInfo->_visible)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Size size = layerInfo->_layerSize;
|
|
|
|
auto& tilesets = mapInfo->getTilesets();
|
|
|
|
if (tilesets.size() > 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
TMXTilesetInfo* tileset = nullptr;
|
|
|
|
for (auto iter = tilesets.crbegin(); iter != tilesets.crend(); ++iter)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
tileset = *iter;
|
|
|
|
if (tileset)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
for (int y = 0; y < size.height; y++)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
for (int x = 0; x < size.width; x++)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
int pos = static_cast<int>(x + size.width * y);
|
|
|
|
int gid = layerInfo->_tiles[pos];
|
|
|
|
|
|
|
|
if (gid != 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if ((gid & kTMXFlippedMask) >= tileset->_firstGid)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
valid = true;
|
|
|
|
break;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (valid)
|
|
|
|
{
|
|
|
|
break;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
if (!valid)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
layerName = layerInfo->_name;
|
|
|
|
break;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
valid = true;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
if (!valid)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
Node* node = Node::create();
|
|
|
|
setPropsWithFlatBuffers(node, (Table*)gameMapOptions);
|
2021-12-25 10:04:45 +08:00
|
|
|
auto label = Label::create();
|
|
|
|
label->setString(StringUtils::format("Some error of gid are in TMX Layer '%s'", layerName.c_str()));
|
|
|
|
node->setScale(1.0f);
|
|
|
|
node->addChild(label);
|
2019-11-23 20:27:39 +08:00
|
|
|
return node;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
/**/
|
|
|
|
|
|
|
|
tmx = FastTMXTiledMap::create(path);
|
|
|
|
if (tmx)
|
|
|
|
{
|
|
|
|
// prevent that editor's data does not match in size and resources
|
|
|
|
Size fileSize = tmx->getContentSize();
|
|
|
|
setPropsWithFlatBuffers(tmx, (Table*)gameMapOptions);
|
|
|
|
tmx->setContentSize(fileSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Node* node = Node::create();
|
|
|
|
setPropsWithFlatBuffers(node, (Table*)gameMapOptions);
|
|
|
|
return node;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
return tmx;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
} // namespace cocostudio
|