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.
|
|
|
|
****************************************************************************/
|
|
|
|
#ifndef __CCTMX_TILE_MAP_H__
|
|
|
|
#define __CCTMX_TILE_MAP_H__
|
|
|
|
#include "CCNode.h"
|
|
|
|
#include "CCTMXObjectGroup.h"
|
|
|
|
|
|
|
|
namespace cocos2d {
|
|
|
|
|
|
|
|
class CCTMXObjectGroup;
|
|
|
|
class CCTMXLayer;
|
|
|
|
class CCTMXLayerInfo;
|
|
|
|
class CCTMXTilesetInfo;
|
|
|
|
class CCTMXMapInfo;
|
|
|
|
|
|
|
|
/** Possible oritentations of the TMX map */
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
/** Orthogonal orientation */
|
|
|
|
CCTMXOrientationOrtho,
|
|
|
|
|
|
|
|
/** Hexagonal orientation */
|
|
|
|
CCTMXOrientationHex,
|
|
|
|
|
|
|
|
/** Isometric orientation */
|
|
|
|
CCTMXOrientationIso,
|
|
|
|
};
|
|
|
|
|
2010-09-29 09:44:57 +08:00
|
|
|
/** @brief CCTMXTiledMap knows how to parse and render a TMX map.
|
2010-09-25 10:28:27 +08:00
|
|
|
|
|
|
|
It adds support for the TMX tiled map format used by http://www.mapeditor.org
|
|
|
|
It supports isometric, hexagonal and orthogonal tiles.
|
|
|
|
It also supports object groups, objects, and properties.
|
|
|
|
|
|
|
|
Features:
|
|
|
|
- Each tile will be treated as an CCSprite
|
2010-09-28 12:14:12 +08:00
|
|
|
- The sprites are created on demand. They will be created only when you call "layer->tileAt(position)"
|
2010-09-25 10:28:27 +08:00
|
|
|
- Each tile can be rotated / moved / scaled / tinted / "opacitied", since each tile is a CCSprite
|
|
|
|
- Tiles can be added/removed in runtime
|
|
|
|
- The z-order of the tiles can be modified in runtime
|
|
|
|
- Each tile has an anchorPoint of (0,0)
|
|
|
|
- The anchorPoint of the TMXTileMap is (0,0)
|
|
|
|
- The TMX layers will be added as a child
|
|
|
|
- The TMX layers will be aliased by default
|
|
|
|
- The tileset image will be loaded using the CCTextureCache
|
|
|
|
- Each tile will have a unique tag
|
|
|
|
- Each tile will have a unique z value. top-left: z=1, bottom-right: z=max z
|
|
|
|
- Each object group will be treated as an NSMutableArray
|
|
|
|
- Object class which will contain all the properties in a dictionary
|
|
|
|
- Properties can be assigned to the Map, Layer, Object Group, and Object
|
|
|
|
|
|
|
|
Limitations:
|
|
|
|
- It only supports one tileset per layer.
|
|
|
|
- Embeded images are not supported
|
|
|
|
- It only supports the XML format (the JSON format is not supported)
|
|
|
|
|
|
|
|
Technical description:
|
2010-12-31 09:36:53 +08:00
|
|
|
Each layer is created using an CCTMXLayer (subclass of CCSpriteBatchNode). If you have 5 layers, then 5 CCTMXLayer will be created,
|
2010-09-25 10:28:27 +08:00
|
|
|
unless the layer visibility is off. In that case, the layer won't be created at all.
|
|
|
|
You can obtain the layers (CCTMXLayer objects) at runtime by:
|
2010-09-28 12:14:12 +08:00
|
|
|
- map->getChildByTag(tag_number); // 0=1st layer, 1=2nd layer, 2=3rd layer, etc...
|
|
|
|
- map->layerNamed(name_of_the_layer);
|
2010-09-25 10:28:27 +08:00
|
|
|
|
|
|
|
Each object group is created using a CCTMXObjectGroup which is a subclass of NSMutableArray.
|
|
|
|
You can obtain the object groups at runtime by:
|
2010-09-28 12:14:12 +08:00
|
|
|
- map->objectGroupNamed(name_of_the_object_group);
|
2010-09-25 10:28:27 +08:00
|
|
|
|
|
|
|
Each object is a CCTMXObject.
|
|
|
|
|
|
|
|
Each property is stored as a key-value pair in an NSMutableDictionary.
|
|
|
|
You can obtain the properties at runtime by:
|
|
|
|
|
2010-09-28 12:14:12 +08:00
|
|
|
map->propertyNamed(name_of_the_property);
|
|
|
|
layer->propertyNamed(name_of_the_property);
|
|
|
|
objectGroup->propertyNamed(name_of_the_property);
|
|
|
|
object->propertyNamed(name_of_the_property);
|
2010-09-25 10:28:27 +08:00
|
|
|
|
|
|
|
@since v0.8.1
|
|
|
|
*/
|
2010-08-31 16:48:42 +08:00
|
|
|
class CCX_DLL CCTMXTiledMap : public CCNode
|
|
|
|
{
|
|
|
|
/** the map's size property measured in tiles */
|
|
|
|
CCX_SYNTHESIZE(CGSize, m_tMapSize, MapSize);
|
|
|
|
/** the tiles's size property measured in pixels */
|
|
|
|
CCX_SYNTHESIZE(CGSize, m_tTileSize, TileSize);
|
|
|
|
/** map orientation */
|
|
|
|
CCX_SYNTHESIZE(int, m_nMapOrientation, MapOrientation);
|
|
|
|
/** object groups */
|
2010-09-04 18:06:26 +08:00
|
|
|
CCX_PROPERTY(NSMutableArray<CCTMXObjectGroup*>*, m_pObjectGroups, ObjectGroups);
|
2010-08-31 16:48:42 +08:00
|
|
|
/** properties */
|
2010-09-07 11:26:28 +08:00
|
|
|
CCX_PROPERTY(CCXStringToStringDictionary*, m_pProperties, Properties);
|
2010-08-31 16:48:42 +08:00
|
|
|
public:
|
|
|
|
CCTMXTiledMap();
|
|
|
|
virtual ~CCTMXTiledMap();
|
|
|
|
|
|
|
|
/** creates a TMX Tiled Map with a TMX file.*/
|
|
|
|
static CCTMXTiledMap * tiledMapWithTMXFile(const char *tmxFile);
|
|
|
|
|
|
|
|
/** initializes a TMX Tiled Map with a TMX file */
|
|
|
|
bool initWithTMXFile(const char *tmxFile);
|
|
|
|
|
|
|
|
/** return the TMXLayer for the specific layer */
|
|
|
|
CCTMXLayer* layerNamed(const char *layerName);
|
|
|
|
|
|
|
|
/** return the TMXObjectGroup for the secific group */
|
|
|
|
CCTMXObjectGroup* objectGroupNamed(const char *groupName);
|
|
|
|
|
|
|
|
/** return the TMXObjectGroup for the secific group
|
|
|
|
@deprecated Use map#objectGroupNamed instead
|
|
|
|
*/
|
|
|
|
CCTMXObjectGroup* groupNamed(const char *groupName);
|
|
|
|
|
|
|
|
/** return the value for the specific property name */
|
2010-09-06 20:26:19 +08:00
|
|
|
NSString *propertyNamed(const char *propertyName);
|
2010-08-31 16:48:42 +08:00
|
|
|
|
|
|
|
/** return properties dictionary for tile GID */
|
2010-09-06 20:26:19 +08:00
|
|
|
NSDictionary<std::string, NSString*> *propertiesForGID(int GID);
|
2010-08-31 16:48:42 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
CCTMXLayer * parseLayer(CCTMXLayerInfo *layerInfo, CCTMXMapInfo *mapInfo);
|
|
|
|
CCTMXTilesetInfo * tilesetForLayer(CCTMXLayerInfo *layerInfo, CCTMXMapInfo *mapInfo);
|
|
|
|
|
|
|
|
protected:
|
2010-09-29 12:03:21 +08:00
|
|
|
//! tile properties
|
2010-09-07 11:26:28 +08:00
|
|
|
NSDictionary<int, CCXStringToStringDictionary*> *m_pTileProperties;
|
2010-08-31 16:48:42 +08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}// namespace cocos2d
|
|
|
|
#endif //__CCTMX_TILE_MAP_H__
|
|
|
|
|
|
|
|
|