Skip to content

Commit 0653090

Browse files
authored
Merge pull request #182 from halx99/leve-one-tmxlayermap
Only leave one TMXLayer & TMXTileMap
2 parents fa9b2c3 + 50fe831 commit 0653090

File tree

26 files changed

+490
-3965
lines changed

26 files changed

+490
-3965
lines changed

cocos/2d/CCFastTMXLayer.cpp

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ bool FastTMXLayer::initWithTilesetInfo(TMXTilesetInfo *tilesetInfo, TMXLayerInfo
9393
// mapInfo
9494
_mapTileSize = mapInfo->getTileSize();
9595
_layerOrientation = mapInfo->getOrientation();
96+
_staggerAxis = mapInfo->getStaggerAxis();
97+
_staggerIndex = mapInfo->getStaggerIndex();
9698

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

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

326+
if (!_tileSet->_animationInfo.empty()) {
327+
/// FastTMXLayer: anim support
328+
for (int y = 0; y < _layerSize.height; y++)
329+
{
330+
for (int x = 0; x < _layerSize.width; x++)
331+
{
332+
int newX = x;
333+
// fix correct render ordering in Hexagonal maps when stagger axis == x
334+
if (_staggerAxis == TMXStaggerAxis_X && _layerOrientation == TMXOrientationHex)
335+
{
336+
if (_staggerIndex == TMXStaggerIndex_Odd)
337+
{
338+
if (x >= _layerSize.width / 2)
339+
newX = (x - std::ceil(_layerSize.width / 2)) * 2 + 1;
340+
else
341+
newX = x * 2;
342+
}
343+
else {
344+
// TMXStaggerIndex_Even
345+
if (x >= static_cast<int>(_layerSize.width / 2))
346+
newX = (x - static_cast<int>(_layerSize.width / 2)) * 2;
347+
else
348+
newX = x * 2 + 1;
349+
}
350+
}
351+
352+
int pos = static_cast<int>(newX + _layerSize.width * y);
353+
int gid = _tiles[pos];
354+
355+
// gid are stored in little endian.
356+
// if host is big endian, then swap
357+
//if( o == CFByteOrderBigEndian )
358+
// gid = CFSwapInt32( gid );
359+
/* We support little endian.*/
360+
361+
// FIXME:: gid == 0 --> empty tile
362+
if (gid != 0)
363+
{
364+
if (_tileSet->_animationInfo.find(gid) != _tileSet->_animationInfo.end())
365+
{
366+
_animTileCoord[gid].push_back(Vec2(newX, y));
367+
}
368+
}
369+
}
370+
}
371+
372+
if (hasTileAnimation())
373+
{
374+
_tileAnimManager = new TMXTileAnimManager(this);
375+
}
376+
}
324377
}
325378

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

966+
TMXTileAnimManager::TMXTileAnimManager(FastTMXLayer* layer)
967+
{
968+
_layer = layer;
969+
for (const auto& p : *_layer->getAnimTileCoord())
970+
{
971+
for (auto tilePos : p.second)
972+
{
973+
_tasks.pushBack(TMXTileAnimTask::create(_layer, _layer->getTileSet()->_animationInfo.at(p.first), tilePos));
974+
}
975+
}
976+
}
977+
978+
TMXTileAnimManager* TMXTileAnimManager::create(FastTMXLayer* layer)
979+
{
980+
TMXTileAnimManager* ret = new (std::nothrow) TMXTileAnimManager(layer);
981+
if (ret)
982+
{
983+
ret->autorelease();
984+
return ret;
985+
}
986+
CC_SAFE_DELETE(ret);
987+
return nullptr;
988+
}
989+
990+
void TMXTileAnimManager::startAll()
991+
{
992+
if (_started || _tasks.empty())
993+
return;
994+
_started = true;
995+
for (auto& task : _tasks)
996+
{
997+
task->start();
998+
}
999+
}
1000+
1001+
void TMXTileAnimManager::stopAll()
1002+
{
1003+
if (!_started)
1004+
return;
1005+
_started = false;
1006+
for (auto& task : _tasks)
1007+
{
1008+
task->stop();
1009+
}
1010+
}
1011+
1012+
TMXTileAnimTask::TMXTileAnimTask(FastTMXLayer* layer, TMXTileAnimInfo* animation, const Vec2& tilePos)
1013+
{
1014+
_layer = layer;
1015+
_animation = animation;
1016+
_frameCount = static_cast<uint32_t>(_animation->_frames.size());
1017+
_tilePosition = tilePos;
1018+
std::stringstream ss;
1019+
ss << "TickAnimOnTilePos(" << _tilePosition.x << "," << _tilePosition.y << ")";
1020+
_key = ss.str();
1021+
}
1022+
1023+
void TMXTileAnimTask::tickAndScheduleNext(float dt)
1024+
{
1025+
setCurrFrame();
1026+
_layer->getParent()->scheduleOnce(CC_CALLBACK_1(TMXTileAnimTask::tickAndScheduleNext, this), _animation->_frames[_currentFrame]._duration / 1000.0f, _key);
1027+
}
1028+
1029+
void TMXTileAnimTask::start()
1030+
{
1031+
_isRunning = true;
1032+
tickAndScheduleNext(0.0f);
1033+
}
1034+
1035+
void TMXTileAnimTask::stop()
1036+
{
1037+
_isRunning = false;
1038+
_layer->getParent()->unschedule(_key);
1039+
}
1040+
1041+
void TMXTileAnimTask::setCurrFrame()
1042+
{
1043+
_layer->setTileGID(_animation->_frames[_currentFrame]._tileID, _tilePosition);
1044+
_currentFrame = (_currentFrame + 1) % _frameCount;
1045+
}
1046+
1047+
TMXTileAnimTask* TMXTileAnimTask::create(FastTMXLayer* layer, TMXTileAnimInfo* animation, const Vec2& tilePos)
1048+
{
1049+
TMXTileAnimTask* ret = new (std::nothrow) TMXTileAnimTask(layer, animation, tilePos);
1050+
if (ret)
1051+
{
1052+
ret->autorelease();
1053+
return ret;
1054+
}
1055+
CC_SAFE_DELETE(ret);
1056+
return nullptr;
1057+
}
9131058

