Skip to content
Merged
193 changes: 147 additions & 46 deletions cocos/2d/CCTMXLayer.cpp

Large diffs are not rendered by default.

83 changes: 82 additions & 1 deletion cocos/2d/CCTMXLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ NS_CC_BEGIN
class TMXMapInfo;
class TMXLayerInfo;
class TMXTilesetInfo;
class TMXTileAnimManager;
struct _ccCArray;

/**
Expand Down Expand Up @@ -294,6 +295,22 @@ class CC_DLL TMXLayer : public SpriteBatchNode
*/
virtual std::string getDescription() const override;

/** Map from gid of animated tile to its instance.
*
* @return Map from gid of animated tile to its instance.
*/
const std::map<uint32_t, std::vector<Vec2>>* getAnimTileCoord() {
return &_animTileCoord;
}

bool hasTileAnimation() const {
return !_animTileCoord.empty();
}

TMXTileAnimManager* getTileAnimManager() const {
return _tileAnimManager;
}

protected:
Vec2 getPositionForIsoAt(const Vec2& pos);
Vec2 getPositionForOrthoAt(const Vec2& pos);
Expand Down Expand Up @@ -353,6 +370,71 @@ class CC_DLL TMXLayer : public SpriteBatchNode
int _hexSideLength;
/** properties from the layer. They can be added using Tiled */
ValueMap _properties;

/** map from gid of animated tile to its instance. Also useful for optimization*/
std::map<uint32_t, std::vector<Vec2>> _animTileCoord;
/** pointer to the tile animation manager of this layer */
TMXTileAnimManager *_tileAnimManager = nullptr;
};

/** @brief TMXTileAnimTask represents the frame-tick task of an animated tile.
* It is a assistant class for TMXTileAnimTicker.
*/
class CC_DLL TMXTileAnimTask : public Ref
{
public:
TMXTileAnimTask(TMXLayer *layer, TMXTileAnimInfo *animation, const Vec2 &tilePos);
static TMXTileAnimTask * create(TMXLayer *layer, TMXTileAnimInfo *animation, const Vec2 &tilePos);
/** start the animation task */
void start();
/** stop the animation task */
void stop();
bool isRunning() const {
return _isRunning;
}

protected:
/** set texture of tile to current frame */
void setCurrFrame();
/** tick to next frame and schedule next tick */
void tickAndScheduleNext(float dt);

bool _isRunning = false;
/** key of schedule task for specific animated tile */
std::string _key;
TMXLayer *_layer = nullptr;
/** position of the animated tile */
Vec2 _tilePosition;
/** AnimationInfo on this tile */
TMXTileAnimInfo *_animation = nullptr;
/** Index of the frame that should be drawn currently */
uint32_t _currentFrame = 0;
uint32_t _frameCount = 0;
};

/** @brief TMXTileAnimManager controls all tile animation of a layer.
*/
class CC_DLL TMXTileAnimManager : public Ref
{
public:
static TMXTileAnimManager * create(TMXLayer *layer);
explicit TMXTileAnimManager(TMXLayer *layer);

/** start all tile animations */
void startAll();
/** stop all tile animations */
void stopAll();

/** get vector of tasks */
const Vector<TMXTileAnimTask*>& getTasks() const {
return _tasks;
}

protected:
bool _started = false;
/** vector contains all tasks of this layer */
Vector<TMXTileAnimTask*> _tasks;
TMXLayer* _layer = nullptr;
};

// end of tilemap_parallax_nodes group
Expand All @@ -361,4 +443,3 @@ class CC_DLL TMXLayer : public SpriteBatchNode
NS_CC_END

#endif //__CCTMX_LAYER_H__

18 changes: 18 additions & 0 deletions cocos/2d/CCTMXTiledMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,4 +273,22 @@ int TMXTiledMap::getLayerNum()
return _tmxLayerNum;
}

void TMXTiledMap::setTileAnimEnabled(bool enabled)
{
for (auto& child : _children)
{
TMXLayer* layer = dynamic_cast<TMXLayer*>(child);
if(layer)
{
if(layer->hasTileAnimation())
{
if(enabled)
layer->getTileAnimManager()->startAll();
else
layer->getTileAnimManager()->stopAll();
}
}
}
}

NS_CC_END
5 changes: 5 additions & 0 deletions cocos/2d/CCTMXTiledMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,11 @@ class CC_DLL TMXTiledMap : public Node
int getLayerNum();
const std::string& getResourceFile() const { return _tmxFile; }

