2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2011 Zynga Inc.
|
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
|
|
|
|
|
|
|
Copyright (c) 2011 HKASoftware
|
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
https://axmolengine.github.io/
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
Original rewrite of TMXLayer was based on HKTMXTiledMap by HKASoftware http://hkasoftware.com
|
|
|
|
Further info: http://www.cocos2d-iphone.org/forums/topic/hktmxtiledmap/
|
|
|
|
|
|
|
|
It was rewritten again, and only a small part of the original HK ideas/code remains in this implementation
|
|
|
|
|
|
|
|
*/
|
|
|
|
#include "2d/CCFastTMXLayer.h"
|
2021-12-25 10:04:45 +08:00
|
|
|
#include <stddef.h> // offsetof
|
2019-11-23 20:27:39 +08:00
|
|
|
#include "base/ccTypes.h"
|
|
|
|
#include "2d/CCFastTMXTiledMap.h"
|
|
|
|
#include "2d/CCSprite.h"
|
|
|
|
#include "2d/CCCamera.h"
|
|
|
|
#include "renderer/CCTextureCache.h"
|
|
|
|
#include "renderer/CCRenderer.h"
|
|
|
|
#include "renderer/ccShaders.h"
|
|
|
|
#include "renderer/backend/Device.h"
|
|
|
|
#include "renderer/backend/Buffer.h"
|
|
|
|
#include "base/CCDirector.h"
|
|
|
|
#include "base/ccUTF8.h"
|
|
|
|
#include "renderer/backend/ProgramState.h"
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
const int FastTMXLayer::FAST_TMX_ORIENTATION_ORTHO = 0;
|
2021-12-25 10:04:45 +08:00
|
|
|
const int FastTMXLayer::FAST_TMX_ORIENTATION_HEX = 1;
|
|
|
|
const int FastTMXLayer::FAST_TMX_ORIENTATION_ISO = 2;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// FastTMXLayer - init & alloc & dealloc
|
2021-12-25 10:04:45 +08:00
|
|
|
FastTMXLayer* FastTMXLayer::create(TMXTilesetInfo* tilesetInfo, TMXLayerInfo* layerInfo, TMXMapInfo* mapInfo)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
FastTMXLayer* ret = new FastTMXLayer();
|
2019-11-23 20:27:39 +08:00
|
|
|
if (ret->initWithTilesetInfo(tilesetInfo, layerInfo, mapInfo))
|
|
|
|
{
|
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
|
|
|
}
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_DELETE(ret);
|
2019-11-23 20:27:39 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool FastTMXLayer::initWithTilesetInfo(TMXTilesetInfo* tilesetInfo, TMXLayerInfo* layerInfo, TMXMapInfo* mapInfo)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (tilesetInfo)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-04-22 22:01:47 +08:00
|
|
|
_texture = _director->getTextureCache()->addImage(tilesetInfo->_sourceImage);
|
2019-11-23 20:27:39 +08:00
|
|
|
_texture->retain();
|
|
|
|
}
|
|
|
|
|
|
|
|
// layerInfo
|
2021-12-25 10:04:45 +08:00
|
|
|
_layerName = layerInfo->_name;
|
|
|
|
_layerSize = layerInfo->_layerSize;
|
|
|
|
_tiles = layerInfo->_tiles;
|
2019-11-23 20:27:39 +08:00
|
|
|
_quadsDirty = true;
|
2021-12-25 10:04:45 +08:00
|
|
|
setOpacity(layerInfo->_opacity);
|
2019-11-23 20:27:39 +08:00
|
|
|
setProperties(layerInfo->getProperties());
|
|
|
|
|
|
|
|
// tilesetInfo
|
|
|
|
_tileSet = tilesetInfo;
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RETAIN(_tileSet);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// mapInfo
|
2021-12-25 10:04:45 +08:00
|
|
|
_mapTileSize = mapInfo->getTileSize();
|
2019-11-23 20:27:39 +08:00
|
|
|
_layerOrientation = mapInfo->getOrientation();
|
2021-12-25 10:04:45 +08:00
|
|
|
_staggerAxis = mapInfo->getStaggerAxis();
|
|
|
|
_staggerIndex = mapInfo->getStaggerIndex();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// offset (after layer orientation is set);
|
|
|
|
Vec2 offset = this->calculateLayerOffset(layerInfo->_offset);
|
2022-07-16 10:43:05 +08:00
|
|
|
this->setPosition(AX_POINT_PIXELS_TO_POINTS(offset));
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
this->setContentSize(
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SIZE_PIXELS_TO_POINTS(Vec2(_layerSize.width * _mapTileSize.width, _layerSize.height * _mapTileSize.height)));
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
this->tileToNodeTransform();
|
|
|
|
|
|
|
|
_useAutomaticVertexZ = false;
|
2021-12-25 10:04:45 +08:00
|
|
|
_vertexZvalue = 0;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
FastTMXLayer::FastTMXLayer() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
FastTMXLayer::~FastTMXLayer()
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RELEASE(_tileSet);
|
|
|
|
AX_SAFE_RELEASE(_texture);
|
|
|
|
AX_SAFE_FREE(_tiles);
|
|
|
|
AX_SAFE_RELEASE(_vertexBuffer);
|
|
|
|
AX_SAFE_RELEASE(_indexBuffer);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& e : _customCommands)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RELEASE(e.second->getPipelineDescriptor().programState);
|
2019-11-23 20:27:39 +08:00
|
|
|
delete e.second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void FastTMXLayer::draw(Renderer* renderer, const Mat4& transform, uint32_t flags)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
updateTotalQuads();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
if (flags != 0 || _dirty || _quadsDirty)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Vec2 s = _director->getVisibleSize();
|
|
|
|
const Vec2& anchor = getAnchorPoint();
|
2019-11-23 20:27:39 +08:00
|
|
|
auto rect = Rect(Camera::getVisitingCamera()->getPositionX() - s.width * (anchor.x == 0.0f ? 0.5f : anchor.x),
|
|
|
|
Camera::getVisitingCamera()->getPositionY() - s.height * (anchor.y == 0.0f ? 0.5f : anchor.y),
|
2021-12-25 10:04:45 +08:00
|
|
|
s.width, s.height);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
Mat4 inv = transform;
|
|
|
|
inv.inverse();
|
|
|
|
rect = RectApplyTransform(rect, inv);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
updateTiles(rect);
|
|
|
|
updateIndexBuffer();
|
|
|
|
updatePrimitives();
|
|
|
|
_dirty = false;
|
|
|
|
}
|
|
|
|
|
2021-04-22 22:01:47 +08:00
|
|
|
const auto& projectionMat = _director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
|
2021-12-25 10:04:45 +08:00
|
|
|
Mat4 finalMat = projectionMat * _modelViewTransform;
|
2019-11-23 20:27:39 +08:00
|
|
|
for (const auto& e : _customCommands)
|
|
|
|
{
|
|
|
|
if (e.second->getIndexDrawCount() > 0)
|
|
|
|
{
|
|
|
|
auto mvpmatrixLocation = e.second->getPipelineDescriptor().programState->getUniformLocation("u_MVPMatrix");
|
2021-12-25 10:04:45 +08:00
|
|
|
e.second->getPipelineDescriptor().programState->setUniform(mvpmatrixLocation, finalMat.m,
|
|
|
|
sizeof(finalMat.m));
|
2019-11-23 20:27:39 +08:00
|
|
|
renderer->addCommand(e.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FastTMXLayer::updateTiles(const Rect& culledRect)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Rect visibleTiles = Rect(culledRect.origin, culledRect.size * _director->getContentScaleFactor());
|
2022-07-16 10:43:05 +08:00
|
|
|
Vec2 mapTileSize = AX_SIZE_PIXELS_TO_POINTS(_mapTileSize);
|
|
|
|
Vec2 tileSize = AX_SIZE_PIXELS_TO_POINTS(_tileSet->_tileSize);
|
2019-11-23 20:27:39 +08:00
|
|
|
Mat4 nodeToTileTransform = _tileToNodeTransform.getInversed();
|
2021-12-25 10:04:45 +08:00
|
|
|
// transform to tile
|
2019-11-23 20:27:39 +08:00
|
|
|
visibleTiles = RectApplyTransform(visibleTiles, nodeToTileTransform);
|
|
|
|
// tile coordinate is upside-down, so we need to make the tile coordinate use top-left for the start point.
|
|
|
|
visibleTiles.origin.y += 1;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// if x=0.7, width=9.5, we need to draw number 0~10 of tiles, and so is height.
|
2021-12-25 10:04:45 +08:00
|
|
|
visibleTiles.size.width = ceil(visibleTiles.origin.x + visibleTiles.size.width) - floor(visibleTiles.origin.x);
|
2019-11-23 20:27:39 +08:00
|
|
|
visibleTiles.size.height = ceil(visibleTiles.origin.y + visibleTiles.size.height) - floor(visibleTiles.origin.y);
|
2021-12-25 10:04:45 +08:00
|
|
|
visibleTiles.origin.x = floor(visibleTiles.origin.x);
|
|
|
|
visibleTiles.origin.y = floor(visibleTiles.origin.y);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// for the bigger tiles.
|
|
|
|
int tilesOverX = 0;
|
|
|
|
int tilesOverY = 0;
|
|
|
|
// for diagonal orientation tiles
|
|
|
|
float tileSizeMax = std::max(tileSize.width, tileSize.height);
|
|
|
|
if (_layerOrientation == FAST_TMX_ORIENTATION_ORTHO)
|
|
|
|
{
|
|
|
|
tilesOverX = (int)ceil(tileSizeMax / mapTileSize.width) - 1;
|
|
|
|
tilesOverY = (int)ceil(tileSizeMax / mapTileSize.height) - 1;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
if (tilesOverX < 0)
|
|
|
|
tilesOverX = 0;
|
|
|
|
if (tilesOverY < 0)
|
|
|
|
tilesOverY = 0;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (_layerOrientation == FAST_TMX_ORIENTATION_ISO)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
Rect overTileRect(0, 0, tileSizeMax - mapTileSize.width, tileSizeMax - mapTileSize.height);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (overTileRect.size.width < 0)
|
|
|
|
overTileRect.size.width = 0;
|
|
|
|
if (overTileRect.size.height < 0)
|
|
|
|
overTileRect.size.height = 0;
|
2019-11-23 20:27:39 +08:00
|
|
|
overTileRect = RectApplyTransform(overTileRect, nodeToTileTransform);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
tilesOverX = (int)(ceil(overTileRect.origin.x + overTileRect.size.width) - floor(overTileRect.origin.x));
|
|
|
|
tilesOverY = (int)(ceil(overTileRect.origin.y + overTileRect.size.height) - floor(overTileRect.origin.y));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// do nothing, do not support
|
2022-07-16 10:43:05 +08:00
|
|
|
// AXASSERT(0, "TMX invalid value");
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
_indicesVertexZNumber.clear();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
for (const auto& iter : _indicesVertexZOffsets)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_indicesVertexZNumber[iter.first] = iter.second;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
int yBegin = static_cast<int>(std::max(0.f, visibleTiles.origin.y - tilesOverY));
|
|
|
|
int yEnd =
|
|
|
|
static_cast<int>(std::min(_layerSize.height, visibleTiles.origin.y + visibleTiles.size.height + tilesOverY));
|
|
|
|
int xBegin = static_cast<int>(std::max(0.f, visibleTiles.origin.x - tilesOverX));
|
|
|
|
int xEnd =
|
|
|
|
static_cast<int>(std::min(_layerSize.width, visibleTiles.origin.x + visibleTiles.size.width + tilesOverX));
|
|
|
|
|
|
|
|
for (int y = yBegin; y < yEnd; ++y)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
for (int x = xBegin; x < xEnd; ++x)
|
|
|
|
{
|
|
|
|
int tileIndex = getTileIndexByPos(x, y);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_tiles[tileIndex] == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
int vertexZ = getVertexZForPos(Vec2((float)x, (float)y));
|
|
|
|
auto iter = _indicesVertexZNumber.find(vertexZ);
|
|
|
|
int offset = iter->second;
|
2019-11-23 20:27:39 +08:00
|
|
|
iter->second++;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
unsigned short quadIndex = static_cast<unsigned short>(_tileToQuadIndex[tileIndex]);
|
|
|
|
_indices[6 * offset + 0] = quadIndex * 4 + 0;
|
|
|
|
_indices[6 * offset + 1] = quadIndex * 4 + 1;
|
|
|
|
_indices[6 * offset + 2] = quadIndex * 4 + 2;
|
|
|
|
_indices[6 * offset + 3] = quadIndex * 4 + 3;
|
|
|
|
_indices[6 * offset + 4] = quadIndex * 4 + 2;
|
|
|
|
_indices[6 * offset + 5] = quadIndex * 4 + 1;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
} // for x
|
|
|
|
} // for y
|
|
|
|
|
|
|
|
for (const auto& iter : _indicesVertexZOffsets)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_indicesVertexZNumber[iter.first] -= iter.second;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_indicesVertexZNumber[iter.first] == 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_indicesVertexZNumber.erase(iter.first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FastTMXLayer::updateVertexBuffer()
|
|
|
|
{
|
|
|
|
unsigned int vertexBufferSize = (unsigned int)(sizeof(V3F_C4B_T2F) * _totalQuads.size() * 4);
|
|
|
|
if (!_vertexBuffer)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto device = backend::Device::getInstance();
|
2019-11-23 20:27:39 +08:00
|
|
|
_vertexBuffer = device->newBuffer(vertexBufferSize, backend::BufferType::VERTEX, backend::BufferUsage::STATIC);
|
|
|
|
}
|
|
|
|
_vertexBuffer->updateData(&_totalQuads[0], vertexBufferSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FastTMXLayer::updateIndexBuffer()
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
#ifdef AX_FAST_TILEMAP_32_BIT_INDICES
|
2019-11-23 20:27:39 +08:00
|
|
|
unsigned int indexBufferSize = (unsigned int)(sizeof(unsigned int) * _indices.size());
|
|
|
|
#else
|
|
|
|
unsigned int indexBufferSize = (unsigned int)(sizeof(unsigned short) * _indices.size());
|
|
|
|
#endif
|
|
|
|
if (!_indexBuffer)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto device = backend::Device::getInstance();
|
2019-11-29 17:39:40 +08:00
|
|
|
_indexBuffer = device->newBuffer(indexBufferSize, backend::BufferType::INDEX, backend::BufferUsage::DYNAMIC);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
_indexBuffer->updateData(&_indices[0], indexBufferSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
// FastTMXLayer - setup Tiles
|
|
|
|
void FastTMXLayer::setupTiles()
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
// Optimization: quick hack that sets the image size on the tileset
|
|
|
|
_tileSet->_imageSize = _texture->getContentSizeInPixels();
|
|
|
|
|
|
|
|
// By default all the tiles are aliased
|
|
|
|
// pros: easier to render
|
|
|
|
// cons: difficult to scale / rotate / etc.
|
|
|
|
_texture->setAliasTexParameters();
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// CFByteOrder o = CFByteOrderGetCurrent();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// Parse cocos2d properties
|
|
|
|
this->parseInternalProperties();
|
|
|
|
|
2021-04-22 22:01:47 +08:00
|
|
|
auto& screenSize = _director->getWinSize();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
switch (_layerOrientation)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
case FAST_TMX_ORIENTATION_ORTHO:
|
|
|
|
_screenGridSize.width = ceil(screenSize.width / _mapTileSize.width) + 1;
|
|
|
|
_screenGridSize.height = ceil(screenSize.height / _mapTileSize.height) + 1;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// tiles could be bigger than the grid, add additional rows if needed
|
|
|
|
_screenGridSize.height += _tileSet->_tileSize.height / _mapTileSize.height;
|
|
|
|
break;
|
|
|
|
case FAST_TMX_ORIENTATION_ISO:
|
|
|
|
_screenGridSize.width = ceil(screenSize.width / _mapTileSize.width) + 2;
|
|
|
|
_screenGridSize.height = ceil(screenSize.height / (_mapTileSize.height / 2)) + 4;
|
|
|
|
break;
|
|
|
|
case FAST_TMX_ORIENTATION_HEX:
|
|
|
|
default:
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOGERROR("FastTMX does not support type %d", _layerOrientation);
|
2021-12-25 10:04:45 +08:00
|
|
|
break;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
_screenTileCount = (int)(_screenGridSize.width * _screenGridSize.height);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!_tileSet->_animationInfo.empty())
|
|
|
|
{
|
2020-08-28 16:33:52 +08:00
|
|
|
/// 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;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else
|
|
|
|
{
|
2020-08-28 16:33:52 +08:00
|
|
|
// 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);
|
2023-03-07 23:26:37 +08:00
|
|
|
uint32_t gid = _tiles[pos];
|
|
|
|
uint32_t flags = 0;
|
|
|
|
|
|
|
|
// issue#1098 TileMap flipped/rotated animation bug.
|
|
|
|
flags |= gid & kTMXFlipedAll;
|
|
|
|
gid &= ~kTMXFlipedAll;
|
2020-08-28 16:33:52 +08:00
|
|
|
|
|
|
|
// gid are stored in little endian.
|
|
|
|
// if host is big endian, then swap
|
2021-12-25 10:04:45 +08:00
|
|
|
// if( o == CFByteOrderBigEndian )
|
2020-08-28 16:33:52 +08:00
|
|
|
// gid = CFSwapInt32( gid );
|
|
|
|
/* We support little endian.*/
|
|
|
|
|
|
|
|
// FIXME:: gid == 0 --> empty tile
|
|
|
|
if (gid != 0)
|
|
|
|
{
|
|
|
|
if (_tileSet->_animationInfo.find(gid) != _tileSet->_animationInfo.end())
|
|
|
|
{
|
2023-03-07 23:26:37 +08:00
|
|
|
_animTileCoord[gid].emplace_back(TMXTileAnimFlag{Vec2(newX, y), flags});
|
2020-08-28 16:33:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hasTileAnimation())
|
|
|
|
{
|
|
|
|
_tileAnimManager = new TMXTileAnimManager(this);
|
|
|
|
}
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Mat4 FastTMXLayer::tileToNodeTransform()
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
float w = _mapTileSize.width / AX_CONTENT_SCALE_FACTOR();
|
|
|
|
float h = _mapTileSize.height / AX_CONTENT_SCALE_FACTOR();
|
2019-11-23 20:27:39 +08:00
|
|
|
float offY = (_layerSize.height - 1) * h;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
switch (_layerOrientation)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
case FAST_TMX_ORIENTATION_ORTHO:
|
|
|
|
{
|
|
|
|
_tileToNodeTransform =
|
|
|
|
Mat4(w, 0.0f, 0.0f, 0.0f, 0.0f, -h, 0.0f, offY, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0, 0.0f, 1.0f);
|
|
|
|
|
|
|
|
return _tileToNodeTransform;
|
|
|
|
}
|
|
|
|
case FAST_TMX_ORIENTATION_ISO:
|
|
|
|
{
|
|
|
|
float offX = (_layerSize.width - 1) * w / 2;
|
|
|
|
_tileToNodeTransform =
|
|
|
|
Mat4(w / 2, -w / 2, 0.0f, offX, -h / 2, -h / 2, 0.0f, offY, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f);
|
|
|
|
return _tileToNodeTransform;
|
|
|
|
}
|
|
|
|
case FAST_TMX_ORIENTATION_HEX:
|
|
|
|
{
|
|
|
|
_tileToNodeTransform = Mat4(h * sqrtf(0.75), 0, 0, 0, -h / 2, -h, 0, offY, 0, 0, 1, 0, 0, 0, 0, 1);
|
|
|
|
return _tileToNodeTransform;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
_tileToNodeTransform = Mat4::IDENTITY;
|
|
|
|
return _tileToNodeTransform;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FastTMXLayer::updatePrimitives()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto blendfunc =
|
|
|
|
_texture->hasPremultipliedAlpha() ? BlendFunc::ALPHA_PREMULTIPLIED : BlendFunc::ALPHA_NON_PREMULTIPLIED;
|
|
|
|
for (const auto& iter : _indicesVertexZNumber)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
int start = _indicesVertexZOffsets.at(iter.first);
|
|
|
|
|
|
|
|
auto commandIter = _customCommands.find(iter.first);
|
|
|
|
if (_customCommands.end() == commandIter)
|
|
|
|
{
|
|
|
|
auto command = new CustomCommand();
|
|
|
|
command->setVertexBuffer(_vertexBuffer);
|
|
|
|
|
|
|
|
CustomCommand::IndexFormat indexFormat = CustomCommand::IndexFormat::U_SHORT;
|
2022-07-16 10:43:05 +08:00
|
|
|
#if AX_FAST_TILEMAP_32_BIT_INDICES
|
2019-11-23 20:27:39 +08:00
|
|
|
indexFormat = CustomCommand::IndexFormat::U_INT;
|
|
|
|
#endif
|
|
|
|
command->setIndexBuffer(_indexBuffer, indexFormat);
|
|
|
|
|
|
|
|
command->setIndexDrawInfo(start * 6, iter.second * 6);
|
|
|
|
|
|
|
|
auto& pipelineDescriptor = command->getPipelineDescriptor();
|
|
|
|
|
|
|
|
if (_useAutomaticVertexZ)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RELEASE(pipelineDescriptor.programState);
|
2021-12-25 10:04:45 +08:00
|
|
|
auto* program =
|
|
|
|
backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_TEXTURE_COLOR_ALPHA_TEST);
|
|
|
|
auto programState = new backend::ProgramState(program);
|
2019-11-23 20:27:39 +08:00
|
|
|
pipelineDescriptor.programState = programState;
|
2021-12-25 10:04:45 +08:00
|
|
|
_alphaValueLocation = pipelineDescriptor.programState->getUniformLocation("u_alpha_value");
|
|
|
|
pipelineDescriptor.programState->setUniform(_alphaValueLocation, &_alphaFuncValue,
|
|
|
|
sizeof(_alphaFuncValue));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RELEASE(pipelineDescriptor.programState);
|
2021-12-25 10:04:45 +08:00
|
|
|
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_TEXTURE_COLOR);
|
2021-12-08 00:11:53 +08:00
|
|
|
auto programState = new backend::ProgramState(program);
|
2019-11-23 20:27:39 +08:00
|
|
|
pipelineDescriptor.programState = programState;
|
|
|
|
}
|
2022-09-23 22:41:30 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
_mvpMatrixLocaiton = pipelineDescriptor.programState->getUniformLocation("u_MVPMatrix");
|
2022-07-05 00:39:20 +08:00
|
|
|
_textureLocation = pipelineDescriptor.programState->getUniformLocation("u_tex0");
|
2019-11-23 20:27:39 +08:00
|
|
|
pipelineDescriptor.programState->setTexture(_textureLocation, 0, _texture->getBackendTexture());
|
|
|
|
command->init(_globalZOrder, blendfunc);
|
|
|
|
|
|
|
|
_customCommands[iter.first] = command;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
commandIter->second->setIndexDrawInfo(start * 6, iter.second * 6);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void FastTMXLayer::setOpacity(uint8_t opacity)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
Node::setOpacity(opacity);
|
|
|
|
_quadsDirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FastTMXLayer::updateTotalQuads()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_quadsDirty)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
Vec2 tileSize = AX_SIZE_PIXELS_TO_POINTS(_tileSet->_tileSize);
|
2021-12-25 10:04:45 +08:00
|
|
|
Vec2 texSize = _tileSet->_imageSize;
|
2019-11-23 20:27:39 +08:00
|
|
|
_tileToQuadIndex.clear();
|
|
|
|
_totalQuads.resize(int(_layerSize.width * _layerSize.height));
|
|
|
|
_indices.resize(6 * int(_layerSize.width * _layerSize.height));
|
2021-12-25 10:04:45 +08:00
|
|
|
_tileToQuadIndex.resize(int(_layerSize.width * _layerSize.height), -1);
|
2019-11-23 20:27:39 +08:00
|
|
|
_indicesVertexZOffsets.clear();
|
|
|
|
|
|
|
|
auto color = Color4B::WHITE;
|
2021-12-25 10:04:45 +08:00
|
|
|
color.a = getDisplayedOpacity();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_texture->hasPremultipliedAlpha())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto alpha = color.a / 255.0f;
|
2021-12-25 10:04:45 +08:00
|
|
|
color.r = static_cast<uint8_t>(color.r * alpha);
|
|
|
|
color.g = static_cast<uint8_t>(color.g * alpha);
|
|
|
|
color.b = static_cast<uint8_t>(color.b * alpha);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int quadIndex = 0;
|
2021-12-25 10:04:45 +08:00
|
|
|
for (int y = 0; y < _layerSize.height; ++y)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
for (int x = 0; x < _layerSize.width; ++x)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
int tileIndex = getTileIndexByPos(x, y);
|
2021-12-25 10:04:45 +08:00
|
|
|
int tileGID = _tiles[tileIndex];
|
|
|
|
|
|
|
|
if (tileGID == 0)
|
|
|
|
continue;
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
_tileToQuadIndex[tileIndex] = quadIndex;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto& quad = _totalQuads[quadIndex];
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Vec3 nodePos(float(x), float(y), 0);
|
|
|
|
_tileToNodeTransform.transformPoint(&nodePos);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
float left, right, top, bottom, z;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
int zPos = getVertexZForPos(Vec2((float)x, (float)y));
|
|
|
|
z = (float)zPos;
|
2019-11-23 20:27:39 +08:00
|
|
|
auto iter = _indicesVertexZOffsets.find(zPos);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (iter == _indicesVertexZOffsets.end())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_indicesVertexZOffsets[zPos] = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
iter->second++;
|
|
|
|
}
|
|
|
|
// vertices
|
|
|
|
if (tileGID & kTMXTileDiagonalFlag)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
left = nodePos.x;
|
|
|
|
right = nodePos.x + tileSize.height;
|
2019-11-23 20:27:39 +08:00
|
|
|
bottom = nodePos.y + tileSize.width;
|
2021-12-25 10:04:45 +08:00
|
|
|
top = nodePos.y;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
left = nodePos.x;
|
|
|
|
right = nodePos.x + tileSize.width;
|
2019-11-23 20:27:39 +08:00
|
|
|
bottom = nodePos.y + tileSize.height;
|
2021-12-25 10:04:45 +08:00
|
|
|
top = nodePos.y;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
if (tileGID & kTMXTileVerticalFlag)
|
2019-11-23 20:27:39 +08:00
|
|
|
std::swap(top, bottom);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (tileGID & kTMXTileHorizontalFlag)
|
2019-11-23 20:27:39 +08:00
|
|
|
std::swap(left, right);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
if (tileGID & kTMXTileDiagonalFlag)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
// FIXME: not working correctly
|
|
|
|
quad.bl.vertices.x = left;
|
|
|
|
quad.bl.vertices.y = bottom;
|
|
|
|
quad.bl.vertices.z = z;
|
|
|
|
quad.br.vertices.x = left;
|
|
|
|
quad.br.vertices.y = top;
|
|
|
|
quad.br.vertices.z = z;
|
|
|
|
quad.tl.vertices.x = right;
|
|
|
|
quad.tl.vertices.y = bottom;
|
|
|
|
quad.tl.vertices.z = z;
|
|
|
|
quad.tr.vertices.x = right;
|
|
|
|
quad.tr.vertices.y = top;
|
|
|
|
quad.tr.vertices.z = z;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
quad.bl.vertices.x = left;
|
|
|
|
quad.bl.vertices.y = bottom;
|
|
|
|
quad.bl.vertices.z = z;
|
|
|
|
quad.br.vertices.x = right;
|
|
|
|
quad.br.vertices.y = bottom;
|
|
|
|
quad.br.vertices.z = z;
|
|
|
|
quad.tl.vertices.x = left;
|
|
|
|
quad.tl.vertices.y = top;
|
|
|
|
quad.tl.vertices.z = z;
|
|
|
|
quad.tr.vertices.x = right;
|
|
|
|
quad.tr.vertices.y = top;
|
|
|
|
quad.tr.vertices.z = z;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// texcoords
|
|
|
|
Rect tileTexture = _tileSet->getRectForGID(tileGID);
|
2021-12-25 10:04:45 +08:00
|
|
|
left = (tileTexture.origin.x / texSize.width);
|
|
|
|
right = left + (tileTexture.size.width / texSize.width);
|
|
|
|
bottom = (tileTexture.origin.y / texSize.height);
|
|
|
|
top = bottom + (tileTexture.size.height / texSize.height);
|
|
|
|
|
2023-03-06 22:41:16 +08:00
|
|
|
// issue#1085 OpenGL sub-pixel horizontal-vertical lines pixel-tolerance fix.
|
|
|
|
float ptx = 1.0 / (_tileSet->_imageSize.x * tileSize.x);
|
|
|
|
float pty = 1.0 / (_tileSet->_imageSize.y * tileSize.y);
|
|
|
|
|
|
|
|
quad.bl.texCoords.u = left + ptx;
|
|
|
|
quad.bl.texCoords.v = bottom + pty;
|
|
|
|
quad.br.texCoords.u = right - ptx;
|
|
|
|
quad.br.texCoords.v = bottom + pty;
|
|
|
|
quad.tl.texCoords.u = left + ptx;
|
|
|
|
quad.tl.texCoords.v = top - pty;
|
|
|
|
quad.tr.texCoords.u = right - ptx;
|
|
|
|
quad.tr.texCoords.v = top - pty;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
quad.bl.colors = color;
|
|
|
|
quad.br.colors = color;
|
|
|
|
quad.tl.colors = color;
|
|
|
|
quad.tr.colors = color;
|
|
|
|
|
|
|
|
++quadIndex;
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
int offset = 0;
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& vertexZOffset : _indicesVertexZOffsets)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
std::swap(offset, vertexZOffset.second);
|
|
|
|
offset += vertexZOffset.second;
|
|
|
|
}
|
|
|
|
updateVertexBuffer();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
_quadsDirty = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// removing / getting tiles
|
|
|
|
Sprite* FastTMXLayer::getTileAt(const Vec2& tileCoordinate)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(tileCoordinate.x < _layerSize.width && tileCoordinate.y < _layerSize.height && tileCoordinate.x >= 0 &&
|
2021-12-25 10:04:45 +08:00
|
|
|
tileCoordinate.y >= 0,
|
|
|
|
"TMXLayer: invalid position");
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(_tiles, "TMXLayer: the tiles map has been released");
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2023-03-12 16:15:11 +08:00
|
|
|
TMXTileFlags flags;
|
2021-12-25 10:04:45 +08:00
|
|
|
Sprite* tile = nullptr;
|
2023-03-12 16:15:11 +08:00
|
|
|
int gid = this->getTileGIDAt(tileCoordinate, &flags);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// if GID == 0, then no tile is present
|
2021-12-25 10:04:45 +08:00
|
|
|
if (gid)
|
|
|
|
{
|
|
|
|
int index = (int)tileCoordinate.x + (int)(tileCoordinate.y * _layerSize.width);
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto it = _spriteContainer.find(index);
|
|
|
|
if (it != _spriteContainer.end())
|
|
|
|
{
|
|
|
|
tile = it->second.first;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// tile not created yet. create it
|
|
|
|
Rect rect = _tileSet->getRectForGID(gid);
|
2022-07-16 10:43:05 +08:00
|
|
|
rect = AX_RECT_PIXELS_TO_POINTS(rect);
|
2021-12-25 10:04:45 +08:00
|
|
|
tile = Sprite::createWithTexture(_texture, rect);
|
|
|
|
|
2023-03-13 08:55:26 +08:00
|
|
|
setupTileSprite(tile, tileCoordinate, gid | flags);
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
tile->setTag(index);
|
|
|
|
this->addChild(tile, index);
|
2023-03-13 08:55:26 +08:00
|
|
|
|
2023-03-12 16:15:11 +08:00
|
|
|
_spriteContainer.insert(
|
|
|
|
std::pair<int, std::pair<Sprite*, uint32_t>>(index, std::pair<Sprite*, uint32_t>(tile, gid | flags)));
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// tile is converted to sprite.
|
|
|
|
setFlaggedTileGIDByIndex(index, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tile;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
int FastTMXLayer::getTileGIDAt(const Vec2& tileCoordinate, TMXTileFlags* flags /* = nullptr*/)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(tileCoordinate.x < _layerSize.width && tileCoordinate.y < _layerSize.height && tileCoordinate.x >= 0 &&
|
2021-12-25 10:04:45 +08:00
|
|
|
tileCoordinate.y >= 0,
|
|
|
|
"TMXLayer: invalid position");
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(_tiles, "TMXLayer: the tiles map has been released");
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
int idx = static_cast<int>(((int)tileCoordinate.x + (int)tileCoordinate.y * _layerSize.width));
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Bits on the far end of the 32-bit global tile ID are used for tile flags
|
|
|
|
int tile = _tiles[idx];
|
2021-12-25 10:04:45 +08:00
|
|
|
auto it = _spriteContainer.find(idx);
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// converted to sprite.
|
|
|
|
if (tile == 0 && it != _spriteContainer.end())
|
|
|
|
{
|
|
|
|
tile = it->second.second;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// issue1264, flipped tiles can be changed dynamically
|
|
|
|
if (flags)
|
|
|
|
{
|
|
|
|
*flags = (TMXTileFlags)(tile & kTMXFlipedAll);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return (tile & kTMXFlippedMask);
|
|
|
|
}
|
|
|
|
|
|
|
|
Vec2 FastTMXLayer::getPositionAt(const Vec2& pos)
|
|
|
|
{
|
|
|
|
return PointApplyTransform(pos, _tileToNodeTransform);
|
|
|
|
}
|
|
|
|
|
|
|
|
int FastTMXLayer::getVertexZForPos(const Vec2& pos)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
int ret = 0;
|
2019-11-23 20:27:39 +08:00
|
|
|
int maxVal = 0;
|
|
|
|
if (_useAutomaticVertexZ)
|
|
|
|
{
|
|
|
|
switch (_layerOrientation)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
case FAST_TMX_ORIENTATION_ISO:
|
|
|
|
maxVal = static_cast<int>(_layerSize.width + _layerSize.height);
|
|
|
|
ret = static_cast<int>(-(maxVal - (pos.x + pos.y)));
|
|
|
|
break;
|
|
|
|
case FAST_TMX_ORIENTATION_ORTHO:
|
|
|
|
ret = static_cast<int>(-(_layerSize.height - pos.y));
|
|
|
|
break;
|
|
|
|
case FAST_TMX_ORIENTATION_HEX:
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(0, "TMX Hexa vertexZ not supported");
|
2021-12-25 10:04:45 +08:00
|
|
|
break;
|
|
|
|
default:
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(0, "TMX invalid value");
|
2021-12-25 10:04:45 +08:00
|
|
|
break;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = _vertexZvalue;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FastTMXLayer::removeTileAt(const Vec2& tileCoordinate)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(tileCoordinate.x < _layerSize.width && tileCoordinate.y < _layerSize.height && tileCoordinate.x >= 0 &&
|
2021-12-25 10:04:45 +08:00
|
|
|
tileCoordinate.y >= 0,
|
|
|
|
"TMXLayer: invalid position");
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
int gid = this->getTileGIDAt(tileCoordinate);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
if (gid)
|
|
|
|
{
|
|
|
|
|
|
|
|
int z = (int)tileCoordinate.x + (int)(tileCoordinate.y * _layerSize.width);
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// remove tile from GID map
|
|
|
|
setFlaggedTileGIDByIndex(z, 0);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// remove it from sprites
|
|
|
|
auto it = _spriteContainer.find(z);
|
|
|
|
if (it != _spriteContainer.end())
|
|
|
|
{
|
|
|
|
this->removeChild(it->second.first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FastTMXLayer::setFlaggedTileGIDByIndex(int index, uint32_t gid)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (gid == _tiles[index])
|
|
|
|
return;
|
2019-11-23 20:27:39 +08:00
|
|
|
_tiles[index] = gid;
|
2021-12-25 10:04:45 +08:00
|
|
|
_quadsDirty = true;
|
|
|
|
_dirty = true;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FastTMXLayer::removeChild(Node* node, bool cleanup)
|
|
|
|
{
|
|
|
|
int tag = node->getTag();
|
|
|
|
auto it = _spriteContainer.find(tag);
|
|
|
|
if (it != _spriteContainer.end() && it->second.first == node)
|
|
|
|
{
|
|
|
|
_spriteContainer.erase(it);
|
|
|
|
}
|
|
|
|
Node::removeChild(node, cleanup);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TMXLayer - Properties
|
2021-12-31 12:12:40 +08:00
|
|
|
Value FastTMXLayer::getProperty(std::string_view propertyName) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto propItr = _properties.find(propertyName);
|
|
|
|
if (propItr != _properties.end())
|
|
|
|
return propItr->second;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return Value();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FastTMXLayer::parseInternalProperties()
|
|
|
|
{
|
|
|
|
auto vertexz = getProperty("cc_vertexz");
|
2021-12-25 10:04:45 +08:00
|
|
|
if (vertexz.isNull())
|
|
|
|
return;
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string vertexZStr = vertexz.asString();
|
|
|
|
// If "automatic" is on, then parse the "cc_alpha_func" too
|
|
|
|
if (vertexZStr == "automatic")
|
|
|
|
{
|
|
|
|
_useAutomaticVertexZ = true;
|
2021-12-25 10:04:45 +08:00
|
|
|
auto alphaFuncVal = getProperty("cc_alpha_func");
|
|
|
|
_alphaFuncValue = alphaFuncVal.asFloat();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-08-18 11:33:18 +08:00
|
|
|
_vertexZvalue = vertexz.asInt();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// CCTMXLayer2 - obtaining positions, offset
|
2019-11-23 20:27:39 +08:00
|
|
|
Vec2 FastTMXLayer::calculateLayerOffset(const Vec2& pos)
|
|
|
|
{
|
|
|
|
Vec2 ret;
|
2021-12-25 10:04:45 +08:00
|
|
|
switch (_layerOrientation)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
case FAST_TMX_ORIENTATION_ORTHO:
|
2021-12-25 10:04:45 +08:00
|
|
|
ret.set(pos.x * _mapTileSize.width, -pos.y * _mapTileSize.height);
|
2019-11-23 20:27:39 +08:00
|
|
|
break;
|
|
|
|
case FAST_TMX_ORIENTATION_ISO:
|
2021-12-25 10:04:45 +08:00
|
|
|
ret.set((_mapTileSize.width / 2) * (pos.x - pos.y), (_mapTileSize.height / 2) * (-pos.x - pos.y));
|
2019-11-23 20:27:39 +08:00
|
|
|
break;
|
|
|
|
case FAST_TMX_ORIENTATION_HEX:
|
|
|
|
default:
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(pos.isZero(), "offset for this map not implemented yet");
|
2019-11-23 20:27:39 +08:00
|
|
|
break;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
return ret;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// TMXLayer - adding / remove tiles
|
|
|
|
void FastTMXLayer::setTileGID(int gid, const Vec2& tileCoordinate)
|
|
|
|
{
|
|
|
|
setTileGID(gid, tileCoordinate, (TMXTileFlags)0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FastTMXLayer::setTileGID(int gid, const Vec2& tileCoordinate, TMXTileFlags flags)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(tileCoordinate.x < _layerSize.width && tileCoordinate.y < _layerSize.height && tileCoordinate.x >= 0 &&
|
2021-12-25 10:04:45 +08:00
|
|
|
tileCoordinate.y >= 0,
|
|
|
|
"TMXLayer: invalid position");
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(_tiles, "TMXLayer: the tiles map has been released");
|
|
|
|
AXASSERT(gid == 0 || gid >= _tileSet->_firstGid, "TMXLayer: invalid gid");
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
TMXTileFlags currentFlags;
|
|
|
|
int currentGID = getTileGIDAt(tileCoordinate, ¤tFlags);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
if (currentGID == gid && currentFlags == flags)
|
|
|
|
return;
|
|
|
|
|
2023-03-12 16:15:11 +08:00
|
|
|
const uint32_t gidAndFlags = gid | flags;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// setting gid=0 is equal to remove the tile
|
|
|
|
if (gid == 0)
|
|
|
|
{
|
|
|
|
removeTileAt(tileCoordinate);
|
|
|
|
}
|
|
|
|
// empty tile. create a new one
|
|
|
|
else if (currentGID == 0)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
int z = (int)tileCoordinate.x + (int)(tileCoordinate.y * _layerSize.width);
|
2019-11-23 20:27:39 +08:00
|
|
|
setFlaggedTileGIDByIndex(z, gidAndFlags);
|
|
|
|
}
|
|
|
|
// modifying an existing tile with a non-empty tile
|
|
|
|
else
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
int z = (int)tileCoordinate.x + (int)(tileCoordinate.y * _layerSize.width);
|
2019-11-23 20:27:39 +08:00
|
|
|
auto it = _spriteContainer.find(z);
|
|
|
|
if (it != _spriteContainer.end())
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Sprite* sprite = it->second.first;
|
|
|
|
Rect rect = _tileSet->getRectForGID(gid);
|
2022-07-16 10:43:05 +08:00
|
|
|
rect = AX_RECT_PIXELS_TO_POINTS(rect);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
sprite->setTextureRect(rect, false, rect.size);
|
|
|
|
this->reorderChild(sprite, z);
|
|
|
|
if (flags)
|
|
|
|
{
|
2023-03-12 16:15:11 +08:00
|
|
|
setupTileSprite(sprite, tileCoordinate, gidAndFlags);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
it->second.second = gidAndFlags;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
setFlaggedTileGIDByIndex(z, gidAndFlags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FastTMXLayer::setupTileSprite(Sprite* sprite, const Vec2& pos, uint32_t gid)
|
|
|
|
{
|
|
|
|
sprite->setPosition(getPositionAt(pos));
|
|
|
|
sprite->setPositionZ((float)getVertexZForPos(pos));
|
|
|
|
sprite->setAnchorPoint(Vec2::ZERO);
|
|
|
|
sprite->setOpacity(this->getOpacity());
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
// issue 1264, flip can be undone as well
|
2019-11-23 20:27:39 +08:00
|
|
|
sprite->setFlippedX(false);
|
|
|
|
sprite->setFlippedY(false);
|
|
|
|
sprite->setRotation(0.0f);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
// Rotation in tiled is achieved using 3 flipped states, flipping across the horizontal, vertical, and diagonal axes
|
|
|
|
// of the tiles.
|
2019-11-23 20:27:39 +08:00
|
|
|
if (gid & kTMXTileDiagonalFlag)
|
|
|
|
{
|
|
|
|
// put the anchor in the middle for ease of rotation.
|
2021-12-25 10:04:45 +08:00
|
|
|
sprite->setAnchorPoint(Vec2(0.5f, 0.5f));
|
|
|
|
sprite->setPosition(getPositionAt(pos).x + sprite->getContentSize().height / 2,
|
|
|
|
getPositionAt(pos).y + sprite->getContentSize().width / 2);
|
|
|
|
|
|
|
|
uint32_t flag = gid & (kTMXTileHorizontalFlag | kTMXTileVerticalFlag);
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// handle the 4 diagonally flipped states.
|
|
|
|
if (flag == kTMXTileHorizontalFlag)
|
|
|
|
{
|
|
|
|
sprite->setRotation(90.0f);
|
|
|
|
}
|
|
|
|
else if (flag == kTMXTileVerticalFlag)
|
|
|
|
{
|
|
|
|
sprite->setRotation(270.0f);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (flag == (kTMXTileVerticalFlag | kTMXTileHorizontalFlag))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
sprite->setRotation(90.0f);
|
|
|
|
sprite->setFlippedX(true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sprite->setRotation(270.0f);
|
|
|
|
sprite->setFlippedX(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (gid & kTMXTileHorizontalFlag)
|
|
|
|
{
|
|
|
|
sprite->setFlippedX(true);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (gid & kTMXTileVerticalFlag)
|
|
|
|
{
|
|
|
|
sprite->setFlippedY(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string FastTMXLayer::getDescription() const
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
return StringUtils::format("<FastTMXLayer | tag = %d, size = %d,%d>", _tag, (int)_mapTileSize.width,
|
|
|
|
(int)_mapTileSize.height);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2020-08-28 16:33:52 +08:00
|
|
|
TMXTileAnimManager::TMXTileAnimManager(FastTMXLayer* layer)
|
|
|
|
{
|
|
|
|
_layer = layer;
|
|
|
|
for (const auto& p : *_layer->getAnimTileCoord())
|
|
|
|
{
|
2023-03-07 23:26:37 +08:00
|
|
|
for (auto&& tile : p.second)
|
2020-08-28 16:33:52 +08:00
|
|
|
{
|
2023-03-07 23:26:37 +08:00
|
|
|
_tasks.pushBack(TMXTileAnimTask::create(_layer, _layer->getTileSet()->_animationInfo.at(p.first), tile._tilePos, tile._flag));
|
2020-08-28 16:33:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TMXTileAnimManager* TMXTileAnimManager::create(FastTMXLayer* layer)
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
TMXTileAnimManager* ret = new TMXTileAnimManager(layer);
|
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
2020-08-28 16:33:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TMXTileAnimManager::startAll()
|
|
|
|
{
|
|
|
|
if (_started || _tasks.empty())
|
|
|
|
return;
|
|
|
|
_started = true;
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& task : _tasks)
|
2020-08-28 16:33:52 +08:00
|
|
|
{
|
|
|
|
task->start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TMXTileAnimManager::stopAll()
|
|
|
|
{
|
|
|
|
if (!_started)
|
|
|
|
return;
|
|
|
|
_started = false;
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& task : _tasks)
|
2020-08-28 16:33:52 +08:00
|
|
|
{
|
|
|
|
task->stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-07 23:26:37 +08:00
|
|
|
TMXTileAnimTask::TMXTileAnimTask(FastTMXLayer* layer, TMXTileAnimInfo* animation, const Vec2& tilePos, uint32_t flag)
|
2020-08-28 16:33:52 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_layer = layer;
|
|
|
|
_animation = animation;
|
|
|
|
_frameCount = static_cast<uint32_t>(_animation->_frames.size());
|
2020-08-28 16:33:52 +08:00
|
|
|
_tilePosition = tilePos;
|
2023-03-07 23:26:37 +08:00
|
|
|
_flag = flag;
|
2020-08-28 16:33:52 +08:00
|
|
|
std::stringstream ss;
|
|
|
|
ss << "TickAnimOnTilePos(" << _tilePosition.x << "," << _tilePosition.y << ")";
|
|
|
|
_key = ss.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TMXTileAnimTask::tickAndScheduleNext(float dt)
|
|
|
|
{
|
|
|
|
setCurrFrame();
|
2022-07-16 10:43:05 +08:00
|
|
|
_layer->getParent()->scheduleOnce(AX_CALLBACK_1(TMXTileAnimTask::tickAndScheduleNext, this),
|
2021-12-25 10:04:45 +08:00
|
|
|
_animation->_frames[_currentFrame]._duration / 1000.0f, _key);
|
2020-08-28 16:33:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TMXTileAnimTask::start()
|
|
|
|
{
|
|
|
|
_isRunning = true;
|
|
|
|
tickAndScheduleNext(0.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TMXTileAnimTask::stop()
|
|
|
|
{
|
|
|
|
_isRunning = false;
|
|
|
|
_layer->getParent()->unschedule(_key);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TMXTileAnimTask::setCurrFrame()
|
|
|
|
{
|
2023-03-07 23:26:37 +08:00
|
|
|
_layer->setTileGID(_animation->_frames[_currentFrame]._tileID, _tilePosition, (TMXTileFlags)_flag);
|
2020-08-28 16:33:52 +08:00
|
|
|
_currentFrame = (_currentFrame + 1) % _frameCount;
|
|
|
|
}
|
|
|
|
|
2023-03-07 23:26:37 +08:00
|
|
|
TMXTileAnimTask* TMXTileAnimTask::create(FastTMXLayer* layer, TMXTileAnimInfo* animation, const Vec2& tilePos, uint32_t flag)
|
2020-08-28 16:33:52 +08:00
|
|
|
{
|
2023-03-07 23:26:37 +08:00
|
|
|
TMXTileAnimTask* ret = new TMXTileAnimTask(layer, animation, tilePos, flag);
|
2021-12-08 00:11:53 +08:00
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
2020-08-28 16:33:52 +08:00
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|