2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2009-2010 Ricardo Quesada
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2012-04-19 14:35:52 +08:00
|
|
|
Copyright (c) 2011 Zynga Inc.
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2012-04-19 14:35:52 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
#ifndef __CCTMX_TILE_MAP_H__
|
|
|
|
#define __CCTMX_TILE_MAP_H__
|
2012-06-19 13:50:11 +08:00
|
|
|
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "2d/CCNode.h"
|
2014-08-28 17:03:29 +08:00
|
|
|
#include "2d/CCTMXObjectGroup.h"
|
2014-04-27 01:35:57 +08:00
|
|
|
#include "base/CCValue.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
2010-08-31 16:48:42 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
class TMXLayer;
|
|
|
|
class TMXLayerInfo;
|
|
|
|
class TMXTilesetInfo;
|
|
|
|
class TMXMapInfo;
|
2010-08-31 16:48:42 +08:00
|
|
|
|
2012-06-20 18:09:11 +08:00
|
|
|
/**
|
2015-03-23 18:37:28 +08:00
|
|
|
* @addtogroup _2d
|
2012-06-20 18:09:11 +08:00
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2015-03-23 18:37:28 +08:00
|
|
|
/** Possible orientations of the TMX map. */
|
2012-03-20 15:04:53 +08:00
|
|
|
enum
|
|
|
|
{
|
2015-03-23 18:37:28 +08:00
|
|
|
/** Orthogonal orientation. */
|
2013-06-20 14:13:12 +08:00
|
|
|
TMXOrientationOrtho,
|
2010-08-31 16:48:42 +08:00
|
|
|
|
2015-03-23 18:37:28 +08:00
|
|
|
/** Hexagonal orientation. */
|
2013-06-20 14:13:12 +08:00
|
|
|
TMXOrientationHex,
|
2010-08-31 16:48:42 +08:00
|
|
|
|
2015-03-23 18:37:28 +08:00
|
|
|
/** Isometric orientation. */
|
2013-06-20 14:13:12 +08:00
|
|
|
TMXOrientationIso,
|
2014-10-10 17:52:13 +08:00
|
|
|
|
2015-03-23 18:37:28 +08:00
|
|
|
/** Isometric staggered orientation. */
|
2014-10-10 17:52:13 +08:00
|
|
|
TMXOrientationStaggered,
|
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
|
|
|
|
2015-03-23 18:37:28 +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 Sprite.
|
|
|
|
* - The sprites are created on demand. They will be created only when you call "layer->tileAt(position)".
|
|
|
|
* - Each tile can be rotated / moved / scaled / tinted / "opaqued", since each tile is a Sprite.
|
|
|
|
* - 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 TextureCache.
|
|
|
|
* - 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 MutableArray.
|
|
|
|
* - 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.
|
|
|
|
* - Embedded images are not supported.
|
|
|
|
* - It only supports the XML format (the JSON format is not supported).
|
|
|
|
|
|
|
|
* Technical description:
|
|
|
|
* Each layer is created using an TMXLayer (subclass of SpriteBatchNode). If you have 5 layers, then 5 TMXLayer will be created,
|
|
|
|
* unless the layer visibility is off. In that case, the layer won't be created at all.
|
|
|
|
* You can obtain the layers (TMXLayer objects) at runtime by:
|
|
|
|
* - map->getChildByTag(tag_number); // 0=1st layer, 1=2nd layer, 2=3rd layer, etc...
|
|
|
|
* - map->getLayer(name_of_the_layer);
|
|
|
|
|
|
|
|
* Each object group is created using a TMXObjectGroup which is a subclass of MutableArray.
|
|
|
|
* You can obtain the object groups at runtime by:
|
|
|
|
* - map->getObjectGroup(name_of_the_object_group);
|
|
|
|
|
|
|
|
* Each object is a TMXObject.
|
|
|
|
|
|
|
|
* Each property is stored as a key-value pair in an MutableDictionary.
|
|
|
|
* You can obtain the properties at runtime by:
|
|
|
|
|
|
|
|
* map->getProperty(name_of_the_property);
|
|
|
|
* layer->getProperty(name_of_the_property);
|
|
|
|
* objectGroup->getProperty(name_of_the_property);
|
|
|
|
* object->getProperty(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
|
|
|
{
|
|
|
|
public:
|
2015-03-23 18:37:28 +08:00
|
|
|
/** Creates a TMX Tiled Map with a TMX file.
|
|
|
|
*
|
|
|
|
* @param tmxFile A TMX file.
|
|
|
|
* @return An autorelease object.
|
|
|
|
*/
|
2013-11-07 09:05:13 +08:00
|
|
|
static TMXTiledMap* create(const std::string& tmxFile);
|
2012-03-16 17:56:19 +08:00
|
|
|
|
2015-03-23 18:37:28 +08:00
|
|
|
/** Initializes a TMX Tiled Map with a TMX formatted XML string and a path to TMX resources.
|
|
|
|
*
|
|
|
|
* @param tmxString A TMX formatted XML string.
|
|
|
|
* @param resourcePath The path to TMX resources.
|
|
|
|
* @return An autorelease object.
|
|
|
|
*/
|
2013-11-07 09:05:13 +08:00
|
|
|
static TMXTiledMap* createWithXML(const std::string& tmxString, const std::string& resourcePath);
|
2010-08-31 16:48:42 +08:00
|
|
|
|
2015-03-23 18:37:28 +08:00
|
|
|
/** Return the TMXLayer for the specific layer.
|
|
|
|
*
|
|
|
|
* @param layerName A specific layer.
|
|
|
|
* @return The TMXLayer for the specific layer.
|
|
|
|
*/
|
2013-11-07 09:05:13 +08:00
|
|
|
TMXLayer* getLayer(const std::string& layerName) const;
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-12-24 15:49:58 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE TMXLayer* layerNamed(const std::string& layerName) const { return getLayer(layerName); };
|
2010-08-31 16:48:42 +08:00
|
|
|
|
2015-03-23 18:37:28 +08:00
|
|
|
/** Return the TMXObjectGroup for the specific group.
|
|
|
|
*
|
|
|
|
* @param groupName The group Name.
|
|
|
|
* @return A Type of TMXObjectGroup.
|
|
|
|
*/
|
2013-11-07 09:05:13 +08:00
|
|
|
TMXObjectGroup* getObjectGroup(const std::string& groupName) const;
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-12-24 15:49:58 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE TMXObjectGroup* objectGroupNamed(const std::string& groupName) const { return getObjectGroup(groupName); };
|
2010-08-31 16:48:42 +08:00
|
|
|
|
2015-03-23 18:37:28 +08:00
|
|
|
/** Return the value for the specific property name.
|
|
|
|
*
|
|
|
|
* @param propertyName The specific property name.
|
|
|
|
* @return Return the value for the specific property name.
|
|
|
|
*/
|
2013-12-03 14:47:35 +08:00
|
|
|
Value getProperty(const std::string& propertyName) const;
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-12-03 14:47:35 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE Value propertyNamed(const char *propertyName) const { return getProperty(propertyName); };
|
2010-08-31 16:48:42 +08:00
|
|
|
|
2015-03-23 18:37:28 +08:00
|
|
|
/** Return properties dictionary for tile GID.
|
|
|
|
*
|
|
|
|
* @param GID The tile GID.
|
|
|
|
* @return Return properties dictionary for tile GID.
|
|
|
|
*/
|
2013-12-03 14:47:35 +08:00
|
|
|
Value getPropertiesForGID(int GID) const;
|
|
|
|
CC_DEPRECATED_ATTRIBUTE Value propertiesForGID(int GID) const { return getPropertiesForGID(GID); };
|
2010-08-31 16:48:42 +08:00
|
|
|
|
2014-02-01 06:57:26 +08:00
|
|
|
/** Assings properties to argument value, returns true if it did found properties
|
2015-03-23 18:37:28 +08:00
|
|
|
* for that GID and did assinged a value, else it returns false.
|
|
|
|
*
|
|
|
|
* @param GID The tile GID.
|
|
|
|
* @param value Argument value.
|
|
|
|
* @return Return true if it did found properties for that GID and did assinged a value, else it returns false.
|
2014-02-01 06:57:26 +08:00
|
|
|
*/
|
|
|
|
bool getPropertiesForGID(int GID, Value** value);
|
|
|
|
|
2015-03-23 18:37:28 +08:00
|
|
|
/** The map's size property measured in tiles.
|
|
|
|
*
|
|
|
|
* @return The map's size property measured in tiles.
|
|
|
|
*/
|
2013-07-23 18:26:26 +08:00
|
|
|
inline const Size& getMapSize() const { return _mapSize; };
|
2015-03-23 18:37:28 +08:00
|
|
|
|
|
|
|
/** Set the map's size property measured in tiles.
|
|
|
|
*
|
|
|
|
* @param mapSize The map's size property measured in tiles.
|
|
|
|
*/
|
2013-07-23 18:26:26 +08:00
|
|
|
inline void setMapSize(const Size& mapSize) { _mapSize = mapSize; };
|
|
|
|
|
2015-03-23 18:37:28 +08:00
|
|
|
/** The tiles's size property measured in pixels.
|
|
|
|
*
|
|
|
|
* @return The tiles's size property measured in pixels.
|
|
|
|
*/
|
2013-07-23 18:26:26 +08:00
|
|
|
inline const Size& getTileSize() const { return _tileSize; };
|
2015-03-23 18:37:28 +08:00
|
|
|
|
|
|
|
/** Set the tiles's size property measured in pixels.
|
|
|
|
*
|
2015-03-27 11:39:31 +08:00
|
|
|
* @param tileSize The tiles's size property measured in pixels.
|
2015-03-23 18:37:28 +08:00
|
|
|
*/
|
2013-07-23 18:26:26 +08:00
|
|
|
inline void setTileSize(const Size& tileSize) { _tileSize = tileSize; };
|
|
|
|
|
2015-03-23 18:37:28 +08:00
|
|
|
/** Map orientation.
|
|
|
|
*
|
|
|
|
* @return Map orientation.
|
|
|
|
*/
|
2013-07-23 18:26:26 +08:00
|
|
|
inline int getMapOrientation() const { return _mapOrientation; };
|
2015-03-23 18:37:28 +08:00
|
|
|
|
|
|
|
/** Set map orientation.
|
|
|
|
*
|
|
|
|
* @param mapOrientation The map orientation.
|
|
|
|
*/
|
2013-07-23 18:26:26 +08:00
|
|
|
inline void setMapOrientation(int mapOrientation) { _mapOrientation = mapOrientation; };
|
2013-07-23 20:36:41 +08:00
|
|
|
|
2015-03-23 18:37:28 +08:00
|
|
|
/** Get the Object groups.
|
|
|
|
*
|
|
|
|
* @return The object groups.
|
|
|
|
*/
|
2013-11-29 11:36:42 +08:00
|
|
|
inline const Vector<TMXObjectGroup*>& getObjectGroups() const { return _objectGroups; };
|
|
|
|
inline Vector<TMXObjectGroup*>& getObjectGroups() { return _objectGroups; };
|
2015-03-23 18:37:28 +08:00
|
|
|
|
|
|
|
/** Set the object groups.
|
|
|
|
*
|
|
|
|
* @param groups The object groups.
|
|
|
|
*/
|
2013-11-29 11:36:42 +08:00
|
|
|
inline void setObjectGroups(const Vector<TMXObjectGroup*>& groups) {
|
2013-07-23 20:36:41 +08:00
|
|
|
_objectGroups = groups;
|
|
|
|
};
|
2013-07-23 18:26:26 +08:00
|
|
|
|
2015-03-23 18:37:28 +08:00
|
|
|
/** Properties.
|
|
|
|
*
|
|
|
|
* @return Properties.
|
|
|
|
*/
|
2013-12-04 17:46:57 +08:00
|
|
|
inline ValueMap& getProperties() { return _properties; };
|
2015-03-23 18:37:28 +08:00
|
|
|
|
|
|
|
/** Set the properties.
|
|
|
|
*
|
|
|
|
* @param properties A Type of ValueMap to set the properties.
|
|
|
|
*/
|
2013-12-04 17:46:57 +08:00
|
|
|
inline void setProperties(const ValueMap& properties) {
|
2013-07-23 20:36:41 +08:00
|
|
|
_properties = properties;
|
|
|
|
};
|
2015-03-23 18:37:28 +08:00
|
|
|
|
|
|
|
/** Get the description. */
|
2013-12-13 06:30:22 +08:00
|
|
|
virtual std::string getDescription() const override;
|
2014-03-21 13:44:29 +08:00
|
|
|
|
|
|
|
CC_CONSTRUCTOR_ACCESS:
|
2013-11-15 08:33:43 +08:00
|
|
|
/**
|
|
|
|
* @js ctor
|
|
|
|
*/
|
|
|
|
TMXTiledMap();
|
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
|
|
|
virtual ~TMXTiledMap();
|
2014-03-21 13:44:29 +08:00
|
|
|
|
2013-11-15 08:33:43 +08:00
|
|
|
/** initializes a TMX Tiled Map with a TMX file */
|
|
|
|
bool initWithTMXFile(const std::string& tmxFile);
|
2014-03-21 13:44:29 +08:00
|
|
|
|
2013-11-15 08:33:43 +08:00
|
|
|
/** initializes a TMX Tiled Map with a TMX formatted XML string and a path to TMX resources */
|
|
|
|
bool initWithXML(const std::string& tmxString, const std::string& resourcePath);
|
2014-03-21 13:44:29 +08:00
|
|
|
|
|
|
|
protected:
|
2013-06-20 14:13:12 +08:00
|
|
|
TMXLayer * parseLayer(TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo);
|
|
|
|
TMXTilesetInfo * tilesetForLayer(TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo);
|
|
|
|
void buildWithMapInfo(TMXMapInfo* mapInfo);
|
2013-11-15 08:33:43 +08:00
|
|
|
|
2013-07-23 18:26:26 +08:00
|
|
|
/** the map's size property measured in tiles */
|
|
|
|
Size _mapSize;
|
|
|
|
/** the tiles's size property measured in pixels */
|
|
|
|
Size _tileSize;
|
|
|
|
/** map orientation */
|
|
|
|
int _mapOrientation;
|
|
|
|
/** object groups */
|
2013-11-29 11:36:42 +08:00
|
|
|
Vector<TMXObjectGroup*> _objectGroups;
|
2013-07-23 18:26:26 +08:00
|
|
|
/** properties */
|
2013-12-04 17:46:57 +08:00
|
|
|
ValueMap _properties;
|
2013-07-23 18:26:26 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
//! tile properties
|
2013-12-26 22:07:20 +08:00
|
|
|
ValueMapIntKey _tileProperties;
|
2010-08-31 16:48:42 +08:00
|
|
|
|
2013-11-15 08:33:43 +08:00
|
|
|
private:
|
|
|
|
CC_DISALLOW_COPY_AND_ASSIGN(TMXTiledMap);
|
|
|
|
|
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__
|
|
|
|
|
|
|
|
|