/** Set all tile animations enabled or not.
* animations are not enabled by default
*/
void setTileAnimEnabled(bool enabled);

CC_CONSTRUCTOR_ACCESS:
/**
* @js ctor
Expand Down
40 changes: 40 additions & 0 deletions cocos/2d/CCTMXXMLParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,19 @@ void TMXMapInfo::startElement(void* /*ctx*/, const char *name, const char **atts
dict["polylinePoints"] = Value(pointsArray);
}
}
else if(elementName == "animation")
{
TMXTilesetInfo* info = tmxMapInfo->getTilesets().back();
info->_animationInfo.insert(tmxMapInfo->getParentGID(), TMXTileAnimInfo::create(tmxMapInfo->getParentGID()));
tmxMapInfo->setParentElement(TMXPropertyAnimation);
}
else if(elementName == "frame")
{
TMXTilesetInfo* info = tmxMapInfo->getTilesets().back();
auto animInfo = info->_animationInfo.at(tmxMapInfo->getParentGID());
// calculate gid of frame
animInfo->_frames.emplace_back(TMXTileAnimFrame(info->_firstGid + attributeDict["tileid"].asInt(), attributeDict["duration"].asFloat()));
}
}

void TMXMapInfo::endElement(void* /*ctx*/, const char *name)
Expand Down Expand Up @@ -792,6 +805,10 @@ void TMXMapInfo::endElement(void* /*ctx*/, const char *name)
{
_recordFirstGID = true;
}
else if (elementName == "animation")
{
tmxMapInfo->setParentElement(TMXPropertyNone);
}
}

void TMXMapInfo::textHandler(void* /*ctx*/, const char *ch, size_t len)
Expand All @@ -807,4 +824,27 @@ void TMXMapInfo::textHandler(void* /*ctx*/, const char *ch, size_t len)
}
}

TMXTileAnimFrame::TMXTileAnimFrame(uint32_t tileID, float duration)
: _tileID(tileID)
, _duration(duration)
{
}

TMXTileAnimInfo::TMXTileAnimInfo(uint32_t tileID)
: _tileID(tileID)
{
}

TMXTileAnimInfo *TMXTileAnimInfo::create(uint32_t tileID)
{
TMXTileAnimInfo *ret = new (std::nothrow) TMXTileAnimInfo(tileID);
if (ret)
{
ret->autorelease();
return ret;
}
CC_SAFE_DELETE(ret);
return nullptr;
}

NS_CC_END
36 changes: 35 additions & 1 deletion cocos/2d/CCTMXXMLParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ THE SOFTWARE.
#include "platform/CCSAXParser.h"
#include "base/CCVector.h"
#include "base/CCValue.h"
#include "base/CCMap.h"
#include "2d/CCTMXObjectGroup.h" // needed for Vector<TMXObjectGroup*> for binding

#include <string>
Expand Down Expand Up @@ -72,7 +73,8 @@ enum {
TMXPropertyLayer,
TMXPropertyObjectGroup,
TMXPropertyObject,
TMXPropertyTile
TMXPropertyTile,
TMXPropertyAnimation
};

typedef enum TMXTileFlags_ {
Expand Down Expand Up @@ -119,6 +121,35 @@ class CC_DLL TMXLayerInfo : public Ref
Vec2 _offset;
};

/** @brief TMXTileAnimFrame contains the information about the frame of a animated tile like:
- Frame gid
- duration of this frame

This information is obtained from the TMX file.
*/
struct CC_DLL TMXTileAnimFrame
{
TMXTileAnimFrame(uint32_t tileID, float duration);
/** gid of the frame */
uint32_t _tileID = 0;
/** duration of the frame */
float _duration = 0.0f;
};

/** @brief TMXTileAnimInfo contains the information about the animated tile like:
- Animated Tile gid
- frames the animated tile contains

This information is obtained from the TMX file.
*/
struct CC_DLL TMXTileAnimInfo : public Ref
{
static TMXTileAnimInfo * create(uint32_t tileID);
explicit TMXTileAnimInfo(uint32_t tileID);
uint32_t _tileID = 0;
std::vector<TMXTileAnimFrame> _frames;
};

/** @brief TMXTilesetInfo contains the information about the tilesets like:
- Tileset name
- Tileset spacing
Expand All @@ -144,6 +175,9 @@ class CC_DLL TMXTilesetInfo : public Ref
Size _imageSize;
std::string _originSourceImage;

//! map from gid of animated tile to its animation info
Map<uint32_t, TMXTileAnimInfo*> _animationInfo;

public:
/**
* @js ctor
Expand Down
Loading