9141059
NS_CC_END

cocos/2d/CCFastTMXLayer.h

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ NS_CC_BEGIN
3737
class TMXMapInfo;
3838
class TMXLayerInfo;
3939
class TMXTilesetInfo;
40+
class TMXTileAnimManager;
4041
class Texture2D;
4142
class Sprite;
4243

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

279+
/** Map from gid of animated tile to its instance.
280+
*
281+
* @return Map from gid of animated tile to its instance.
282+
*/
283+
const std::unordered_map<uint32_t, std::vector<Vec2>>* getAnimTileCoord() {
284+
return &_animTileCoord;
285+
}
286+
287+
bool hasTileAnimation() const {
288+
return !_animTileCoord.empty();
289+
}
290+
291+
TMXTileAnimManager* getTileAnimManager() const {
292+
return _tileAnimManager;
293+
}
294+
295+
CC_CONSTRUCTOR_ACCESS:
296+
bool initWithTilesetInfo(TMXTilesetInfo* tilesetInfo, TMXLayerInfo* layerInfo, TMXMapInfo* mapInfo);
297+
278298
protected:
279299
virtual void setOpacity(uint8_t opacity) override;
280300

281-
bool initWithTilesetInfo(TMXTilesetInfo *tilesetInfo, TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo);
301+
282302
void updateTiles(const Rect& culledRect);
283303
Vec2 calculateLayerOffset(const Vec2& offset);
284304

@@ -315,9 +335,16 @@ class CC_DLL FastTMXLayer : public Node
315335
TMXTilesetInfo* _tileSet = nullptr;
316336
/** Layer orientation, which is the same as the map orientation */
317337
int _layerOrientation = FAST_TMX_ORIENTATION_ORTHO;
338+
int _staggerAxis = TMXStaggerAxis_Y;
339+
int _staggerIndex = TMXStaggerIndex_Even;
318340
/** properties from the layer. They can be added using Tiled */
319341
ValueMap _properties;
320342

343+
/** map from gid of animated tile to its instance. Also useful for optimization*/
344+
std::unordered_map<uint32_t, std::vector<Vec2>> _animTileCoord;
345+
/** pointer to the tile animation manager of this layer */
346+
TMXTileAnimManager* _tileAnimManager = nullptr;
347+
321348
Texture2D *_texture = nullptr;
322349

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

