2011-03-19 10:59:01 +08:00
|
|
|
/****************************************************************************
|
2012-09-24 21:22:20 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2011-03-19 10:59:01 +08:00
|
|
|
Copyright (c) 2009-2010 Ricardo Quesada
|
2011-07-04 14:11:43 +08:00
|
|
|
Copyright (c) 2011 Zynga Inc.
|
2011-03-19 10:59:01 +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_LAYER_H__
|
|
|
|
#define __CCTMX_LAYER_H__
|
2012-03-16 13:42:53 +08:00
|
|
|
|
2011-03-19 10:59:01 +08:00
|
|
|
#include "CCTMXObjectGroup.h"
|
2012-06-19 13:50:11 +08:00
|
|
|
#include "base_nodes/CCAtlasNode.h"
|
|
|
|
#include "sprite_nodes/CCSpriteBatchNode.h"
|
2012-03-16 13:42:53 +08:00
|
|
|
#include "CCTMXXMLParser.h"
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
class TMXMapInfo;
|
|
|
|
class TMXLayerInfo;
|
|
|
|
class TMXTilesetInfo;
|
2012-03-16 13:42:53 +08:00
|
|
|
struct _ccCArray;
|
|
|
|
|
2012-06-20 18:09:11 +08:00
|
|
|
/**
|
|
|
|
* @addtogroup tilemap_parallax_nodes
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
/** @brief TMXLayer represents the TMX layer.
|
2012-03-16 13:42:53 +08:00
|
|
|
|
2013-06-20 14:13:12 +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
|
2012-03-16 13:42:53 +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.
|
|
|
|
|
|
|
|
On the other hand, if the "cc_vertexz" property has the "automatic" value, then the tiles will use an automatic vertex Z value.
|
2012-09-17 15:02:24 +08:00
|
|
|
Also before drawing the tiles, GL_ALPHA_TEST will be enabled, and disabled after drawing them. The used alpha func will be:
|
2012-03-16 13:42:53 +08:00
|
|
|
|
|
|
|
glAlphaFunc( GL_GREATER, value )
|
|
|
|
|
|
|
|
"value" by default is 0, but you can change it from Tiled by adding the "cc_alpha_func" property to the layer.
|
2012-09-17 15:02:24 +08:00
|
|
|
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
|
2012-03-16 13:42:53 +08:00
|
|
|
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 v0.8.1
|
2013-06-20 14:13:12 +08:00
|
|
|
Tiles can have tile flags for additional properties. At the moment only flip horizontal and flip vertical are used. These bit flags are defined in TMXXMLParser.h.
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
@since 1.1
|
2012-03-16 13:42:53 +08:00
|
|
|
*/
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
class CC_DLL TMXLayer : public SpriteBatchNode
|
2012-03-16 13:42:53 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
/** size of the layer in tiles */
|
2013-06-20 14:13:12 +08:00
|
|
|
CC_SYNTHESIZE_PASS_BY_REF(Size, _layerSize, LayerSize);
|
2012-09-17 15:02:24 +08:00
|
|
|
/** size of the map's tile (could be different from the tile's size) */
|
2013-06-20 14:13:12 +08:00
|
|
|
CC_SYNTHESIZE_PASS_BY_REF(Size, _mapTileSize, MapTileSize);
|
2012-04-19 14:35:52 +08:00
|
|
|
/** pointer to the map of tiles */
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SYNTHESIZE(unsigned int*, _tiles, Tiles);
|
2012-09-17 15:02:24 +08:00
|
|
|
/** Tileset information for the layer */
|
2013-06-20 14:13:12 +08:00
|
|
|
CC_PROPERTY(TMXTilesetInfo*, _tileSet, TileSet);
|
2012-04-19 14:35:52 +08:00
|
|
|
/** Layer orientation, which is the same as the map orientation */
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SYNTHESIZE(unsigned int, _layerOrientation, LayerOrientation);
|
2012-04-19 14:35:52 +08:00
|
|
|
/** properties from the layer. They can be added using Tiled */
|
2013-06-20 14:13:12 +08:00
|
|
|
CC_PROPERTY(Dictionary*, _properties, Properties);
|
2012-03-16 13:42:53 +08:00
|
|
|
public:
|
2013-06-20 14:13:12 +08:00
|
|
|
TMXLayer();
|
|
|
|
virtual ~TMXLayer();
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
/** creates a TMXLayer with an tileset info, a layer info and a map info */
|
|
|
|
static TMXLayer * create(TMXTilesetInfo *tilesetInfo, TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo);
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
/** initializes a TMXLayer with a tileset info, a layer info and a map info */
|
|
|
|
bool initWithTilesetInfo(TMXTilesetInfo *tilesetInfo, TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/** dealloc the map that contains the tile position from memory.
|
|
|
|
Unless you want to know at runtime the tiles positions, you can safely call this method.
|
|
|
|
If you are going to call layer->tileGIDAt() then, don't release the map
|
|
|
|
*/
|
|
|
|
void releaseMap();
|
|
|
|
|
2013-06-20 14:13:12 +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.
|
2012-04-19 14:35:52 +08:00
|
|
|
You can remove either by calling:
|
|
|
|
- layer->removeChild(sprite, cleanup);
|
2013-07-12 18:04:32 +08:00
|
|
|
- or layer->removeTileAt(Point(x,y));
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
Sprite* tileAt(const Point& tileCoordinate);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/** returns the tile gid at a given tile coordinate.
|
|
|
|
if it returns 0, it means that the tile is empty.
|
|
|
|
This method requires the the tile map has not been previously released (eg. don't call layer->releaseMap())
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
unsigned int tileGIDAt(const Point& tileCoordinate);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/** returns the tile gid at a given tile coordinate. It also returns the tile flags.
|
|
|
|
This method requires the the tile map has not been previously released (eg. don't call [layer releaseMap])
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
unsigned int tileGIDAt(const Point& tileCoordinate, ccTMXTileFlags* flags);
|
2012-04-19 14:35:52 +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.
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
void setTileGID(unsigned int gid, const Point& tileCoordinate);
|
2012-04-19 14:35:52 +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
|
|
|
|
*/
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void setTileGID(unsigned int gid, const Point& tileCoordinate, ccTMXTileFlags flags);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/** removes a tile at given tile coordinate */
|
2013-06-20 14:13:12 +08:00
|
|
|
void removeTileAt(const Point& tileCoordinate);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/** returns the position in points of a given tile coordinate */
|
2013-06-20 14:13:12 +08:00
|
|
|
Point positionAt(const Point& tileCoordinate);
|
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);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/** Creates the tiles */
|
|
|
|
void setupTiles();
|
|
|
|
|
2013-07-16 03:43:22 +08:00
|
|
|
inline const char* getLayerName(){ return _layerName.c_str(); }
|
|
|
|
inline void setLayerName(const char *layerName){ _layerName = layerName; }
|
|
|
|
|
|
|
|
//
|
|
|
|
// Override
|
|
|
|
//
|
2013-06-20 14:13:12 +08:00
|
|
|
/** TMXLayer doesn't support adding a Sprite manually.
|
2013-07-16 03:43:22 +08:00
|
|
|
@warning addchild(z, tag); is not supported on TMXLayer. Instead of setTileGID.
|
|
|
|
*/
|
|
|
|
virtual void addChild(Node * child, int zOrder, int tag) override;
|
2012-04-19 14:35:52 +08:00
|
|
|
// super method
|
2013-07-16 03:43:22 +08:00
|
|
|
void removeChild(Node* child, bool cleanup) override;
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-03-16 13:42:53 +08:00
|
|
|
private:
|
2013-06-20 14:13:12 +08:00
|
|
|
Point positionForIsoAt(const Point& pos);
|
|
|
|
Point positionForOrthoAt(const Point& pos);
|
|
|
|
Point positionForHexAt(const Point& pos);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Point calculateLayerOffset(const Point& offset);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-09-17 15:02:24 +08:00
|
|
|
/* optimization methods */
|
2013-06-20 14:13:12 +08:00
|
|
|
Sprite* appendTileForGID(unsigned int gid, const Point& pos);
|
|
|
|
Sprite* insertTileForGID(unsigned int gid, const Point& pos);
|
|
|
|
Sprite* updateTileForGID(unsigned int gid, const Point& pos);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/* The layer recognizes some special properties, like cc_vertez */
|
|
|
|
void parseInternalProperties();
|
2013-06-20 14:13:12 +08:00
|
|
|
void setupTileSprite(Sprite* sprite, Point pos, unsigned int gid);
|
|
|
|
Sprite* reusedTileWithRect(Rect rect);
|
|
|
|
int vertexZForPos(const Point& pos);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// index
|
|
|
|
unsigned int atlasIndexForExistantZ(unsigned int z);
|
|
|
|
unsigned int atlasIndexForNewZ(int z);
|
2012-03-16 13:42:53 +08:00
|
|
|
protected:
|
2012-04-19 14:35:52 +08:00
|
|
|
//! name of the layer
|
2013-06-15 14:03:30 +08:00
|
|
|
std::string _layerName;
|
2012-04-19 14:35:52 +08:00
|
|
|
//! TMX Layer supports opacity
|
2013-06-15 14:03:30 +08:00
|
|
|
unsigned char _opacity;
|
2012-03-16 13:42:53 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
unsigned int _minGID;
|
|
|
|
unsigned int _maxGID;
|
2012-03-16 13:42:53 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
//! Only used when vertexZ is used
|
2013-06-15 14:03:30 +08:00
|
|
|
int _vertexZvalue;
|
|
|
|
bool _useAutomaticVertexZ;
|
2012-03-16 13:42:53 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
//! used for optimization
|
2013-06-20 14:13:12 +08:00
|
|
|
Sprite *_reusedTile;
|
2013-06-15 14:03:30 +08:00
|
|
|
ccCArray *_atlasIndexArray;
|
2012-03-16 13:42:53 +08:00
|
|
|
|
|
|
|
// used for retina display
|
2013-06-15 14:03:30 +08:00
|
|
|
float _contentScaleFactor;
|
2012-03-16 13:42:53 +08:00
|
|
|
};
|
|
|
|
|
2012-06-20 18:09:11 +08:00
|
|
|
// end of tilemap_parallax_nodes group
|
|
|
|
/// @}
|
|
|
|
|
2012-03-16 13:42:53 +08:00
|
|
|
NS_CC_END
|
|
|
|
|
2011-03-19 10:59:01 +08:00
|
|
|
#endif //__CCTMX_LAYER_H__
|
|
|
|
|