123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- /****************************************************************************
- Copyright (c) 2009-2010 Ricardo Quesada
- Copyright (c) 2010-2012 cocos2d-x.org
- Copyright (c) 2011 Zynga Inc.
- Copyright (c) 2013-2016 Chukong Technologies Inc.
- Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
- 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.
- ****************************************************************************/
- #include "2d/CCTMXTiledMap.h"
- #include "2d/CCTMXXMLParser.h"
- #include "2d/CCTMXLayer.h"
- #include "2d/CCSprite.h"
- #include "base/ccUTF8.h"
- NS_CC_BEGIN
- // implementation TMXTiledMap
- TMXTiledMap * TMXTiledMap::create(const std::string& tmxFile)
- {
- TMXTiledMap *ret = new (std::nothrow) TMXTiledMap();
- if (ret->initWithTMXFile(tmxFile))
- {
- ret->autorelease();
- return ret;
- }
- CC_SAFE_DELETE(ret);
- return nullptr;
- }
- TMXTiledMap* TMXTiledMap::createWithXML(const std::string& tmxString, const std::string& resourcePath)
- {
- TMXTiledMap *ret = new (std::nothrow) TMXTiledMap();
- if (ret->initWithXML(tmxString, resourcePath))
- {
- ret->autorelease();
- return ret;
- }
- CC_SAFE_DELETE(ret);
- return nullptr;
- }
- bool TMXTiledMap::initWithTMXFile(const std::string& tmxFile)
- {
- CCASSERT(tmxFile.size()>0, "TMXTiledMap: tmx file should not be empty");
- _tmxFile = tmxFile;
- setContentSize(Size::ZERO);
- TMXMapInfo *mapInfo = TMXMapInfo::create(tmxFile);
- if (! mapInfo)
- {
- return false;
- }
- CCASSERT( !mapInfo->getTilesets().empty(), "TMXTiledMap: Map not found. Please check the filename.");
- buildWithMapInfo(mapInfo);
- return true;
- }
- bool TMXTiledMap::initWithXML(const std::string& tmxString, const std::string& resourcePath)
- {
- _tmxFile = tmxString;
- setContentSize(Size::ZERO);
- TMXMapInfo *mapInfo = TMXMapInfo::createWithXML(tmxString, resourcePath);
- CCASSERT( !mapInfo->getTilesets().empty(), "TMXTiledMap: Map not found. Please check the filename.");
- buildWithMapInfo(mapInfo);
- return true;
- }
- TMXTiledMap::TMXTiledMap()
- :_mapSize(Size::ZERO)
- ,_tileSize(Size::ZERO)
- ,_tmxFile("")
- , _tmxLayerNum(0)
- {
- }
- TMXTiledMap::~TMXTiledMap()
- {
- }
- // private
- TMXLayer * TMXTiledMap::parseLayer(TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo)
- {
- TMXTilesetInfo *tileset = tilesetForLayer(layerInfo, mapInfo);
- if (tileset == nullptr)
- return nullptr;
-
- TMXLayer *layer = TMXLayer::create(tileset, layerInfo, mapInfo);
- if (nullptr != layer)
- {
- // tell the layerinfo to release the ownership of the tiles map.
- layerInfo->_ownTiles = false;
- layer->setupTiles();
- }
- return layer;
- }
- TMXTilesetInfo * TMXTiledMap::tilesetForLayer(TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo)
- {
- auto height = static_cast<uint32_t>(layerInfo->_layerSize.height);
- auto width = static_cast<uint32_t>(layerInfo->_layerSize.width);
- auto& tilesets = mapInfo->getTilesets();
- for (auto iter = tilesets.crbegin(), end = tilesets.crend(); iter != end; ++iter)
- {
- TMXTilesetInfo* tileset = *iter;
- if (tileset)
- {
- for (uint32_t y = 0; y < height; y++)
- {
- for (uint32_t x = 0; x < width; x++)
- {
- auto pos = x + width * y;
- auto gid = layerInfo->_tiles[ pos ];
- // FIXME:: gid == 0 --> empty tile
- if (gid != 0)
- {
- // Optimization: quick return
- // if the layer is invalid (more than 1 tileset per layer)
- // an CCAssert will be thrown later
- if (tileset->_firstGid < 0 ||
- (gid & kTMXFlippedMask) >= static_cast<uint32_t>(tileset->_firstGid))
- return tileset;
- }
- }
- }
- }
- }
- // If all the tiles are 0, return empty tileset
- CCLOG("cocos2d: Warning: TMX Layer '%s' has no tiles", layerInfo->_name.c_str());
- return nullptr;
- }
- void TMXTiledMap::buildWithMapInfo(TMXMapInfo* mapInfo)
- {
- _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) {
- TMXLayer *child = parseLayer(layerInfo, mapInfo);
- if (child == nullptr) {
- idx++;
- continue;
- }
- addChild(child, idx, idx);
- // update content size with the max size
- const Size& childSize = child->getContentSize();
- Size currentSize = this->getContentSize();
- currentSize.width = std::max(currentSize.width, childSize.width);
- currentSize.height = std::max(currentSize.height, childSize.height);
- this->setContentSize(currentSize);
- idx++;
- }
- }
- _tmxLayerNum = idx;
- }
- // public
- TMXLayer * TMXTiledMap::getLayer(const std::string& layerName) const
- {
- CCASSERT(layerName.size() > 0, "Invalid layer name!");
-
- for (auto& child : _children)
- {
- TMXLayer* layer = dynamic_cast<TMXLayer*>(child);
- if(layer)
- {
- if(layerName.compare( layer->getLayerName()) == 0)
- {
- return layer;
- }
- }
- }
- // layer not found
- return nullptr;
- }
- TMXObjectGroup * TMXTiledMap::getObjectGroup(const std::string& groupName) const
- {
- CCASSERT(groupName.size() > 0, "Invalid group name!");
- for (const auto objectGroup : _objectGroups)
- {
- if (objectGroup && objectGroup->getGroupName() == groupName)
- {
- return objectGroup;
- }
- }
- // objectGroup not found
- return nullptr;
- }
- Value TMXTiledMap::getProperty(const std::string& propertyName) const
- {
- if (_properties.find(propertyName) != _properties.end())
- return _properties.at(propertyName);
-
- return Value();
- }
- Value TMXTiledMap::getPropertiesForGID(int GID) const
- {
- if (_tileProperties.find(GID) != _tileProperties.end())
- return _tileProperties.at(GID);
-
- return Value();
- }
- bool TMXTiledMap::getPropertiesForGID(int GID, Value** value)
- {
- if (_tileProperties.find(GID) != _tileProperties.end()) {
- *value = &_tileProperties.at(GID);
- return true;
- } else {
- return false;
- }
- }
- std::string TMXTiledMap::getDescription() const
- {
- return StringUtils::format("<TMXTiledMap | Tag = %d, Layers = %d", _tag, static_cast<int>(_children.size()));
- }
- int TMXTiledMap::getLayerNum()
- {
- return _tmxLayerNum;
- }
- NS_CC_END
|