386+
/** @brief TMXTileAnimTask represents the frame-tick task of an animated tile.
387+
* It is a assistant class for TMXTileAnimTicker.
388+
*/
389+
class CC_DLL TMXTileAnimTask : public Ref
390+
{
391+
public:
392+
TMXTileAnimTask(FastTMXLayer* layer, TMXTileAnimInfo* animation, const Vec2& tilePos);
393+
static TMXTileAnimTask* create(FastTMXLayer* layer, TMXTileAnimInfo* animation, const Vec2& tilePos);
394+
/** start the animation task */
395+
void start();
396+
/** stop the animation task */
397+
void stop();
398+
bool isRunning() const {
399+
return _isRunning;
400+
}
401+
402+
protected:
403+
/** set texture of tile to current frame */
404+
void setCurrFrame();
405+
/** tick to next frame and schedule next tick */
406+
void tickAndScheduleNext(float dt);
407+
408+
bool _isRunning = false;
409+
/** key of schedule task for specific animated tile */
410+
std::string _key;
411+
FastTMXLayer* _layer = nullptr;
412+
/** position of the animated tile */
413+
Vec2 _tilePosition;
414+
/** AnimationInfo on this tile */
415+
TMXTileAnimInfo* _animation = nullptr;
416+
/** Index of the frame that should be drawn currently */
417+
uint32_t _currentFrame = 0;
418+
uint32_t _frameCount = 0;
419+
};
420+
421+
/** @brief TMXTileAnimManager controls all tile animation of a layer.
422+
*/
423+
class CC_DLL TMXTileAnimManager : public Ref
424+
{
425+
public:
426+
static TMXTileAnimManager* create(FastTMXLayer* layer);
427+
explicit TMXTileAnimManager(FastTMXLayer* layer);
428+
429+
/** start all tile animations */
430+
void startAll();
431+
/** stop all tile animations */
432+
void stopAll();
433+
434+
/** get vector of tasks */
435+
const Vector<TMXTileAnimTask*>& getTasks() const {
436+
return _tasks;
437+
}
438+
439+
protected:
440+
bool _started = false;
441+
/** vector contains all tasks of this layer */
442+
Vector<TMXTileAnimTask*> _tasks;
443+
FastTMXLayer* _layer = nullptr;
444+
};
445+
446+
// @API compatible
447+
typedef FastTMXLayer TMXLayer;
448+
359449
// end of tilemap_parallax_nodes group
360450
/// @}
361451
NS_CC_END

cocos/2d/CCFastTMXTiledMap.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ bool FastTMXTiledMap::initWithTMXFile(const std::string& tmxFile)
7272
CCASSERT( !mapInfo->getTilesets().empty(), "FastTMXTiledMap: Map not found. Please check the filename.");
7373
buildWithMapInfo(mapInfo);
7474

75+
_tmxFile = tmxFile;
76+
7577
return true;
7678
}
7779

@@ -192,6 +194,8 @@ void FastTMXTiledMap::buildWithMapInfo(TMXMapInfo* mapInfo)
192194
idx++;
193195
}
194196
}
197+
198+
_layerCount = idx;
195199
}
196200

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

264+
void FastTMXTiledMap::setTileAnimEnabled(bool enabled)
265+
{
266+
for (auto& child : _children)
267+
{
268+
FastTMXLayer* layer = dynamic_cast<FastTMXLayer*>(child);
269+
if (layer)
270+
{
271+
if (layer->hasTileAnimation())
272+
{
273+
if (enabled)
274+
layer->getTileAnimManager()->startAll();
275+
else
276+
layer->getTileAnimManager()->stopAll();
277+
}
278+
}
279+
}
280+
}
281+
260282
NS_CC_END

cocos/2d/CCFastTMXTiledMap.h

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,18 @@ class CC_DLL FastTMXTiledMap : public Node
201201

202202
virtual std::string getDescription() const override;
203203

204-
protected:
204+
/** Set all tile animations enabled or not.
205+
* animations are not enabled by default
206+
*/
207+
void setTileAnimEnabled(bool enabled);
208+
209+
CC_DEPRECATED_ATTRIBUTE int getLayerNum() const { return getLayerCount(); }
210+
211+
int getLayerCount() const { return _layerCount; }
212+
213+
const std::string& getResourceFile() const { return _tmxFile; }
214+
215+
CC_CONSTRUCTOR_ACCESS:
205216
/**
206217
* @js ctor
207218
*/
@@ -218,6 +229,7 @@ class CC_DLL FastTMXTiledMap : public Node
218229
/** initializes a TMX Tiled Map with a TMX formatted XML string and a path to TMX resources */
219230
bool initWithXML(const std::string& tmxString, const std::string& resourcePath);
220231

232+
protected:
221233
FastTMXLayer * parseLayer(TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo);
222234
TMXTilesetInfo * tilesetForLayer(TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo);
223235
void buildWithMapInfo(TMXMapInfo* mapInfo);
@@ -236,13 +248,18 @@ class CC_DLL FastTMXTiledMap : public Node
236248
//! tile properties
237249
ValueMapIntKey _tileProperties;
238250

251+
int _layerCount = 0;
252+
253+
std::string _tmxFile;
239254
private:
240255
CC_DISALLOW_COPY_AND_ASSIGN(FastTMXTiledMap);
241256

242257
};
243258

244259
// end of tilemap_parallax_nodes group
245260
/** @} */
246-
261+
262+
// @API compatible
263+
typedef FastTMXTiledMap TMXTiledMap;
247264

248265
NS_CC_END

0 commit comments

Comments
 (0)