2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
2012-09-24 21:22:20 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2012-04-19 14:35:52 +08:00
|
|
|
Copyright (c) 2009-2010 Ricardo Quesada
|
|
|
|
Copyright (c) 2011 Zynga Inc.
|
|
|
|
|
|
|
|
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__
|
2012-06-19 13:50:11 +08:00
|
|
|
|
|
|
|
#include "base_nodes/CCNode.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
#include "CCTMXObjectGroup.h"
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
2010-08-31 16:48:42 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
class TMXObjectGroup;
|
|
|
|
class TMXLayer;
|
|
|
|
class TMXLayerInfo;
|
|
|
|
class TMXTilesetInfo;
|
|
|
|
class TMXMapInfo;
|
2010-08-31 16:48:42 +08:00
|
|
|
|
2012-06-20 18:09:11 +08:00
|
|
|
/**
|
|
|
|
* @addtogroup tilemap_parallax_nodes
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2012-09-17 15:02:24 +08:00
|
|
|
/** Possible orientations of the TMX map */
|
2012-03-20 15:04:53 +08:00
|
|
|
enum
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
/** Orthogonal orientation */
|
2013-06-20 14:13:12 +08:00
|
|
|
TMXOrientationOrtho,
|
2010-08-31 16:48:42 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/** Hexagonal orientation */
|
2013-06-20 14:13:12 +08:00
|
|
|
TMXOrientationHex,
|
2010-08-31 16:48:42 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/** Isometric orientation */
|
2013-06-20 14:13:12 +08:00
|
|
|
TMXOrientationIso,
|
2012-03-20 15:04:53 +08:00
|
|
|
};
|
2010-08-31 16:48:42 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
/** @brief TMXTiledMap knows how to parse and render a TMX map.
|
2012-04-19 14:35:52 +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:
|
2013-06-20 14:13:12 +08:00
|
|
|
- Each tile will be treated as an Sprite
|
2012-04-19 14:35:52 +08:00
|
|
|
- The sprites are created on demand. They will be created only when you call "layer->tileAt(position)"
|
2013-06-20 14:13:12 +08:00
|
|
|
- Each tile can be rotated / moved / scaled / tinted / "opaqued", since each tile is a Sprite
|
2012-04-19 14:35:52 +08:00
|
|
|
- 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
|
2013-06-20 14:13:12 +08:00
|
|
|
- The tileset image will be loaded using the TextureCache
|
2012-04-19 14:35:52 +08:00
|
|
|
- Each tile will have a unique tag
|
|
|
|
- Each tile will have a unique z value. top-left: z=1, bottom-right: z=max z
|
2013-06-20 14:13:12 +08:00
|
|
|
- Each object group will be treated as an MutableArray
|
2012-04-19 14:35:52 +08:00
|
|
|
- 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.
|
2012-09-17 15:02:24 +08:00
|
|
|
- Embedded images are not supported
|
2012-04-19 14:35:52 +08:00
|
|
|
- It only supports the XML format (the JSON format is not supported)
|
|
|
|
|
|
|
|
Technical description:
|
2013-06-20 14:13:12 +08:00
|
|
|
Each layer is created using an TMXLayer (subclass of SpriteBatchNode). If you have 5 layers, then 5 TMXLayer will be created,
|
2012-04-19 14:35:52 +08:00
|
|
|
unless the layer visibility is off. In that case, the layer won't be created at all.
|
2013-06-20 14:13:12 +08:00
|
|
|
You can obtain the layers (TMXLayer objects) at runtime by:
|
2012-04-19 14:35:52 +08:00
|
|
|
- map->getChildByTag(tag_number); // 0=1st layer, 1=2nd layer, 2=3rd layer, etc...
|
|
|
|
- map->layerNamed(name_of_the_layer);
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Each object group is created using a TMXObjectGroup which is a subclass of MutableArray.
|
2012-04-19 14:35:52 +08:00
|
|
|
You can obtain the object groups at runtime by:
|
|
|
|
- map->objectGroupNamed(name_of_the_object_group);
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Each object is a TMXObject.
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Each property is stored as a key-value pair in an MutableDictionary.
|
2012-04-19 14:35:52 +08:00
|
|
|
You can obtain the properties at runtime by:
|
|
|
|
|
|
|
|
map->propertyNamed(name_of_the_property);
|
|
|
|
layer->propertyNamed(name_of_the_property);
|
|
|
|
objectGroup->propertyNamed(name_of_the_property);
|
|
|
|
object->propertyNamed(name_of_the_property);
|
|
|
|
|
|
|
|
@since v0.8.1
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
class CC_DLL TMXTiledMap : public Node
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
/** the map's size property measured in tiles */
|
2013-06-20 14:13:12 +08:00
|
|
|
CC_SYNTHESIZE_PASS_BY_REF(Size, _mapSize, MapSize);
|
2012-04-19 14:35:52 +08:00
|
|
|
/** the tiles's size property measured in pixels */
|
2013-06-20 14:13:12 +08:00
|
|
|
CC_SYNTHESIZE_PASS_BY_REF(Size, _tileSize, TileSize);
|
2012-04-19 14:35:52 +08:00
|
|
|
/** map orientation */
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SYNTHESIZE(int, _mapOrientation, MapOrientation);
|
2012-04-19 14:35:52 +08:00
|
|
|
/** object groups */
|
2013-06-20 14:13:12 +08:00
|
|
|
CC_PROPERTY(Array*, _objectGroups, ObjectGroups);
|
2012-04-19 14:35:52 +08:00
|
|
|
/** properties */
|
2013-06-20 14:13:12 +08:00
|
|
|
CC_PROPERTY(Dictionary*, _properties, Properties);
|
2012-03-20 15:04:53 +08:00
|
|
|
public:
|
2013-06-20 14:13:12 +08:00
|
|
|
TMXTiledMap();
|
|
|
|
virtual ~TMXTiledMap();
|
2010-08-31 16:48:42 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/** creates a TMX Tiled Map with a TMX file.*/
|
2013-06-20 14:13:12 +08:00
|
|
|
static TMXTiledMap* create(const char *tmxFile);
|
2012-03-16 17:56:19 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/** initializes a TMX Tiled Map with a TMX formatted XML string and a path to TMX resources */
|
2013-06-20 14:13:12 +08:00
|
|
|
static TMXTiledMap* createWithXML(const char* tmxString, const char* resourcePath);
|
2010-08-31 16:48:42 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/** initializes a TMX Tiled Map with a TMX file */
|
|
|
|
bool initWithTMXFile(const char *tmxFile);
|
2010-08-31 16:48:42 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/** initializes a TMX Tiled Map with a TMX formatted XML string and a path to TMX resources */
|
|
|
|
bool initWithXML(const char* tmxString, const char* resourcePath);
|
2012-03-16 17:56:19 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/** return the TMXLayer for the specific layer */
|
2013-06-20 14:13:12 +08:00
|
|
|
TMXLayer* layerNamed(const char *layerName);
|
2010-08-31 16:48:42 +08:00
|
|
|
|
2012-09-17 15:02:24 +08:00
|
|
|
/** return the TMXObjectGroup for the specific group */
|
2013-06-20 14:13:12 +08:00
|
|
|
TMXObjectGroup* objectGroupNamed(const char *groupName);
|
2010-08-31 16:48:42 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/** return the value for the specific property name */
|
2013-06-20 14:13:12 +08:00
|
|
|
String *propertyNamed(const char *propertyName);
|
2010-08-31 16:48:42 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/** return properties dictionary for tile GID */
|
2013-06-20 14:13:12 +08:00
|
|
|
Dictionary* propertiesForGID(int GID);
|
2010-08-31 16:48:42 +08:00
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
private:
|
2013-06-20 14:13:12 +08:00
|
|
|
TMXLayer * parseLayer(TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo);
|
|
|
|
TMXTilesetInfo * tilesetForLayer(TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo);
|
|
|
|
void buildWithMapInfo(TMXMapInfo* mapInfo);
|
2012-03-20 15:04:53 +08:00
|
|
|
protected:
|
2012-04-19 14:35:52 +08:00
|
|
|
//! tile properties
|
2013-06-20 14:13:12 +08:00
|
|
|
Dictionary* _tileProperties;
|
2010-08-31 16:48:42 +08:00
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
};
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-06-20 18:09:11 +08:00
|
|
|
// end of tilemap_parallax_nodes group
|
|
|
|
/// @}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
NS_CC_END
|
|
|
|
|
|
|
|
#endif //__CCTMX_TILE_MAP_H__
|
|
|
|
|
|
|
|
|