2014-06-06 16:15:46 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2011 Zynga Inc.
|
|
|
|
Copyright (c) 2013-2014 Chukong Technologies 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.
|
|
|
|
****************************************************************************/
|
2014-06-27 18:00:06 +08:00
|
|
|
#ifndef __CC_FAST_TMX_LAYER_H__
|
|
|
|
#define __CC_FAST_TMX_LAYER_H__
|
2014-06-06 16:15:46 +08:00
|
|
|
|
|
|
|
#include <map>
|
2014-06-24 11:04:17 +08:00
|
|
|
#include <unordered_map>
|
2014-08-28 17:03:29 +08:00
|
|
|
#include "2d/CCNode.h"
|
|
|
|
#include "2d/CCTMXXMLParser.h"
|
|
|
|
#include "renderer/CCPrimitiveCommand.h"
|
2014-08-29 15:39:52 +08:00
|
|
|
#include "base/CCMap.h"
|
2014-06-06 16:15:46 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
class TMXMapInfo;
|
|
|
|
class TMXLayerInfo;
|
|
|
|
class TMXTilesetInfo;
|
|
|
|
class Texture2D;
|
|
|
|
class Sprite;
|
|
|
|
struct _ccCArray;
|
|
|
|
|
2014-07-17 10:46:20 +08:00
|
|
|
namespace experimental{
|
|
|
|
|
2014-06-06 16:15:46 +08:00
|
|
|
/**
|
2015-03-21 17:06:26 +08:00
|
|
|
* @addtogroup _2d
|
2014-06-06 16:15:46 +08:00
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2014-06-19 15:57:13 +08:00
|
|
|
/** @brief FastTMXLayer represents the TMX layer.
|
2014-06-06 16:15:46 +08:00
|
|
|
|
2015-03-21 17:06:26 +08:00
|
|
|
* It is a subclass of SpriteBatchNode. By default the tiles are rendered using a TextureAtlas.
|
|
|
|
* If you modify a tile on runtime, then, that tile will become a Sprite, otherwise no Sprite objects are created.
|
|
|
|
* The benefits of using Sprite objects as tiles are:
|
|
|
|
* - tiles (Sprite) can be rotated/scaled/moved with a nice API.
|
2014-06-06 16:15:46 +08:00
|
|
|
|
2015-03-21 17:06:26 +08:00
|
|
|
* If the layer contains a property named "cc_vertexz" with an integer (in can be positive or negative),
|
|
|
|
* then all the tiles belonging to the layer will use that value as their OpenGL vertex Z for depth.
|
2014-06-06 16:15:46 +08:00
|
|
|
|
2015-03-21 17:06:26 +08:00
|
|
|
* On the other hand, if the "cc_vertexz" property has the "automatic" value, then the tiles will use an automatic vertex Z value.
|
|
|
|
* Also before drawing the tiles, GL_ALPHA_TEST will be enabled, and disabled after drawing them. The used alpha func will be:
|
2014-06-06 16:15:46 +08:00
|
|
|
|
2015-03-21 17:06:26 +08:00
|
|
|
* glAlphaFunc( GL_GREATER, value ).
|
2014-06-06 16:15:46 +08:00
|
|
|
|
2015-03-21 17:06:26 +08:00
|
|
|
* "value" by default is 0, but you can change it from Tiled by adding the "cc_alpha_func" property to the layer.
|
|
|
|
* The value 0 should work for most cases, but if you have tiles that are semi-transparent, then you might want to use a different
|
|
|
|
* value, like 0.5.
|
|
|
|
|
|
|
|
* For further information, please see the programming guide:
|
|
|
|
* http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:tiled_maps
|
|
|
|
|
|
|
|
* @since v3.2
|
|
|
|
*/
|
2014-06-06 16:15:46 +08:00
|
|
|
|
2014-07-17 14:09:53 +08:00
|
|
|
class CC_DLL TMXLayer : public Node
|
2014-06-06 16:15:46 +08:00
|
|
|
{
|
|
|
|
public:
|
2015-03-21 17:06:26 +08:00
|
|
|
/** Creates a FastTMXLayer with an tileset info, a layer info and a map info.
|
|
|
|
*
|
|
|
|
* @param tilesetInfo An tileset info.
|
|
|
|
* @param layerInfo A layer info.
|
|
|
|
* @param mapInfo A map info.
|
|
|
|
* @return Reruen an autorelease object.
|
|
|
|
*/
|
2014-07-17 14:09:53 +08:00
|
|
|
static TMXLayer * create(TMXTilesetInfo *tilesetInfo, TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo);
|
2014-06-06 16:15:46 +08:00
|
|
|
/**
|
|
|
|
* @js ctor
|
|
|
|
*/
|
2014-07-17 14:09:53 +08:00
|
|
|
TMXLayer();
|
2014-06-06 16:15:46 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2014-07-17 14:09:53 +08:00
|
|
|
virtual ~TMXLayer();
|
2014-06-06 16:15:46 +08:00
|
|
|
|
2015-03-21 17:06:26 +08:00
|
|
|
/** Returns the tile gid at a given tile coordinate. It also returns the tile flags.
|
|
|
|
*
|
|
|
|
* @param tileCoordinate The tile coordinate.
|
|
|
|
* @param flags A TMXTileFlags.
|
|
|
|
* @return The tile gid at a given tile coordinate. It also returns the tile flags.
|
2014-06-06 16:15:46 +08:00
|
|
|
*/
|
2014-06-19 15:57:13 +08:00
|
|
|
int getTileGIDAt(const Vec2& tileCoordinate, TMXTileFlags* flags = nullptr);
|
2014-06-06 16:15:46 +08:00
|
|
|
|
2015-03-21 17:06:26 +08:00
|
|
|
/** Sets the tile gid (gid = tile global id) at a given tile coordinate.
|
|
|
|
* The Tile GID can be obtained by using the method "tileGIDAt" or by using the TMX editor -> Tileset Mgr +1.
|
|
|
|
* If a tile is already placed at that position, then it will be removed.
|
|
|
|
* @param gid The gid value.
|
|
|
|
* @param tileCoordinate The tile coordinate.
|
|
|
|
*/
|
2014-06-19 15:57:13 +08:00
|
|
|
void setTileGID(int gid, const Vec2& tileCoordinate);
|
2014-06-06 16:15:46 +08:00
|
|
|
|
2015-03-21 17:06:26 +08:00
|
|
|
/** Sets the tile gid (gid = tile global id) at a given tile coordinate.
|
|
|
|
* The Tile GID can be obtained by using the method "tileGIDAt" or by using the TMX editor -> Tileset Mgr +1.
|
|
|
|
* If a tile is already placed at that position, then it will be removed.
|
|
|
|
* Use withFlags if the tile flags need to be changed as well.
|
|
|
|
*
|
|
|
|
* @param gid A integer value,it will be sets the tile gid.
|
|
|
|
* @param tileCoordinate The tile coordinate.
|
|
|
|
* @param flags A TMXTileFlags.
|
2014-06-06 16:15:46 +08:00
|
|
|
*/
|
2014-06-19 15:57:13 +08:00
|
|
|
void setTileGID(int gid, const Vec2& tileCoordinate, TMXTileFlags flags);
|
2014-06-06 16:15:46 +08:00
|
|
|
|
2015-03-21 17:06:26 +08:00
|
|
|
/** Removes a tile at given tile coordinate.
|
|
|
|
*
|
|
|
|
* @param tileCoordinate The tile Coordinate.
|
|
|
|
*/
|
2014-06-19 15:57:13 +08:00
|
|
|
void removeTileAt(const Vec2& tileCoordinate);
|
2014-06-06 16:15:46 +08:00
|
|
|
|
2015-03-21 17:06:26 +08:00
|
|
|
/** Returns the position in points of a given tile coordinate.
|
|
|
|
*
|
|
|
|
* @param tileCoordinate The tile Coordinate.
|
|
|
|
* @return The position in points of a given tile coordinate.
|
|
|
|
*/
|
2014-06-19 15:57:13 +08:00
|
|
|
Vec2 getPositionAt(const Vec2& tileCoordinate);
|
2014-06-06 16:15:46 +08:00
|
|
|
|
2015-03-21 17:06:26 +08:00
|
|
|
/** Return the value for the specific property name.
|
|
|
|
*
|
|
|
|
* @param propertyName The value for the specific property name.
|
|
|
|
* @return The value for the specific property name.
|
|
|
|
*/
|
2014-06-06 16:15:46 +08:00
|
|
|
Value getProperty(const std::string& propertyName) const;
|
|
|
|
|
2015-03-21 17:06:26 +08:00
|
|
|
/** Creates the tiles. */
|
2014-06-06 16:15:46 +08:00
|
|
|
void setupTiles();
|
2015-03-21 17:06:26 +08:00
|
|
|
|
|
|
|
/** Get the tile layer name.
|
|
|
|
*
|
|
|
|
* @return The tile layer name.
|
|
|
|
*/
|
2014-06-06 16:15:46 +08:00
|
|
|
inline const std::string& getLayerName(){ return _layerName; }
|
2015-03-21 17:06:26 +08:00
|
|
|
|
|
|
|
/** Set the tile layer name.
|
|
|
|
*
|
|
|
|
* @param layerName The new layer name.
|
|
|
|
*/
|
2014-06-06 16:15:46 +08:00
|
|
|
inline void setLayerName(const std::string& layerName){ _layerName = layerName; }
|
|
|
|
|
2015-03-21 17:06:26 +08:00
|
|
|
/** Size of the layer in tiles.
|
|
|
|
*
|
|
|
|
* @return Size of the layer in tiles.
|
|
|
|
*/
|
2014-06-06 16:15:46 +08:00
|
|
|
inline const Size& getLayerSize() const { return _layerSize; };
|
2015-03-21 17:06:26 +08:00
|
|
|
|
|
|
|
/** Set the size of the layer in tiles.
|
|
|
|
*
|
|
|
|
* @param size The new size of the layer in tiles.
|
|
|
|
*/
|
2014-06-06 16:15:46 +08:00
|
|
|
inline void setLayerSize(const Size& size) { _layerSize = size; };
|
|
|
|
|
2015-03-21 17:06:26 +08:00
|
|
|
/** Size of the map's tile (could be different from the tile's size).
|
|
|
|
*
|
|
|
|
* @return Size of the map's tile (could be different from the tile's size).
|
|
|
|
*/
|
2014-06-06 16:15:46 +08:00
|
|
|
inline const Size& getMapTileSize() const { return _mapTileSize; };
|
2015-03-21 17:06:26 +08:00
|
|
|
|
|
|
|
/** Set the size of the map's tile.
|
|
|
|
*
|
|
|
|
* @param size The new size of the map's tile.
|
|
|
|
*/
|
2014-06-06 16:15:46 +08:00
|
|
|
inline void setMapTileSize(const Size& size) { _mapTileSize = size; };
|
|
|
|
|
2015-03-21 17:06:26 +08:00
|
|
|
/** Pointer to the map of tiles.
|
2014-06-06 16:15:46 +08:00
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
2015-03-21 17:06:26 +08:00
|
|
|
* @return The pointer to the map of tiles.
|
2014-06-06 16:15:46 +08:00
|
|
|
*/
|
2014-06-26 20:45:08 +08:00
|
|
|
const uint32_t* getTiles() const { return _tiles; };
|
2015-03-21 17:06:26 +08:00
|
|
|
|
|
|
|
/** Set the pointer to the map of tiles.
|
|
|
|
*
|
|
|
|
* @param tiles The pointer to the map of tiles.
|
|
|
|
*/
|
2014-06-26 20:45:08 +08:00
|
|
|
void setTiles(uint32_t* tiles) { _tiles = tiles; _quadsDirty = true;};
|
2014-06-06 16:15:46 +08:00
|
|
|
|
2015-03-21 17:06:26 +08:00
|
|
|
/** Tileset information for the layer.
|
|
|
|
*
|
|
|
|
* @return Tileset information for the layer.
|
|
|
|
*/
|
2014-06-06 16:15:46 +08:00
|
|
|
inline TMXTilesetInfo* getTileSet() const { return _tileSet; };
|
2015-03-21 17:06:26 +08:00
|
|
|
|
|
|
|
/** Set the tileset information for the layer.
|
|
|
|
*
|
|
|
|
* @param info The new tileset information for the layer.
|
|
|
|
*/
|
2014-06-06 16:15:46 +08:00
|
|
|
inline void setTileSet(TMXTilesetInfo* info) {
|
|
|
|
CC_SAFE_RETAIN(info);
|
|
|
|
CC_SAFE_RELEASE(_tileSet);
|
|
|
|
_tileSet = info;
|
|
|
|
};
|
|
|
|
|
2015-03-21 17:06:26 +08:00
|
|
|
/** Layer orientation, which is the same as the map orientation.
|
|
|
|
*
|
|
|
|
* @return Layer orientation, which is the same as the map orientation.
|
|
|
|
*/
|
2014-06-06 16:15:46 +08:00
|
|
|
inline int getLayerOrientation() const { return _layerOrientation; };
|
2015-03-21 17:06:26 +08:00
|
|
|
|
|
|
|
/** Set Layer orientation, which is the same as the map orientation.
|
|
|
|
*
|
|
|
|
* @param orientation Layer orientation, which is the same as the map orientation.
|
|
|
|
*/
|
2014-06-06 16:15:46 +08:00
|
|
|
inline void setLayerOrientation(int orientation) { _layerOrientation = orientation; };
|
|
|
|
|
2015-03-21 17:06:26 +08:00
|
|
|
/** Properties from the layer. They can be added using Tiled.
|
|
|
|
*
|
|
|
|
* @return Properties from the layer. They can be added using Tiled.
|
|
|
|
*/
|
2014-06-06 16:15:46 +08:00
|
|
|
inline const ValueMap& getProperties() const { return _properties; };
|
2015-03-21 17:06:26 +08:00
|
|
|
|
|
|
|
/** Properties from the layer. They can be added using Tiled.
|
|
|
|
*
|
|
|
|
* @return Properties from the layer. They can be added using Tiled.
|
|
|
|
*/
|
2014-06-06 16:15:46 +08:00
|
|
|
inline ValueMap& getProperties() { return _properties; };
|
2015-03-21 17:06:26 +08:00
|
|
|
|
|
|
|
/** Set the properties to the layer.
|
|
|
|
*
|
|
|
|
* @param properties The properties to the layer.
|
|
|
|
*/
|
2014-06-26 20:45:08 +08:00
|
|
|
inline void setProperties(const ValueMap& properties)
|
|
|
|
{
|
2014-06-06 16:15:46 +08:00
|
|
|
_properties = properties;
|
|
|
|
};
|
|
|
|
|
2015-03-21 17:06:26 +08:00
|
|
|
/** Returns the tile (Sprite) at a given a tile coordinate.
|
|
|
|
* The returned Sprite will be already added to the TMXLayer. Don't add it again.
|
|
|
|
* The Sprite can be treated like any other Sprite: rotated, scaled, translated, opacity, color, etc.
|
|
|
|
* You can remove either by calling:
|
|
|
|
* - layer->removeChild(sprite, cleanup);
|
|
|
|
*
|
|
|
|
* @return Returns the tile (Sprite) at a given a tile coordinate.
|
2014-06-06 16:15:46 +08:00
|
|
|
*/
|
2014-06-19 15:57:13 +08:00
|
|
|
Sprite* getTileAt(const Vec2& tileCoordinate);
|
2015-03-21 17:06:26 +08:00
|
|
|
|
|
|
|
/** Set an sprite to the tile,with the tile coordinate and gid.
|
|
|
|
*
|
|
|
|
* @param sprite A Sprite.
|
|
|
|
* @param pos The tile coordinate.
|
|
|
|
* @param gid The tile gid.
|
|
|
|
*/
|
2014-06-19 15:57:13 +08:00
|
|
|
void setupTileSprite(Sprite* sprite, Vec2 pos, int gid);
|
2014-06-06 16:15:46 +08:00
|
|
|
|
|
|
|
//
|
|
|
|
// Override
|
|
|
|
//
|
|
|
|
virtual std::string getDescription() const override;
|
|
|
|
virtual void draw(Renderer *renderer, const Mat4& transform, uint32_t flags) override;
|
|
|
|
void removeChild(Node* child, bool cleanup = true) override;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
bool initWithTilesetInfo(TMXTilesetInfo *tilesetInfo, TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo);
|
2014-06-27 14:42:53 +08:00
|
|
|
void updateTiles(const Rect& culledRect);
|
2014-06-19 15:57:13 +08:00
|
|
|
Vec2 calculateLayerOffset(const Vec2& offset);
|
2014-06-06 16:15:46 +08:00
|
|
|
|
|
|
|
/* The layer recognizes some special properties, like cc_vertez */
|
|
|
|
void parseInternalProperties();
|
|
|
|
|
|
|
|
Mat4 tileToNodeTransform();
|
|
|
|
Rect tileBoundsForClipTransform(const Mat4 &tileToClip);
|
|
|
|
|
2014-06-19 15:57:13 +08:00
|
|
|
int getVertexZForPos(const Vec2& pos);
|
2014-06-06 16:15:46 +08:00
|
|
|
|
2014-06-26 20:45:08 +08:00
|
|
|
//Flip flags is packed into gid
|
|
|
|
void setFlaggedTileGIDByIndex(int index, int gid);
|
|
|
|
|
|
|
|
//
|
|
|
|
void updateTotalQuads();
|
|
|
|
|
2014-08-04 17:57:31 +08:00
|
|
|
void onDraw(Primitive* primitive);
|
2014-06-26 20:45:08 +08:00
|
|
|
inline int getTileIndexByPos(int x, int y) const { return x + y * (int) _layerSize.width; }
|
2014-06-27 10:30:21 +08:00
|
|
|
|
|
|
|
void updateVertexBuffer();
|
2014-06-27 11:50:44 +08:00
|
|
|
void updateIndexBuffer();
|
2014-08-04 17:57:31 +08:00
|
|
|
void updatePrimitives();
|
2014-06-06 16:15:46 +08:00
|
|
|
protected:
|
|
|
|
|
|
|
|
//! name of the layer
|
|
|
|
std::string _layerName;
|
|
|
|
|
|
|
|
/** size of the layer in tiles */
|
|
|
|
Size _layerSize;
|
|
|
|
/** size of the map's tile (could be different from the tile's size) */
|
|
|
|
Size _mapTileSize;
|
|
|
|
/** pointer to the map of tiles */
|
|
|
|
uint32_t* _tiles;
|
|
|
|
/** Tileset information for the layer */
|
|
|
|
TMXTilesetInfo* _tileSet;
|
|
|
|
/** Layer orientation, which is the same as the map orientation */
|
|
|
|
int _layerOrientation;
|
|
|
|
/** properties from the layer. They can be added using Tiled */
|
|
|
|
ValueMap _properties;
|
|
|
|
|
|
|
|
Texture2D *_texture;
|
|
|
|
|
|
|
|
/** container for sprite children. map<index, pair<sprite, gid> > */
|
|
|
|
std::map<int, std::pair<Sprite*, int> > _spriteContainer;
|
|
|
|
|
2014-08-04 10:40:07 +08:00
|
|
|
//GLuint _buffersVBO; //0: vertex, 1: indices
|
2014-06-06 16:15:46 +08:00
|
|
|
|
|
|
|
Size _screenGridSize;
|
|
|
|
Rect _screenGridRect;
|
|
|
|
int _screenTileCount;
|
|
|
|
|
|
|
|
int _vertexZvalue;
|
|
|
|
bool _useAutomaticVertexZ;
|
|
|
|
|
|
|
|
/** tile coordinate to node coordinate transform */
|
|
|
|
Mat4 _tileToNodeTransform;
|
2014-06-27 14:42:53 +08:00
|
|
|
/** data for rendering */
|
2014-06-26 20:45:08 +08:00
|
|
|
bool _quadsDirty;
|
2014-07-02 17:16:03 +08:00
|
|
|
std::vector<int> _tileToQuadIndex;
|
2014-06-26 20:45:08 +08:00
|
|
|
std::vector<V3F_C4B_T2F_Quad> _totalQuads;
|
2014-09-17 11:06:15 +08:00
|
|
|
std::vector<GLushort> _indices;
|
2014-06-27 11:50:44 +08:00
|
|
|
std::map<int/*vertexZ*/, int/*offset to _indices by quads*/> _indicesVertexZOffsets;
|
|
|
|
std::unordered_map<int/*vertexZ*/, int/*number to quads*/> _indicesVertexZNumber;
|
2014-08-05 15:08:01 +08:00
|
|
|
std::vector<PrimitiveCommand> _renderCommands;
|
2014-06-06 18:03:44 +08:00
|
|
|
bool _dirty;
|
2014-06-27 18:00:06 +08:00
|
|
|
|
2014-08-01 17:50:22 +08:00
|
|
|
VertexBuffer* _vertexBuffer;
|
|
|
|
|
2014-08-04 16:08:54 +08:00
|
|
|
VertexData* _vData;
|
|
|
|
|
2014-08-04 10:40:07 +08:00
|
|
|
IndexBuffer* _indexBuffer;
|
|
|
|
|
2014-08-04 17:57:31 +08:00
|
|
|
Map<int , Primitive*> _primitives;
|
|
|
|
|
2014-06-27 18:00:06 +08:00
|
|
|
public:
|
|
|
|
/** Possible orientations of the TMX map */
|
|
|
|
static const int FAST_TMX_ORIENTATION_ORTHO;
|
|
|
|
static const int FAST_TMX_ORIENTATION_HEX;
|
|
|
|
static const int FAST_TMX_ORIENTATION_ISO;
|
2014-06-06 16:15:46 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// end of tilemap_parallax_nodes group
|
|
|
|
/// @}
|
2014-07-17 10:46:20 +08:00
|
|
|
} //end of namespace experimental
|
2014-06-06 16:15:46 +08:00
|
|
|
NS_CC_END
|
|
|
|
|
|
|
|
#endif //__CCTMX_LAYER2_H__
|