axmol/core/2d/CCFastTMXTiledMap.h

262 lines
8.6 KiB
C
Raw Normal View History

2014-06-06 16:15:46 +08:00
/****************************************************************************
Copyright (c) 2009-2010 Ricardo Quesada
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2011 Zynga Inc.
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2014-06-06 16:15:46 +08:00
https://axis-project.github.io/
2014-06-06 16:15:46 +08:00
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.
****************************************************************************/
2019-10-23 14:58:31 +08:00
#pragma once
2014-06-06 16:15:46 +08:00
#include "2d/CCNode.h"
#include "2d/CCTMXObjectGroup.h"
2014-06-06 16:15:46 +08:00
NS_AX_BEGIN
2014-06-06 16:15:46 +08:00
class TMXLayerInfo;
class TMXTilesetInfo;
class TMXMapInfo;
2019-10-23 14:58:31 +08:00
class FastTMXLayer;
2015-03-21 17:06:26 +08:00
/**
* @addtogroup _2d
* @{
*/
2021-12-25 10:04:45 +08:00
/** @brief FastTMXTiledMap knows how to parse and render a TMX map.
2014-06-06 16:15:46 +08:00
2015-03-21 17:06:26 +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:
2021-12-25 10:04:45 +08:00
* Each layer is created using an FastTMXLayer (subclass of SpriteBatchNode). If you have 5 layers, then 5 FastTMXLayer
will be created,
2015-03-21 17:06:26 +08:00
* unless the layer visibility is off. In that case, the layer won't be created at all.
* You can obtain the layers (FastTMXLayer 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 v3.2
* @js NA
2015-03-21 17:06:26 +08:00
*/
2022-07-15 19:17:01 +08:00
class AX_DLL FastTMXTiledMap : public Node
2014-06-06 16:15:46 +08:00
{
public:
2015-03-21 17:06:26 +08:00
/** Creates a TMX Tiled Map with a TMX file.
*
* @return An autorelease object.
*/
2021-12-26 23:26:34 +08:00
static FastTMXTiledMap* create(std::string_view tmxFile);
2014-06-06 16:15:46 +08:00
2021-12-25 10:04:45 +08:00
/** Initializes a TMX Tiled Map with a TMX formatted XML string and a path to TMX resources.
2015-03-21 17:06:26 +08:00
*
* @param tmxString A TMX formatted XML string.
* @param resourcePath A path to TMX resources.
* @return An autorelease object.
*/
2021-12-26 23:26:34 +08:00
static FastTMXTiledMap* createWithXML(std::string_view tmxString, std::string_view resourcePath);
2014-06-06 16:15:46 +08:00
2021-12-25 10:04:45 +08:00
/** Return the FastTMXLayer for the specific layer.
*
2015-03-21 17:06:26 +08:00
* @return Return the FastTMXLayer for the specific layer.
*/
2021-12-26 23:26:34 +08:00
FastTMXLayer* getLayer(std::string_view layerName) const;
2014-06-06 16:15:46 +08:00
2021-12-25 10:04:45 +08:00
/** Return the TMXObjectGroup for the specific group.
*
2015-03-21 17:06:26 +08:00
* @return Return the TMXObjectGroup for the specific group.
*/
2021-12-26 23:26:34 +08:00
TMXObjectGroup* getObjectGroup(std::string_view groupName) const;
2014-06-06 16:15:46 +08:00
2015-03-21 17:06:26 +08:00
/** Return the value for the specific property name.
*
* @return Return the value for the specific property name.
*/
2021-12-26 23:26:34 +08:00
Value getProperty(std::string_view propertyName) const;
2014-06-06 16:15:46 +08:00
2015-03-21 17:06:26 +08:00
/** Return properties dictionary for tile GID.
*
* @return Return properties dictionary for tile GID.
*/
2014-06-06 16:15:46 +08:00
Value getPropertiesForGID(int GID) const;
2021-12-25 10:04:45 +08:00
/** The map's size property measured in tiles.
2015-03-21 17:06:26 +08:00
*
* @return The map's size property measured in tiles.
*/
2021-10-23 23:27:14 +08:00
const Vec2& getMapSize() const { return _mapSize; }
2021-12-25 10:04:45 +08:00
2015-03-21 17:06:26 +08:00
/** Set the map's size property measured in tiles.
*
* @param mapSize The map's size property measured in tiles.
*/
2021-10-23 23:27:14 +08:00
void setMapSize(const Vec2& mapSize) { _mapSize = mapSize; }
2014-06-06 16:15:46 +08:00
2015-03-21 17:06:26 +08:00
/** The tiles's size property measured in pixels.
*
* @return The tiles's size property measured in pixels.
*/
2021-10-23 23:27:14 +08:00
const Vec2& getTileSize() const { return _tileSize; }
2021-12-25 10:04:45 +08:00
/** Set the tiles's size property measured in pixels.
2015-03-21 17:06:26 +08:00
*
* @param tileSize The tiles's size property measured in pixels.
*/
2021-10-23 23:27:14 +08:00
void setTileSize(const Vec2& tileSize) { _tileSize = tileSize; }
2014-06-06 16:15:46 +08:00
2021-12-25 10:04:45 +08:00
/** Get map orientation.
2015-03-21 17:06:26 +08:00
*
* @return The map orientation.
*/
int getMapOrientation() const { return _mapOrientation; }
2021-12-25 10:04:45 +08:00
/** Set map orientation.
2015-03-21 17:06:26 +08:00
*
* @param mapOrientation The map orientation.
*/
void setMapOrientation(int mapOrientation) { _mapOrientation = mapOrientation; }
2014-06-06 16:15:46 +08:00
2021-12-25 10:04:45 +08:00
/** Get object groups.
2015-03-21 17:06:26 +08:00
*
* @return Object groups.
*/
const Vector<TMXObjectGroup*>& getObjectGroups() const { return _objectGroups; }
Vector<TMXObjectGroup*>& getObjectGroups() { return _objectGroups; }
2021-12-25 10:04:45 +08:00
/** Set object groups.
2015-03-21 17:06:26 +08:00
*
* @param groups An object groups.
*/
2021-12-25 10:04:45 +08:00
void setObjectGroups(const Vector<TMXObjectGroup*>& groups) { _objectGroups = groups; }
2015-03-21 17:06:26 +08:00
/** Get properties.
*
* @return Properties.
*/
const ValueMap& getProperties() const { return _properties; }
2021-12-25 10:04:45 +08:00
/** Set properties.
2015-03-21 17:06:26 +08:00
*
* @param properties An ValueMap Properties.
*/
2021-12-25 10:04:45 +08:00
void setProperties(const ValueMap& properties) { _properties = properties; }
2014-06-06 16:15:46 +08:00
virtual std::string getDescription() const override;
/** Set all tile animations enabled or not.
* animations are not enabled by default
*/
void setTileAnimEnabled(bool enabled);
2020-08-28 17:26:30 +08:00
2022-07-15 19:17:01 +08:00
AX_DEPRECATED_ATTRIBUTE int getLayerNum() const { return getLayerCount(); }
2020-08-28 17:26:30 +08:00
int getLayerCount() const { return _layerCount; }
2021-12-26 23:26:34 +08:00
std::string_view getResourceFile() const { return _tmxFile; }
2020-08-28 17:26:30 +08:00
/**
* @js ctor
*/
FastTMXTiledMap();
2014-06-06 16:15:46 +08:00
/**
* @js NA
* @lua NA
*/
2019-10-23 14:58:31 +08:00
virtual ~FastTMXTiledMap();
2014-06-06 16:15:46 +08:00
/** initializes a TMX Tiled Map with a TMX file */
2021-12-26 23:26:34 +08:00
bool initWithTMXFile(std::string_view tmxFile);
2014-06-06 16:15:46 +08:00
/** initializes a TMX Tiled Map with a TMX formatted XML string and a path to TMX resources */
2021-12-26 23:26:34 +08:00
bool initWithXML(std::string_view tmxString, std::string_view resourcePath);
2021-12-25 10:04:45 +08:00
2020-08-28 17:26:30 +08:00
protected:
2021-12-25 10:04:45 +08:00
FastTMXLayer* parseLayer(TMXLayerInfo* layerInfo, TMXMapInfo* mapInfo);
TMXTilesetInfo* tilesetForLayer(TMXLayerInfo* layerInfo, TMXMapInfo* mapInfo);
2014-06-06 16:15:46 +08:00
void buildWithMapInfo(TMXMapInfo* mapInfo);
/** the map's size property measured in tiles */
2021-10-23 23:27:14 +08:00
Vec2 _mapSize;
2014-06-06 16:15:46 +08:00
/** the tiles's size property measured in pixels */
2021-10-23 23:27:14 +08:00
Vec2 _tileSize;
2014-06-06 16:15:46 +08:00
/** map orientation */
int _mapOrientation;
/** object groups */
Vector<TMXObjectGroup*> _objectGroups;
/** properties */
ValueMap _properties;
2021-12-25 10:04:45 +08:00
2014-06-06 16:15:46 +08:00
//! tile properties
ValueMapIntKey _tileProperties;
2020-08-28 17:26:30 +08:00
int _layerCount = 0;
std::string _tmxFile;
2021-12-25 10:04:45 +08:00
2014-06-06 16:15:46 +08:00
private:
2022-07-15 19:17:01 +08:00
AX_DISALLOW_COPY_AND_ASSIGN(FastTMXTiledMap);
2014-06-06 16:15:46 +08:00
};
// end of tilemap_parallax_nodes group
2015-03-21 17:06:26 +08:00
/** @} */
// @API compatible
typedef FastTMXTiledMap TMXTiledMap;
2014-06-06 16:15:46 +08:00
NS_AX_END