Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 145 additions & 0 deletions cocos/2d/CCFastTMXLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ bool FastTMXLayer::initWithTilesetInfo(TMXTilesetInfo *tilesetInfo, TMXLayerInfo
// mapInfo
_mapTileSize = mapInfo->getTileSize();
_layerOrientation = mapInfo->getOrientation();
_staggerAxis = mapInfo->getStaggerAxis();
_staggerIndex = mapInfo->getStaggerIndex();

// offset (after layer orientation is set);
Vec2 offset = this->calculateLayerOffset(layerInfo->_offset);
Expand Down Expand Up @@ -321,6 +323,57 @@ void FastTMXLayer::setupTiles()

_screenTileCount = (int)(_screenGridSize.width * _screenGridSize.height);

if (!_tileSet->_animationInfo.empty()) {
/// FastTMXLayer: anim support
for (int y = 0; y < _layerSize.height; y++)
{
for (int x = 0; x < _layerSize.width; x++)
{
int newX = x;
// fix correct render ordering in Hexagonal maps when stagger axis == x
if (_staggerAxis == TMXStaggerAxis_X && _layerOrientation == TMXOrientationHex)
{
if (_staggerIndex == TMXStaggerIndex_Odd)
{
if (x >= _layerSize.width / 2)
newX = (x - std::ceil(_layerSize.width / 2)) * 2 + 1;
else
newX = x * 2;
}
else {
// TMXStaggerIndex_Even
if (x >= static_cast<int>(_layerSize.width / 2))
newX = (x - static_cast<int>(_layerSize.width / 2)) * 2;
else
newX = x * 2 + 1;
}
}

int pos = static_cast<int>(newX + _layerSize.width * y);
int gid = _tiles[pos];

// gid are stored in little endian.
// if host is big endian, then swap
//if( o == CFByteOrderBigEndian )
// gid = CFSwapInt32( gid );
/* We support little endian.*/

// FIXME:: gid == 0 --> empty tile
if (gid != 0)
{
if (_tileSet->_animationInfo.find(gid) != _tileSet->_animationInfo.end())
{
_animTileCoord[gid].push_back(Vec2(newX, y));
}
}
}
}

if (hasTileAnimation())
{
_tileAnimManager = new TMXTileAnimManager(this);
}
}
}

Mat4 FastTMXLayer::tileToNodeTransform()
Expand Down Expand Up @@ -910,5 +963,97 @@ std::string FastTMXLayer::getDescription() const
return StringUtils::format("<FastTMXLayer | tag = %d, size = %d,%d>", _tag, (int)_mapTileSize.width, (int)_mapTileSize.height);
}

TMXTileAnimManager::TMXTileAnimManager(FastTMXLayer* layer)
{
_layer = layer;
for (const auto& p : *_layer->getAnimTileCoord())
{
for (auto tilePos : p.second)
{
_tasks.pushBack(TMXTileAnimTask::create(_layer, _layer->getTileSet()->_animationInfo.at(p.first), tilePos));
}
}
}

TMXTileAnimManager* TMXTileAnimManager::create(FastTMXLayer* layer)
{
TMXTileAnimManager* ret = new (std::nothrow) TMXTileAnimManager(layer);
if (ret)
{
ret->autorelease();
return ret;
}
CC_SAFE_DELETE(ret);
return nullptr;
}

void TMXTileAnimManager::startAll()
{
if (_started || _tasks.empty())
return;
_started = true;
for (auto& task : _tasks)
{
task->start();
}
}

void TMXTileAnimManager::stopAll()
{
if (!_started)
return;
_started = false;
for (auto& task : _tasks)
{
task->stop();
}
}

TMXTileAnimTask::TMXTileAnimTask(FastTMXLayer* layer, TMXTileAnimInfo* animation, const Vec2& tilePos)
{
_layer = layer;
_animation = animation;
_frameCount = static_cast<uint32_t>(_animation->_frames.size());
_tilePosition = tilePos;
std::stringstream ss;
ss << "TickAnimOnTilePos(" << _tilePosition.x << "," << _tilePosition.y << ")";
_key = ss.str();
}

void TMXTileAnimTask::tickAndScheduleNext(float dt)
{
setCurrFrame();
_layer->getParent()->scheduleOnce(CC_CALLBACK_1(TMXTileAnimTask::tickAndScheduleNext, this), _animation->_frames[_currentFrame]._duration / 1000.0f, _key);
}

void TMXTileAnimTask::start()
{
_isRunning = true;
tickAndScheduleNext(0.0f);
}

void TMXTileAnimTask::stop()
{
_isRunning = false;
_layer->getParent()->unschedule(_key);
}

void TMXTileAnimTask::setCurrFrame()
{
_layer->setTileGID(_animation->_frames[_currentFrame]._tileID, _tilePosition);
_currentFrame = (_currentFrame + 1) % _frameCount;
}

TMXTileAnimTask* TMXTileAnimTask::create(FastTMXLayer* layer, TMXTileAnimInfo* animation, const Vec2& tilePos)
{
TMXTileAnimTask* ret = new (std::nothrow) TMXTileAnimTask(layer, animation, tilePos);
if (ret)
{
ret->autorelease();
return ret;
}
CC_SAFE_DELETE(ret);
return nullptr;
}

