2010-08-31 16:48:42 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010 cocos2d-x.org
|
|
|
|
|
|
|
|
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 "CCTMXTiledMap.h"
|
|
|
|
#include "CCTMXXMLParser.h"
|
|
|
|
#include "CCTMXLayer.h"
|
|
|
|
#include "CCSprite.h"
|
|
|
|
#include "CGPointExtension.h"
|
|
|
|
|
|
|
|
namespace cocos2d{
|
|
|
|
|
|
|
|
// implementation CCTMXTiledMap
|
|
|
|
CCTMXTiledMap * CCTMXTiledMap::tiledMapWithTMXFile(const char *tmxFile)
|
|
|
|
{
|
|
|
|
CCTMXTiledMap *pRet = new CCTMXTiledMap();
|
|
|
|
if (pRet->initWithTMXFile(tmxFile))
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
2010-09-16 11:18:09 +08:00
|
|
|
CCX_SAFE_DELETE(pRet);
|
2010-08-31 16:48:42 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
bool CCTMXTiledMap::initWithTMXFile(const char *tmxFile)
|
|
|
|
{
|
|
|
|
NSAssert(tmxFile != NULL && strlen(tmxFile)>0, "TMXTiledMap: tmx file should not bi nil");
|
|
|
|
|
|
|
|
setContentSize(CGSizeZero);
|
|
|
|
|
|
|
|
CCTMXMapInfo *mapInfo = CCTMXMapInfo::formatWithTMXFile(tmxFile);
|
|
|
|
|
|
|
|
NSAssert( mapInfo->getTilesets()->count() != 0, "TMXTiledMap: Map not found. Please check the filename.");
|
|
|
|
|
|
|
|
m_tMapSize = mapInfo->getMapSize();
|
|
|
|
m_tTileSize = mapInfo->getTileSize();
|
|
|
|
m_nMapOrientation = mapInfo->getOrientation();
|
2010-09-07 11:26:28 +08:00
|
|
|
setObjectGroups(mapInfo->getObjectGroups());
|
|
|
|
setProperties(mapInfo->getProperties());
|
|
|
|
CCX_SAFE_RELEASE(m_pTileProperties);
|
2010-08-31 16:48:42 +08:00
|
|
|
m_pTileProperties = mapInfo->getTileProperties();
|
2010-09-07 11:26:28 +08:00
|
|
|
CCX_SAFE_RETAIN(m_pTileProperties);
|
2010-08-31 16:48:42 +08:00
|
|
|
|
|
|
|
int idx = 0;
|
|
|
|
|
2010-09-02 15:31:37 +08:00
|
|
|
NSMutableArray<CCTMXLayerInfo*>* layers = mapInfo->getLayers();
|
|
|
|
if (layers && layers->count()>0)
|
2010-08-31 16:48:42 +08:00
|
|
|
{
|
|
|
|
CCTMXLayerInfo *layerInfo = NULL;
|
2010-09-01 16:23:30 +08:00
|
|
|
NSMutableArray<CCTMXLayerInfo*>::NSMutableArrayIterator it;
|
|
|
|
for (it = layers->begin(); it != layers->end(); ++it)
|
2010-08-31 16:48:42 +08:00
|
|
|
{
|
2010-09-01 16:23:30 +08:00
|
|
|
layerInfo = *it;
|
|
|
|
if (layerInfo && layerInfo->m_bVisible)
|
2010-08-31 16:48:42 +08:00
|
|
|
{
|
|
|
|
CCTMXLayer *child = parseLayer(layerInfo, mapInfo);
|
|
|
|
addChild((CCNode*)child, idx, idx);
|
|
|
|
|
|
|
|
// update content size with the max size
|
|
|
|
CGSize childSize = child->getContentSize();
|
|
|
|
CGSize currentSize = this->getContentSize();
|
|
|
|
currentSize.width = MAX( currentSize.width, childSize.width );
|
|
|
|
currentSize.height = MAX( currentSize.height, childSize.height );
|
|
|
|
this->setContentSize(currentSize);
|
|
|
|
|
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
CCTMXTiledMap::CCTMXTiledMap()
|
2010-08-31 18:11:31 +08:00
|
|
|
:m_tTileSize(CGSizeZero)
|
|
|
|
,m_tMapSize(CGSizeZero)
|
|
|
|
,m_pObjectGroups(NULL)
|
|
|
|
,m_pProperties(NULL)
|
|
|
|
,m_pTileProperties(NULL)
|
2010-08-31 16:48:42 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
CCTMXTiledMap::~CCTMXTiledMap()
|
|
|
|
{
|
2010-09-06 20:26:19 +08:00
|
|
|
CCX_SAFE_RELEASE(m_pProperties);
|
|
|
|
CCX_SAFE_RELEASE(m_pObjectGroups);
|
|
|
|
CCX_SAFE_RELEASE(m_pTileProperties);
|
2010-08-31 16:48:42 +08:00
|
|
|
}
|
2010-09-04 18:06:26 +08:00
|
|
|
NSMutableArray<CCTMXObjectGroup*> * CCTMXTiledMap::getObjectGroups()
|
|
|
|
{
|
|
|
|
return m_pObjectGroups;
|
|
|
|
}
|
|
|
|
void CCTMXTiledMap::setObjectGroups(NSMutableArray<CCTMXObjectGroup*>* var)
|
|
|
|
{
|
2010-09-27 18:19:10 +08:00
|
|
|
CCX_SAFE_RETAIN(var);
|
2010-09-06 11:31:23 +08:00
|
|
|
CCX_SAFE_RELEASE(m_pObjectGroups);
|
2010-09-04 18:06:26 +08:00
|
|
|
m_pObjectGroups = var;
|
|
|
|
}
|
2010-09-07 11:26:28 +08:00
|
|
|
CCXStringToStringDictionary * CCTMXTiledMap::getProperties()
|
2010-09-06 20:26:19 +08:00
|
|
|
{
|
|
|
|
return m_pProperties;
|
|
|
|
}
|
2010-09-07 11:26:28 +08:00
|
|
|
void CCTMXTiledMap::setProperties(CCXStringToStringDictionary* var)
|
2010-09-06 20:26:19 +08:00
|
|
|
{
|
2010-09-27 18:19:10 +08:00
|
|
|
CCX_SAFE_RETAIN(var);
|
2010-09-06 20:26:19 +08:00
|
|
|
CCX_SAFE_RELEASE(m_pProperties);
|
2010-09-07 11:26:28 +08:00
|
|
|
m_pProperties = var;
|
2010-09-06 20:26:19 +08:00
|
|
|
}
|
2010-08-31 16:48:42 +08:00
|
|
|
// private
|
|
|
|
CCTMXLayer * CCTMXTiledMap::parseLayer(CCTMXLayerInfo *layerInfo, CCTMXMapInfo *mapInfo)
|
|
|
|
{
|
|
|
|
CCTMXTilesetInfo *tileset = tilesetForLayer(layerInfo, mapInfo);
|
|
|
|
CCTMXLayer *layer = CCTMXLayer::layerWithTilesetInfo(tileset, layerInfo, mapInfo);
|
|
|
|
|
|
|
|
// tell the layerinfo to release the ownership of the tiles map.
|
|
|
|
layerInfo->m_bOwnTiles = false;
|
|
|
|
layer->setupTiles();
|
|
|
|
|
|
|
|
return layer;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCTMXTilesetInfo * CCTMXTiledMap::tilesetForLayer(CCTMXLayerInfo *layerInfo, CCTMXMapInfo *mapInfo)
|
|
|
|
{
|
|
|
|
CCTMXTilesetInfo *tileset = NULL;
|
|
|
|
//CFByteOrder o = CFByteOrderGetCurrent();
|
|
|
|
|
|
|
|
CGSize size = layerInfo->m_tLayerSize;
|
2010-09-02 15:31:37 +08:00
|
|
|
NSMutableArray<CCTMXTilesetInfo*>* tilesets = mapInfo->getTilesets();
|
|
|
|
if (tilesets && tilesets->count()>0)
|
2010-08-31 16:48:42 +08:00
|
|
|
{
|
|
|
|
CCTMXTilesetInfo *tileset = NULL;
|
2010-09-01 16:23:30 +08:00
|
|
|
NSMutableArray<CCTMXTilesetInfo*>::NSMutableArrayRevIterator rit;
|
2010-09-02 15:31:37 +08:00
|
|
|
for (rit = tilesets->rbegin(); rit != tilesets->rend(); ++rit)
|
2010-08-31 16:48:42 +08:00
|
|
|
{
|
2010-09-01 16:23:30 +08:00
|
|
|
tileset = *rit;
|
2010-08-31 16:48:42 +08:00
|
|
|
if (tileset)
|
|
|
|
{
|
|
|
|
for( unsigned int y=0; y < size.height; y++ )
|
|
|
|
{
|
|
|
|
for( unsigned int x=0; x < size.width; x++ )
|
|
|
|
{
|
|
|
|
unsigned int pos = (unsigned int)(x + size.width * y);
|
|
|
|
unsigned int gid = layerInfo->m_pTiles[ pos ];
|
|
|
|
|
|
|
|
// gid are stored in little endian.
|
|
|
|
// if host is big endian, then swap
|
|
|
|
//if( o == CFByteOrderBigEndian )
|
|
|
|
// gid = CFSwapInt32( gid );
|
|
|
|
/* We support little endian.*/
|
|
|
|
|
|
|
|
// XXX: gid == 0 --> empty tile
|
|
|
|
if( gid != 0 )
|
|
|
|
{
|
|
|
|
// Optimization: quick return
|
|
|
|
// if the layer is invalid (more than 1 tileset per layer) an assert will be thrown later
|
|
|
|
if( gid >= tileset->m_uFirstGid )
|
|
|
|
return tileset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If all the tiles are 0, return empty tileset
|
|
|
|
CCLOG("cocos2d: Warning: TMX Layer '%@' has no tiles", layerInfo.name);
|
|
|
|
return tileset;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// public
|
|
|
|
CCTMXLayer * CCTMXTiledMap::layerNamed(const char *layerName)
|
|
|
|
{
|
|
|
|
std::string sLayerName = layerName;
|
|
|
|
if (m_pChildren && m_pChildren->count()>0)
|
|
|
|
{
|
|
|
|
CCTMXLayer *layer;
|
|
|
|
NSMutableArray<CCNode*>::NSMutableArrayIterator it;
|
|
|
|
for (it = m_pChildren->begin(); it != m_pChildren->end(); ++it)
|
|
|
|
{
|
|
|
|
layer = (CCTMXLayer*)(*it);
|
|
|
|
if (layer && layer->getLayerName() == sLayerName)
|
|
|
|
{
|
|
|
|
return layer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// layer not found
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
CCTMXObjectGroup * CCTMXTiledMap::objectGroupNamed(const char *groupName)
|
|
|
|
{
|
|
|
|
std::string sGroupName = groupName;
|
2010-09-02 15:31:37 +08:00
|
|
|
if (m_pObjectGroups && m_pObjectGroups->count()>0)
|
2010-08-31 16:48:42 +08:00
|
|
|
{
|
|
|
|
CCTMXObjectGroup *objectGroup;
|
2010-09-02 15:31:37 +08:00
|
|
|
NSMutableArray<CCTMXObjectGroup*>::NSMutableArrayIterator it;
|
|
|
|
for (it = m_pObjectGroups->begin(); it != m_pObjectGroups->end(); ++it)
|
2010-08-31 16:48:42 +08:00
|
|
|
{
|
|
|
|
objectGroup = (CCTMXObjectGroup*)(*it);
|
|
|
|
if (objectGroup && objectGroup->getGroupName() == sGroupName)
|
|
|
|
{
|
|
|
|
return objectGroup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// objectGroup not found
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX deprecated
|
|
|
|
CCTMXObjectGroup * CCTMXTiledMap::groupNamed(const char *groupName)
|
|
|
|
{
|
|
|
|
return objectGroupNamed(groupName);
|
|
|
|
}
|
2010-09-06 20:26:19 +08:00
|
|
|
NSString * CCTMXTiledMap::propertyNamed(const char *propertyName)
|
2010-08-31 16:48:42 +08:00
|
|
|
{
|
2010-09-10 14:08:25 +08:00
|
|
|
return m_pProperties->objectForKey(std::string(propertyName));
|
2010-08-31 16:48:42 +08:00
|
|
|
}
|
2010-09-06 20:26:19 +08:00
|
|
|
NSDictionary<std::string, NSString*> * CCTMXTiledMap::propertiesForGID(int GID)
|
2010-08-31 16:48:42 +08:00
|
|
|
{
|
2010-09-06 20:26:19 +08:00
|
|
|
return m_pTileProperties->objectForKey(GID);
|
2010-08-31 16:48:42 +08:00
|
|
|
}
|
2010-09-06 20:26:19 +08:00
|
|
|
|
2010-08-31 16:48:42 +08:00
|
|
|
|
|
|
|
}// namespace cocos2d
|
|
|
|
|