TMXMap.h
#ifndef __CCTMX_TILE_MAP_H__
#define __CCTMX_TILE_MAP_H__
#include "CCNode.h"
#include "CCTMXObjectGroup.h"
#include "CCValue.h"
NS_CC_BEGIN
// TMXMap 需要包含的类
class TMXObjectGroup;
class TMXLayer;
class TMXLayerInfo;
class TMXTilesetInfo;
class TMXMapInfo;
/** 地图方向 */
enum
{
/** 直角地图 */
TMXOrientationOrtho,
/** 六边形地图 */
TMXOrientationHex,
/** 45度斜角地图 */
TMXOrientationIso,
};
/**
特点:
-每个图块都会被当做sprite处理
-精灵只有在需要的时候才被创建,他们只有当你使用"layer->tileAt(position)"创建
-每个图块可以使用旋转,放大,缩小等等操作
-图块在程序运行的时候可以移除
-TMX地图的锚点时(0 ,0)
-地图图块的z轴值可以动态变更
-图块的锚点时(0 ,0)
-TMX图层被当做Layer添加
-TNX图层有自己的名称
-图块会使用TextureCache添加
-每个图块都有自己特有的标签
-每个图块都有z轴值,左上方z为0,右下方z轴值最大
-每个对象组都会被当做Array处理
-对象类使用字典包含所有的属性
-各个属性会被分配到地图,层,对象组和对象中
限制:
-每个地图层最多支持一套瓦片素材组
-被嵌套的瓦片地图不被支持(如 使用瓦片素材组与其他图片)
-只支持XMl数据格式
技术性描述
TMX地图使用TMXLayer加载图层,如果图层设置为不可见,程序不会加载图层
可以通过
- map->getChildByTag(tag_number); // 0=1st layer, 1=2nd layer, 2=3rd layer, etc...
- map->getLayer(name_of_the_layer);
获取图层
每个对象组都会使用TMXObjectGroup加载对象,可以通过getObjectGroup()函数获取对象组
每个对象都是TMXObject。
每个属性都会存储在字典数组中
map->getProperty(name_of_the_property);
layer->getProperty(name_of_the_property);
objectGroup->getProperty(name_of_the_property);
object->getProperty(name_of_the_property);
*/
class CC_DLL TMXTiledMap : public Node
{
public:
/** 静态函数 根据路径创建TMXMap*/
static TMXTiledMap* create(const std::string& tmxFile);
/** 使用XML数据导入地图 */
static TMXTiledMap* createWithXML(const std::string& tmxString, const std::string& resourcePath);
/** 获取图层 */
TMXLayer* getLayer(const std::string& layerName) const;
/**
* @js NA
* @lua NA
*/
CC_DEPRECATED_ATTRIBUTE TMXLayer* layerNamed(const std::string& layerName) const { return getLayer(layerName); };
/** 返回对象组 */
TMXObjectGroup* getObjectGroup(const std::string& groupName) const;
/**
* @js NA
* @lua NA
*/
CC_DEPRECATED_ATTRIBUTE TMXObjectGroup* objectGroupNamed(const std::string& groupName) const { return getObjectGroup(groupName); };
/** 通过属性名获取属性 */
Value getProperty(const std::string& propertyName) const;
/**
* @js NA
* @lua NA
*/
CC_DEPRECATED_ATTRIBUTE Value propertyNamed(const char *propertyName) const { return getProperty(propertyName); };
/**返回属性字典,每个图块都有一个id */
Value getPropertiesForGID(int GID) const;
CC_DEPRECATED_ATTRIBUTE Value propertiesForGID(int GID) const { return getPropertiesForGID(GID); };
/** 根据GID,获得属性字典
*/
bool getPropertiesForGID(int GID, Value** value);
/** 地图的大小获取与设置 */
inline const Size& getMapSize() const { return _mapSize; };
inline void setMapSize(const Size& mapSize) { _mapSize = mapSize; };
/** 返回图块大小,以像素点为单位 */
inline const Size& getTileSize() const { return _tileSize; };
inline void setTileSize(const Size& tileSize) { _tileSize = tileSize; };
/** 地图的朝向 */
inline int getMapOrientation() const { return _mapOrientation; };
inline void setMapOrientation(int mapOrientation) { _mapOrientation = mapOrientation; };
/** 获取对象数组 */
inline const Vector<TMXObjectGroup*>& getObjectGroups() const { return _objectGroups; };
inline Vector<TMXObjectGroup*>& getObjectGroups() { return _objectGroups; };
inline void setObjectGroups(const Vector<TMXObjectGroup*>& groups) {
_objectGroups = groups;
};
/** 获取与设置属性 */
inline ValueMap& getProperties() { return _properties; };
inline void setProperties(const ValueMap& properties) {
_properties = properties;
};
virtual std::string getDescription() const override;
CC_CONSTRUCTOR_ACCESS:
/**
* @js ctor
*/
TMXTiledMap();
/**
* @js NA
* @lua NA
*/
virtual ~TMXTiledMap();
/** 根据路径获取地图数据 */
bool initWithTMXFile(const std::string& tmxFile);
/** XML字符串与资源路径创建Map */
bool initWithXML(const std::string& tmxString, const std::string& resourcePath);
protected: