2014-06-06 16:15:46 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2009-2010 Ricardo Quesada
|
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2011 Zynga Inc.
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2014-06-06 16:15:46 +08:00
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
2014-08-28 17:03:29 +08:00
|
|
|
#include "2d/CCFastTMXTiledMap.h"
|
|
|
|
#include "2d/CCFastTMXLayer.h"
|
2016-06-15 15:01:26 +08:00
|
|
|
#include "base/ccUTF8.h"
|
2014-06-06 16:15:46 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2014-06-19 15:57:13 +08:00
|
|
|
// implementation FastTMXTiledMap
|
2014-06-06 16:15:46 +08:00
|
|
|
|
2019-10-23 14:58:31 +08:00
|
|
|
FastTMXTiledMap * FastTMXTiledMap::create(const std::string& tmxFile)
|
2014-06-06 16:15:46 +08:00
|
|
|
{
|
2019-10-23 14:58:31 +08:00
|
|
|
FastTMXTiledMap *ret = new (std::nothrow) FastTMXTiledMap();
|
2014-06-06 16:15:46 +08:00
|
|
|
if (ret->initWithTMXFile(tmxFile))
|
|
|
|
{
|
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(ret);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-10-23 14:58:31 +08:00
|
|
|
FastTMXTiledMap* FastTMXTiledMap::createWithXML(const std::string& tmxString, const std::string& resourcePath)
|
2014-06-06 16:15:46 +08:00
|
|
|
{
|
2019-10-23 14:58:31 +08:00
|
|
|
FastTMXTiledMap *ret = new (std::nothrow) FastTMXTiledMap();
|
2014-06-06 16:15:46 +08:00
|
|
|
if (ret->initWithXML(tmxString, resourcePath))
|
|
|
|
{
|
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(ret);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-10-23 14:58:31 +08:00
|
|
|
bool FastTMXTiledMap::initWithTMXFile(const std::string& tmxFile)
|
2014-06-06 16:15:46 +08:00
|
|
|
{
|
2014-06-19 15:57:13 +08:00
|
|
|
CCASSERT(tmxFile.size()>0, "FastTMXTiledMap: tmx file should not be empty");
|
2014-06-06 16:15:46 +08:00
|
|
|
|
2021-10-23 23:27:14 +08:00
|
|
|
setContentSize(Vec2::ZERO);
|
2014-06-06 16:15:46 +08:00
|
|
|
|
|
|
|
TMXMapInfo *mapInfo = TMXMapInfo::create(tmxFile);
|
|
|
|
|
|
|
|
if (! mapInfo)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2014-06-19 15:57:13 +08:00
|
|
|
CCASSERT( !mapInfo->getTilesets().empty(), "FastTMXTiledMap: Map not found. Please check the filename.");
|
2014-06-06 16:15:46 +08:00
|
|
|
buildWithMapInfo(mapInfo);
|
|
|
|
|
2020-08-28 17:26:30 +08:00
|
|
|
_tmxFile = tmxFile;
|
|
|
|
|
2014-06-06 16:15:46 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-10-23 14:58:31 +08:00
|
|
|
bool FastTMXTiledMap::initWithXML(const std::string& tmxString, const std::string& resourcePath)
|
2014-06-06 16:15:46 +08:00
|
|
|
{
|
2021-10-23 23:27:14 +08:00
|
|
|
setContentSize(Vec2::ZERO);
|
2014-06-06 16:15:46 +08:00
|
|
|
|
|
|
|
TMXMapInfo *mapInfo = TMXMapInfo::createWithXML(tmxString, resourcePath);
|
|
|
|
|
2014-06-19 15:57:13 +08:00
|
|
|
CCASSERT( !mapInfo->getTilesets().empty(), "FastTMXTiledMap: Map not found. Please check the filename.");
|
2014-06-06 16:15:46 +08:00
|
|
|
buildWithMapInfo(mapInfo);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-10-23 14:58:31 +08:00
|
|
|
FastTMXTiledMap::FastTMXTiledMap()
|
2021-10-23 23:27:14 +08:00
|
|
|
:_mapSize(Vec2::ZERO)
|
|
|
|
,_tileSize(Vec2::ZERO)
|
2014-06-06 16:15:46 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-10-23 14:58:31 +08:00
|
|
|
FastTMXTiledMap::~FastTMXTiledMap()
|
2014-06-06 16:15:46 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// private
|
2019-10-23 14:58:31 +08:00
|
|
|
FastTMXLayer * FastTMXTiledMap::parseLayer(TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo)
|
2014-06-06 16:15:46 +08:00
|
|
|
{
|
|
|
|
TMXTilesetInfo *tileset = tilesetForLayer(layerInfo, mapInfo);
|
2015-05-22 11:14:34 +08:00
|
|
|
if (tileset == nullptr)
|
|
|
|
return nullptr;
|
|
|
|
|
2019-10-23 14:58:31 +08:00
|
|
|
FastTMXLayer *layer = FastTMXLayer::create(tileset, layerInfo, mapInfo);
|
2014-06-06 16:15:46 +08:00
|
|
|
|
|
|
|
// tell the layerinfo to release the ownership of the tiles map.
|
|
|
|
layerInfo->_ownTiles = false;
|
|
|
|
layer->setupTiles();
|
|
|
|
|
|
|
|
return layer;
|
|
|
|
}
|
|
|
|
|
2019-10-23 14:58:31 +08:00
|
|
|
TMXTilesetInfo * FastTMXTiledMap::tilesetForLayer(TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo)
|
2014-06-06 16:15:46 +08:00
|
|
|
{
|
2021-10-23 23:27:14 +08:00
|
|
|
Vec2 size = layerInfo->_layerSize;
|
2014-06-06 16:15:46 +08:00
|
|
|
auto& tilesets = mapInfo->getTilesets();
|
2016-10-27 15:10:24 +08:00
|
|
|
|
|
|
|
for (auto iter = tilesets.crbegin(), iterCrend = tilesets.crend(); iter != iterCrend; ++iter)
|
2014-06-06 16:15:46 +08:00
|
|
|
{
|
2014-06-30 11:08:57 +08:00
|
|
|
TMXTilesetInfo* tilesetInfo = *iter;
|
|
|
|
if (tilesetInfo)
|
2014-06-06 16:15:46 +08:00
|
|
|
{
|
2014-06-27 18:28:59 +08:00
|
|
|
for( int y=0; y < size.height; y++ )
|
2014-06-06 16:15:46 +08:00
|
|
|
{
|
2014-06-27 18:28:59 +08:00
|
|
|
for( int x=0; x < size.width; x++ )
|
2014-06-06 16:15:46 +08:00
|
|
|
{
|
2016-11-09 10:34:50 +08:00
|
|
|
uint32_t pos = static_cast<uint32_t>(x + size.width * y);
|
|
|
|
uint32_t gid = layerInfo->_tiles[ pos ];
|
2014-06-27 18:28:59 +08:00
|
|
|
|
|
|
|
// gid are stored in little endian.
|
|
|
|
// if host is big endian, then swap
|
|
|
|
//if( o == CFByteOrderBigEndian )
|
|
|
|
// gid = CFSwapInt32( gid );
|
|
|
|
/* We support little endian.*/
|
|
|
|
|
2014-08-30 03:54:24 +08:00
|
|
|
// FIXME: gid == 0 --> empty tile
|
2014-06-27 18:28:59 +08:00
|
|
|
if( gid != 0 )
|
2014-06-06 16:15:46 +08:00
|
|
|
{
|
2014-06-27 18:28:59 +08:00
|
|
|
// Optimization: quick return
|
|
|
|
// if the layer is invalid (more than 1 tileset per layer) an CCAssert will be thrown later
|
2016-11-09 10:34:50 +08:00
|
|
|
if( (gid & kTMXFlippedMask)
|
|
|
|
>= static_cast<uint32_t>(tilesetInfo->_firstGid))
|
|
|
|
{
|
2014-06-30 11:08:57 +08:00
|
|
|
return tilesetInfo;
|
2016-11-09 10:34:50 +08:00
|
|
|
}
|
2014-06-06 16:15:46 +08:00
|
|
|
}
|
2014-06-27 18:28:59 +08:00
|
|
|
}
|
2014-06-06 16:15:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If all the tiles are 0, return empty tileset
|
|
|
|
CCLOG("cocos2d: Warning: TMX Layer '%s' has no tiles", layerInfo->_name.c_str());
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-10-23 14:58:31 +08:00
|
|
|
void FastTMXTiledMap::buildWithMapInfo(TMXMapInfo* mapInfo)
|
2014-06-06 16:15:46 +08:00
|
|
|
{
|
|
|
|
_mapSize = mapInfo->getMapSize();
|
|
|
|
_tileSize = mapInfo->getTileSize();
|
|
|
|
_mapOrientation = mapInfo->getOrientation();
|
|
|
|
|
|
|
|
_objectGroups = mapInfo->getObjectGroups();
|
|
|
|
|
|
|
|
_properties = mapInfo->getProperties();
|
|
|
|
|
|
|
|
_tileProperties = mapInfo->getTileProperties();
|
|
|
|
|
|
|
|
int idx=0;
|
|
|
|
|
|
|
|
auto& layers = mapInfo->getLayers();
|
|
|
|
for(const auto &layerInfo : layers) {
|
|
|
|
if (layerInfo->_visible)
|
|
|
|
{
|
2019-10-23 14:58:31 +08:00
|
|
|
FastTMXLayer *child = parseLayer(layerInfo, mapInfo);
|
2015-05-22 11:14:34 +08:00
|
|
|
if (child == nullptr) {
|
|
|
|
idx++;
|
|
|
|
continue;
|
|
|
|
}
|
2014-06-06 16:15:46 +08:00
|
|
|
addChild(child, idx, idx);
|
|
|
|
|
|
|
|
// update content size with the max size
|
2021-10-23 23:27:14 +08:00
|
|
|
const Vec2& childSize = child->getContentSize();
|
|
|
|
Vec2 currentSize = this->getContentSize();
|
2014-06-06 16:15:46 +08:00
|
|
|
currentSize.width = std::max( currentSize.width, childSize.width );
|
|
|
|
currentSize.height = std::max( currentSize.height, childSize.height );
|
|
|
|
this->setContentSize(currentSize);
|
|
|
|
|
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
}
|
2020-08-28 17:26:30 +08:00
|
|
|
|
|
|
|
_layerCount = idx;
|
2014-06-06 16:15:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// public
|
2019-10-23 14:58:31 +08:00
|
|
|
FastTMXLayer * FastTMXTiledMap::getLayer(const std::string& layerName) const
|
2014-06-06 16:15:46 +08:00
|
|
|
{
|
2020-08-18 14:29:09 +08:00
|
|
|
CCASSERT(!layerName.empty(), "Invalid layer name!");
|
2014-06-06 16:15:46 +08:00
|
|
|
|
|
|
|
for (auto& child : _children)
|
|
|
|
{
|
2019-10-23 14:58:31 +08:00
|
|
|
FastTMXLayer* layer = dynamic_cast<FastTMXLayer*>(child);
|
2014-06-06 16:15:46 +08:00
|
|
|
if(layer)
|
|
|
|
{
|
|
|
|
if(layerName.compare( layer->getLayerName()) == 0)
|
|
|
|
{
|
|
|
|
return layer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// layer not found
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-10-23 14:58:31 +08:00
|
|
|
TMXObjectGroup * FastTMXTiledMap::getObjectGroup(const std::string& groupName) const
|
2014-06-06 16:15:46 +08:00
|
|
|
{
|
2020-08-18 14:29:09 +08:00
|
|
|
CCASSERT(!groupName.empty(), "Invalid group name!");
|
2014-06-06 16:15:46 +08:00
|
|
|
|
|
|
|
if (_objectGroups.size()>0)
|
|
|
|
{
|
2016-10-27 15:10:24 +08:00
|
|
|
for (const auto& objectGroup : _objectGroups)
|
2014-06-06 16:15:46 +08:00
|
|
|
{
|
|
|
|
if (objectGroup && objectGroup->getGroupName() == groupName)
|
|
|
|
{
|
|
|
|
return objectGroup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// objectGroup not found
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-10-23 14:58:31 +08:00
|
|
|
Value FastTMXTiledMap::getProperty(const std::string& propertyName) const
|
2014-06-06 16:15:46 +08:00
|
|
|
{
|
2018-05-30 20:33:53 +08:00
|
|
|
auto propsItr = _properties.find(propertyName);
|
|
|
|
if (propsItr != _properties.end())
|
|
|
|
return propsItr->second;
|
2014-06-06 16:15:46 +08:00
|
|
|
|
|
|
|
return Value();
|
|
|
|
}
|
|
|
|
|
2019-10-23 14:58:31 +08:00
|
|
|
Value FastTMXTiledMap::getPropertiesForGID(int GID) const
|
2014-06-06 16:15:46 +08:00
|
|
|
{
|
2018-05-30 20:33:53 +08:00
|
|
|
auto propsItr = _tileProperties.find(GID);
|
|
|
|
if (propsItr != _tileProperties.end())
|
|
|
|
return propsItr->second;
|
2014-06-06 16:15:46 +08:00
|
|
|
|
|
|
|
return Value();
|
|
|
|
}
|
|
|
|
|
2019-10-23 14:58:31 +08:00
|
|
|
std::string FastTMXTiledMap::getDescription() const
|
2014-06-06 16:15:46 +08:00
|
|
|
{
|
2014-06-19 15:57:13 +08:00
|
|
|
return StringUtils::format("<FastTMXTiledMap | Tag = %d, Layers = %d", _tag, static_cast<int>(_children.size()));
|
2014-06-06 16:15:46 +08:00
|
|
|
}
|
|
|
|
|
2020-08-28 16:33:52 +08:00
|
|
|
void FastTMXTiledMap::setTileAnimEnabled(bool enabled)
|
|
|
|
{
|
|
|
|
for (auto& child : _children)
|
|
|
|
{
|
|
|
|
FastTMXLayer* layer = dynamic_cast<FastTMXLayer*>(child);
|
|
|
|
if (layer)
|
|
|
|
{
|
|
|
|
if (layer->hasTileAnimation())
|
|
|
|
{
|
|
|
|
if (enabled)
|
|
|
|
layer->getTileAnimManager()->startAll();
|
|
|
|
else
|
|
|
|
layer->getTileAnimManager()->stopAll();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-06 16:15:46 +08:00
|
|
|
NS_CC_END
|