NS_CC_END
92 changes: 91 additions & 1 deletion cocos/2d/CCFastTMXLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ NS_CC_BEGIN
class TMXMapInfo;
class TMXLayerInfo;
class TMXTilesetInfo;
class TMXTileAnimManager;
class Texture2D;
class Sprite;

Expand Down Expand Up @@ -275,10 +276,29 @@ class CC_DLL FastTMXLayer : public Node
virtual void draw(Renderer *renderer, const Mat4& transform, uint32_t flags) override;
void removeChild(Node* child, bool cleanup = true) override;

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

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

TMXTileAnimManager* getTileAnimManager() const {
return _tileAnimManager;
}

CC_CONSTRUCTOR_ACCESS:
bool initWithTilesetInfo(TMXTilesetInfo* tilesetInfo, TMXLayerInfo* layerInfo, TMXMapInfo* mapInfo);

protected:
virtual void setOpacity(uint8_t opacity) override;

bool initWithTilesetInfo(TMXTilesetInfo *tilesetInfo, TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo);

void updateTiles(const Rect& culledRect);
Vec2 calculateLayerOffset(const Vec2& offset);

Expand Down Expand Up @@ -315,9 +335,16 @@ class CC_DLL FastTMXLayer : public Node
TMXTilesetInfo* _tileSet = nullptr;
/** Layer orientation, which is the same as the map orientation */
int _layerOrientation = FAST_TMX_ORIENTATION_ORTHO;
int _staggerAxis = TMXStaggerAxis_Y;
int _staggerIndex = TMXStaggerIndex_Even;
/** 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::unordered_map<uint32_t, std::vector<Vec2>> _animTileCoord;
/** pointer to the tile animation manager of this layer */
TMXTileAnimManager* _tileAnimManager = nullptr;

Texture2D *_texture = nullptr;

/** container for sprite children. map<index, pair<sprite, gid> > */
Expand Down Expand Up @@ -356,6 +383,69 @@ class CC_DLL FastTMXLayer : public Node
backend::UniformLocation _alphaValueLocation;
};

/** @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(FastTMXLayer* layer, TMXTileAnimInfo* animation, const Vec2& tilePos);
static TMXTileAnimTask* create(FastTMXLayer* 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;
FastTMXLayer* _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(FastTMXLayer* layer);
explicit TMXTileAnimManager(FastTMXLayer* 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;
FastTMXLayer* _layer = nullptr;
};

// @API compatible
typedef FastTMXLayer TMXLayer;

// end of tilemap_parallax_nodes group
/// @}
NS_CC_END
22 changes: 22 additions & 0 deletions cocos/2d/CCFastTMXTiledMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ bool FastTMXTiledMap::initWithTMXFile(const std::string& tmxFile)
CCASSERT( !mapInfo->getTilesets().empty(), "FastTMXTiledMap: Map not found. Please check the filename.");
buildWithMapInfo(mapInfo);

_tmxFile = tmxFile;

return true;
}

Expand Down Expand Up @@ -192,6 +194,8 @@ void FastTMXTiledMap::buildWithMapInfo(TMXMapInfo* mapInfo)
idx++;
}
}

_layerCount = idx;
}

// public
Expand Down Expand Up @@ -257,4 +261,22 @@ std::string FastTMXTiledMap::getDescription() const
return StringUtils::format("<FastTMXTiledMap | Tag = %d, Layers = %d", _tag, static_cast<int>(_children.size()));
}

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

NS_CC_END
21 changes: 19 additions & 2 deletions cocos/2d/CCFastTMXTiledMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,18 @@ class CC_DLL FastTMXTiledMap : public Node

virtual std::string getDescription() const override;

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

CC_DEPRECATED_ATTRIBUTE int getLayerNum() const { return getLayerCount(); }

int getLayerCount() const { return _layerCount; }

const std::string& getResourceFile() const { return _tmxFile; }

CC_CONSTRUCTOR_ACCESS:
/**
* @js ctor
*/
Expand All @@ -218,6 +229,7 @@ class CC_DLL FastTMXTiledMap : public Node
/** initializes a TMX Tiled Map with a TMX formatted XML string and a path to TMX resources */
bool initWithXML(const std::string& tmxString, const std::string& resourcePath);

protected:
FastTMXLayer * parseLayer(TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo);
TMXTilesetInfo * tilesetForLayer(TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo);
void buildWithMapInfo(TMXMapInfo* mapInfo);
Expand All @@ -236,13 +248,18 @@ class CC_DLL FastTMXTiledMap : public Node
//! tile properties
ValueMapIntKey _tileProperties;

int _layerCount = 0;

std::string _tmxFile;
private:
CC_DISALLOW_COPY_AND_ASSIGN(FastTMXTiledMap);

};

// end of tilemap_parallax_nodes group
/** @} */


// @API compatible
typedef FastTMXTiledMap TMXTiledMap;

NS_CC_END
Loading