2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
2012-09-24 21:22:20 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2012-04-19 14:35:52 +08:00
|
|
|
Copyright (c) 2011 Zynga Inc.
|
2017-02-14 14:36:57 +08:00
|
|
|
Copyright (c) 2013-2017 Chukong Technologies Inc.
|
2012-04-19 14:35:52 +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.
|
|
|
|
****************************************************************************/
|
2014-05-17 05:36:00 +08:00
|
|
|
|
2014-05-10 09:39:25 +08:00
|
|
|
#include "2d/CCTMXLayer.h"
|
|
|
|
#include "2d/CCTMXTiledMap.h"
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "2d/CCSprite.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "base/CCDirector.h"
|
2016-06-15 15:01:26 +08:00
|
|
|
#include "base/ccUTF8.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "renderer/CCTextureCache.h"
|
2014-08-29 15:39:52 +08:00
|
|
|
#include "renderer/CCGLProgram.h"
|
2014-04-09 22:30:49 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// TMXLayer - init & alloc & dealloc
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
TMXLayer * TMXLayer::create(TMXTilesetInfo *tilesetInfo, TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
TMXLayer *ret = new (std::nothrow) TMXLayer();
|
2013-12-18 17:47:20 +08:00
|
|
|
if (ret->initWithTilesetInfo(tilesetInfo, layerInfo, mapInfo))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2015-10-19 18:47:36 +08:00
|
|
|
CC_SAFE_DELETE(ret);
|
2013-12-18 17:47:20 +08:00
|
|
|
return nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-06-20 14:13:12 +08:00
|
|
|
bool TMXLayer::initWithTilesetInfo(TMXTilesetInfo *tilesetInfo, TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-08-30 03:54:24 +08:00
|
|
|
// FIXME:: is 35% a good estimate ?
|
2013-06-20 14:13:12 +08:00
|
|
|
Size size = layerInfo->_layerSize;
|
2012-04-19 14:35:52 +08:00
|
|
|
float totalNumberOfTiles = size.width * size.height;
|
|
|
|
float capacity = totalNumberOfTiles * 0.35f + 1; // 35 percent is occupied ?
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
Texture2D *texture = nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
if( tilesetInfo )
|
|
|
|
{
|
2016-02-03 23:12:37 +08:00
|
|
|
texture = Director::getInstance()->getTextureCache()->addImage(tilesetInfo->_sourceImage);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2015-08-19 14:02:12 +08:00
|
|
|
if (nullptr == texture)
|
|
|
|
return false;
|
|
|
|
|
2013-12-20 09:50:53 +08:00
|
|
|
if (SpriteBatchNode::initWithTexture(texture, static_cast<ssize_t>(capacity)))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// layerInfo
|
2013-06-15 14:03:30 +08:00
|
|
|
_layerName = layerInfo->_name;
|
|
|
|
_layerSize = size;
|
|
|
|
_tiles = layerInfo->_tiles;
|
|
|
|
_opacity = layerInfo->_opacity;
|
2013-12-03 14:47:35 +08:00
|
|
|
setProperties(layerInfo->getProperties());
|
2013-07-12 06:24:23 +08:00
|
|
|
_contentScaleFactor = Director::getInstance()->getContentScaleFactor();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// tilesetInfo
|
2013-06-15 14:03:30 +08:00
|
|
|
_tileSet = tilesetInfo;
|
|
|
|
CC_SAFE_RETAIN(_tileSet);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// mapInfo
|
2013-06-15 14:03:30 +08:00
|
|
|
_mapTileSize = mapInfo->getTileSize();
|
|
|
|
_layerOrientation = mapInfo->getOrientation();
|
2016-07-02 06:51:00 +08:00
|
|
|
_staggerAxis = mapInfo->getStaggerAxis();
|
|
|
|
_staggerIndex = mapInfo->getStaggerIndex();
|
|
|
|
_hexSideLength = mapInfo->getHexSideLength();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// offset (after layer orientation is set);
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 offset = this->calculateLayerOffset(layerInfo->_offset);
|
2012-04-19 14:35:52 +08:00
|
|
|
this->setPosition(CC_POINT_PIXELS_TO_POINTS(offset));
|
|
|
|
|
2013-12-20 09:50:53 +08:00
|
|
|
_atlasIndexArray = ccCArrayNew(totalNumberOfTiles);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2016-08-19 16:28:47 +08:00
|
|
|
float width = 0;
|
|
|
|
float height = 0;
|
|
|
|
if (_layerOrientation == TMXOrientationHex) {
|
|
|
|
if (_staggerAxis == TMXStaggerAxis_X) {
|
|
|
|
height = _mapTileSize.height * (_layerSize.height + 0.5);
|
|
|
|
width = (_mapTileSize.width + _hexSideLength) * ((int)(_layerSize.width / 2)) + _mapTileSize.width * ((int)_layerSize.width % 2);
|
|
|
|
} else {
|
|
|
|
width = _mapTileSize.width * (_layerSize.width + 0.5);
|
|
|
|
height = (_mapTileSize.height + _hexSideLength) * ((int)(_layerSize.height / 2)) + _mapTileSize.height * ((int)_layerSize.height % 2);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
width = _layerSize.width * _mapTileSize.width;
|
|
|
|
height = _layerSize.height * _mapTileSize.height;
|
|
|
|
}
|
|
|
|
this->setContentSize(CC_SIZE_PIXELS_TO_POINTS(Size(width, height)));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_useAutomaticVertexZ = false;
|
|
|
|
_vertexZvalue = 0;
|
2012-08-20 14:06:29 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2012-08-20 14:06:29 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
TMXLayer::TMXLayer()
|
2013-07-29 14:07:57 +08:00
|
|
|
:_layerName("")
|
|
|
|
,_opacity(0)
|
|
|
|
,_vertexZvalue(0)
|
|
|
|
,_useAutomaticVertexZ(false)
|
2013-12-18 17:47:20 +08:00
|
|
|
,_reusedTile(nullptr)
|
|
|
|
,_atlasIndexArray(nullptr)
|
2013-07-29 14:07:57 +08:00
|
|
|
,_contentScaleFactor(1.0f)
|
|
|
|
,_layerSize(Size::ZERO)
|
2013-07-12 14:47:36 +08:00
|
|
|
,_mapTileSize(Size::ZERO)
|
2013-12-18 17:47:20 +08:00
|
|
|
,_tiles(nullptr)
|
|
|
|
,_tileSet(nullptr)
|
2013-07-29 14:07:57 +08:00
|
|
|
,_layerOrientation(TMXOrientationOrtho)
|
2016-07-02 06:51:00 +08:00
|
|
|
,_staggerAxis(TMXStaggerAxis_Y)
|
|
|
|
,_staggerIndex(TMXStaggerIndex_Even)
|
|
|
|
,_hexSideLength(0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{}
|
2012-08-20 14:06:29 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
TMXLayer::~TMXLayer()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_tileSet);
|
|
|
|
CC_SAFE_RELEASE(_reusedTile);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_atlasIndexArray)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
ccCArrayFree(_atlasIndexArray);
|
2013-12-18 17:47:20 +08:00
|
|
|
_atlasIndexArray = nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2016-09-12 10:02:48 +08:00
|
|
|
CC_SAFE_FREE(_tiles);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-08-20 14:06:29 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void TMXLayer::releaseMap()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_tiles)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2016-09-26 14:18:48 +08:00
|
|
|
free(_tiles);
|
2013-12-18 17:47:20 +08:00
|
|
|
_tiles = nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_atlasIndexArray)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
ccCArrayFree(_atlasIndexArray);
|
2013-12-18 17:47:20 +08:00
|
|
|
_atlasIndexArray = nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// TMXLayer - setup Tiles
|
|
|
|
void TMXLayer::setupTiles()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// Optimization: quick hack that sets the image size on the tileset
|
2013-06-15 14:03:30 +08:00
|
|
|
_tileSet->_imageSize = _textureAtlas->getTexture()->getContentSizeInPixels();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// By default all the tiles are aliased
|
|
|
|
// pros:
|
|
|
|
// - easier to render
|
|
|
|
// cons:
|
|
|
|
// - difficult to scale / rotate / etc.
|
2013-06-15 14:03:30 +08:00
|
|
|
_textureAtlas->getTexture()->setAliasTexParameters();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
//CFByteOrder o = CFByteOrderGetCurrent();
|
|
|
|
|
|
|
|
// Parse cocos2d properties
|
|
|
|
this->parseInternalProperties();
|
|
|
|
|
2013-12-20 09:50:53 +08:00
|
|
|
for (int y=0; y < _layerSize.height; y++)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-20 09:50:53 +08:00
|
|
|
for (int x=0; x < _layerSize.width; x++)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2016-07-02 06:51:00 +08:00
|
|
|
int newX = x;
|
2016-07-15 02:21:24 +08:00
|
|
|
// fix correct render ordering in Hexagonal maps when stagger axis == x
|
2016-07-02 06:51:00 +08:00
|
|
|
if (_staggerAxis == TMXStaggerAxis_X && _layerOrientation == TMXOrientationHex)
|
|
|
|
{
|
2016-07-20 13:55:05 +08:00
|
|
|
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;
|
|
|
|
}
|
2016-07-02 06:51:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int pos = static_cast<int>(newX + _layerSize.width * y);
|
2013-12-20 09:50:53 +08:00
|
|
|
int gid = _tiles[ pos ];
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// gid are stored in little endian.
|
|
|
|
// if host is big endian, then swap
|
|
|
|
//if( o == CFByteOrderBigEndian )
|
|
|
|
// gid = CFSwapInt32( gid );
|
|
|
|
/* We support little endian.*/
|
|
|
|
|
2014-08-30 03:54:24 +08:00
|
|
|
// FIXME:: gid == 0 --> empty tile
|
2012-08-20 14:06:29 +08:00
|
|
|
if (gid != 0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2016-07-02 06:51:00 +08:00
|
|
|
this->appendTileForGID(gid, Vec2(newX, y));
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// TMXLayer - Properties
|
2013-12-03 14:47:35 +08:00
|
|
|
Value TMXLayer::getProperty(const std::string& propertyName) const
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-03 14:47:35 +08:00
|
|
|
if (_properties.find(propertyName) != _properties.end())
|
|
|
|
return _properties.at(propertyName);
|
|
|
|
|
|
|
|
return Value();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void TMXLayer::parseInternalProperties()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// if cc_vertex=automatic, then tiles will be rendered using vertexz
|
|
|
|
|
2013-12-03 14:47:35 +08:00
|
|
|
auto vertexz = getProperty("cc_vertexz");
|
|
|
|
if (!vertexz.isNull())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-03 14:47:35 +08:00
|
|
|
std::string vertexZStr = vertexz.asString();
|
2012-04-19 14:35:52 +08:00
|
|
|
// If "automatic" is on, then parse the "cc_alpha_func" too
|
2013-12-03 14:47:35 +08:00
|
|
|
if (vertexZStr == "automatic")
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_useAutomaticVertexZ = true;
|
2013-12-03 14:47:35 +08:00
|
|
|
auto alphaFuncVal = getProperty("cc_alpha_func");
|
|
|
|
float alphaFuncValue = alphaFuncVal.asFloat();
|
2014-05-13 10:12:56 +08:00
|
|
|
setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_ALPHA_TEST));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-05-09 07:42:36 +08:00
|
|
|
GLint alphaValueLocation = glGetUniformLocation(getGLProgram()->getProgram(), GLProgram::UNIFORM_NAME_ALPHA_TEST_VALUE);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// NOTE: alpha test shader is hard-coded to use the equivalent of a glAlphaFunc(GL_GREATER) comparison
|
2013-07-01 16:02:46 +08:00
|
|
|
|
|
|
|
// use shader program to set uniform
|
2014-05-09 07:42:36 +08:00
|
|
|
getGLProgram()->use();
|
|
|
|
getGLProgram()->setUniformLocationWith1f(alphaValueLocation, alphaFuncValue);
|
2013-07-01 16:02:46 +08:00
|
|
|
CHECK_GL_ERROR_DEBUG();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-03 14:47:35 +08:00
|
|
|
_vertexZvalue = vertexz.asInt();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-09 10:34:50 +08:00
|
|
|
void TMXLayer::setupTileSprite(Sprite* sprite, const Vec2& pos, uint32_t gid)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-19 14:57:54 +08:00
|
|
|
sprite->setPosition(getPositionAt(pos));
|
2014-03-14 15:38:43 +08:00
|
|
|
sprite->setPositionZ((float)getVertexZForPos(pos));
|
2014-05-15 01:07:09 +08:00
|
|
|
sprite->setAnchorPoint(Vec2::ZERO);
|
2013-06-15 14:03:30 +08:00
|
|
|
sprite->setOpacity(_opacity);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
//issue 1264, flip can be undone as well
|
2013-09-16 20:38:03 +08:00
|
|
|
sprite->setFlippedX(false);
|
|
|
|
sprite->setFlippedY(false);
|
2012-06-14 05:26:28 +08:00
|
|
|
sprite->setRotation(0.0f);
|
2014-05-15 01:07:09 +08:00
|
|
|
sprite->setAnchorPoint(Vec2(0,0));
|
2012-06-14 05:26:28 +08:00
|
|
|
|
|
|
|
// Rotation in tiled is achieved using 3 flipped states, flipping across the horizontal, vertical, and diagonal axes of the tiles.
|
2013-06-20 14:13:12 +08:00
|
|
|
if (gid & kTMXTileDiagonalFlag)
|
2012-06-14 05:26:28 +08:00
|
|
|
{
|
|
|
|
// put the anchor in the middle for ease of rotation.
|
2014-05-15 01:07:09 +08:00
|
|
|
sprite->setAnchorPoint(Vec2(0.5f,0.5f));
|
2014-08-28 11:41:18 +08:00
|
|
|
sprite->setPosition(getPositionAt(pos).x + sprite->getContentSize().height/2,
|
|
|
|
getPositionAt(pos).y + sprite->getContentSize().width/2 );
|
2012-06-14 05:26:28 +08:00
|
|
|
|
2016-11-09 10:34:50 +08:00
|
|
|
auto flag = gid & (kTMXTileHorizontalFlag | kTMXTileVerticalFlag );
|
2012-06-14 05:26:28 +08:00
|
|
|
|
|
|
|
// handle the 4 diagonally flipped states.
|
2013-06-20 14:13:12 +08:00
|
|
|
if (flag == kTMXTileHorizontalFlag)
|
2012-06-14 05:26:28 +08:00
|
|
|
{
|
|
|
|
sprite->setRotation(90.0f);
|
|
|
|
}
|
2013-06-20 14:13:12 +08:00
|
|
|
else if (flag == kTMXTileVerticalFlag)
|
2012-06-14 05:26:28 +08:00
|
|
|
{
|
|
|
|
sprite->setRotation(270.0f);
|
|
|
|
}
|
2013-06-20 14:13:12 +08:00
|
|
|
else if (flag == (kTMXTileVerticalFlag | kTMXTileHorizontalFlag) )
|
2012-06-14 05:26:28 +08:00
|
|
|
{
|
|
|
|
sprite->setRotation(90.0f);
|
2013-09-16 20:38:03 +08:00
|
|
|
sprite->setFlippedX(true);
|
2012-06-14 05:26:28 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sprite->setRotation(270.0f);
|
2013-09-16 20:38:03 +08:00
|
|
|
sprite->setFlippedX(true);
|
2012-06-14 05:26:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
if (gid & kTMXTileHorizontalFlag)
|
2012-06-14 05:26:28 +08:00
|
|
|
{
|
2013-09-16 20:38:03 +08:00
|
|
|
sprite->setFlippedX(true);
|
2012-06-14 05:26:28 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
if (gid & kTMXTileVerticalFlag)
|
2012-06-14 05:26:28 +08:00
|
|
|
{
|
2013-09-16 20:38:03 +08:00
|
|
|
sprite->setFlippedY(true);
|
2012-06-14 05:26:28 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-17 12:17:56 +08:00
|
|
|
Sprite* TMXLayer::reusedTileWithRect(const Rect& rect)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (! _reusedTile)
|
2012-08-20 14:06:29 +08:00
|
|
|
{
|
2013-11-14 07:55:36 +08:00
|
|
|
_reusedTile = Sprite::createWithTexture(_textureAtlas->getTexture(), rect);
|
2013-06-15 14:03:30 +08:00
|
|
|
_reusedTile->setBatchNode(this);
|
2013-11-14 07:55:36 +08:00
|
|
|
_reusedTile->retain();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-08-30 03:54:24 +08:00
|
|
|
// FIXME: HACK: Needed because if "batch node" is nil,
|
2015-01-08 10:22:45 +08:00
|
|
|
// then the Sprite'squad will be reset
|
2013-12-18 17:47:20 +08:00
|
|
|
_reusedTile->setBatchNode(nullptr);
|
2012-11-14 18:05:15 +08:00
|
|
|
|
2015-01-08 10:22:45 +08:00
|
|
|
// Re-init the sprite
|
2013-06-15 14:03:30 +08:00
|
|
|
_reusedTile->setTextureRect(rect, false, rect.size);
|
2012-11-14 18:05:15 +08:00
|
|
|
|
2015-01-08 10:22:45 +08:00
|
|
|
// restore the batch node
|
2013-06-15 14:03:30 +08:00
|
|
|
_reusedTile->setBatchNode(this);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
return _reusedTile;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// TMXLayer - obtaining tiles/gids
|
2014-05-15 01:07:09 +08:00
|
|
|
Sprite * TMXLayer::getTileAt(const Vec2& pos)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(pos.x < _layerSize.width && pos.y < _layerSize.height && pos.x >=0 && pos.y >=0, "TMXLayer: invalid position");
|
|
|
|
CCASSERT(_tiles && _atlasIndexArray, "TMXLayer: the tiles map has been released");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-19 14:57:54 +08:00
|
|
|
Sprite *tile = nullptr;
|
2013-12-20 09:50:53 +08:00
|
|
|
int gid = this->getTileGIDAt(pos);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// if GID == 0, then no tile is present
|
2012-08-20 14:06:29 +08:00
|
|
|
if (gid)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
int z = (int)(pos.x + pos.y * _layerSize.width);
|
2013-07-19 14:57:54 +08:00
|
|
|
tile = static_cast<Sprite*>(this->getChildByTag(z));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// tile not created yet. create it
|
2012-08-20 14:06:29 +08:00
|
|
|
if (! tile)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-02-08 10:33:30 +08:00
|
|
|
Rect rect = _tileSet->getRectForGID(gid);
|
2012-04-19 14:35:52 +08:00
|
|
|
rect = CC_RECT_PIXELS_TO_POINTS(rect);
|
|
|
|
|
2013-11-14 07:55:36 +08:00
|
|
|
tile = Sprite::createWithTexture(this->getTexture(), rect);
|
2012-04-19 14:35:52 +08:00
|
|
|
tile->setBatchNode(this);
|
2013-07-19 14:57:54 +08:00
|
|
|
tile->setPosition(getPositionAt(pos));
|
2014-03-14 15:38:43 +08:00
|
|
|
tile->setPositionZ((float)getVertexZForPos(pos));
|
2014-05-15 01:07:09 +08:00
|
|
|
tile->setAnchorPoint(Vec2::ZERO);
|
2013-06-15 14:03:30 +08:00
|
|
|
tile->setOpacity(_opacity);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-12-12 12:07:20 +08:00
|
|
|
ssize_t indexForZ = atlasIndexForExistantZ(z);
|
|
|
|
this->addSpriteWithoutQuad(tile, static_cast<int>(indexForZ), z);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
2012-08-20 14:06:29 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return tile;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
uint32_t TMXLayer::getTileGIDAt(const Vec2& pos, TMXTileFlags* flags/* = nullptr*/)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(pos.x < _layerSize.width && pos.y < _layerSize.height && pos.x >=0 && pos.y >=0, "TMXLayer: invalid position");
|
|
|
|
CCASSERT(_tiles && _atlasIndexArray, "TMXLayer: the tiles map has been released");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2015-10-11 00:05:51 +08:00
|
|
|
ssize_t idx = static_cast<int>(((int) pos.x + (int) pos.y * _layerSize.width));
|
2012-04-19 14:35:52 +08:00
|
|
|
// Bits on the far end of the 32-bit global tile ID are used for tile flags
|
2014-02-08 10:33:30 +08:00
|
|
|
uint32_t tile = _tiles[idx];
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// issue1264, flipped tiles can be changed dynamically
|
|
|
|
if (flags)
|
|
|
|
{
|
2014-02-08 10:33:30 +08:00
|
|
|
*flags = (TMXTileFlags)(tile & kTMXFlipedAll);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-08-20 14:06:29 +08:00
|
|
|
|
2014-02-08 10:33:30 +08:00
|
|
|
return (tile & kTMXFlippedMask);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// TMXLayer - adding helper methods
|
2014-05-15 01:07:09 +08:00
|
|
|
Sprite * TMXLayer::insertTileForGID(uint32_t gid, const Vec2& pos)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-02-08 10:33:30 +08:00
|
|
|
if (gid != 0 && (static_cast<int>((gid & kTMXFlippedMask)) - _tileSet->_firstGid) >= 0)
|
2013-12-23 10:37:46 +08:00
|
|
|
{
|
2014-02-08 10:33:30 +08:00
|
|
|
Rect rect = _tileSet->getRectForGID(gid);
|
2013-12-23 10:37:46 +08:00
|
|
|
rect = CC_RECT_PIXELS_TO_POINTS(rect);
|
|
|
|
|
2015-10-11 00:05:51 +08:00
|
|
|
intptr_t z = (intptr_t)((int) pos.x + (int) pos.y * _layerSize.width);
|
2013-12-23 10:37:46 +08:00
|
|
|
|
|
|
|
Sprite *tile = reusedTileWithRect(rect);
|
|
|
|
|
|
|
|
setupTileSprite(tile, pos, gid);
|
|
|
|
|
|
|
|
// get atlas index
|
|
|
|
ssize_t indexForZ = atlasIndexForNewZ(static_cast<int>(z));
|
|
|
|
|
|
|
|
// Optimization: add the quad without adding a child
|
|
|
|
this->insertQuadFromSprite(tile, indexForZ);
|
|
|
|
|
|
|
|
// insert it into the local atlasindex array
|
|
|
|
ccCArrayInsertValueAtIndex(_atlasIndexArray, (void*)z, indexForZ);
|
|
|
|
|
|
|
|
// update possible children
|
|
|
|
|
2013-12-23 15:40:33 +08:00
|
|
|
for(const auto &child : _children) {
|
2013-12-23 10:37:46 +08:00
|
|
|
Sprite* sp = static_cast<Sprite*>(child);
|
2013-12-23 15:40:33 +08:00
|
|
|
ssize_t ai = sp->getAtlasIndex();
|
|
|
|
if ( ai >= indexForZ )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-23 15:40:33 +08:00
|
|
|
sp->setAtlasIndex(ai+1);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
2013-12-23 10:37:46 +08:00
|
|
|
|
|
|
|
_tiles[z] = gid;
|
|
|
|
return tile;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-12-03 14:47:35 +08:00
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Sprite * TMXLayer::updateTileForGID(uint32_t gid, const Vec2& pos)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-02-08 10:33:30 +08:00
|
|
|
Rect rect = _tileSet->getRectForGID(gid);
|
2013-07-12 14:30:26 +08:00
|
|
|
rect = Rect(rect.origin.x / _contentScaleFactor, rect.origin.y / _contentScaleFactor, rect.size.width/ _contentScaleFactor, rect.size.height/ _contentScaleFactor);
|
2015-10-11 00:05:51 +08:00
|
|
|
int z = (int)((int) pos.x + (int) pos.y * _layerSize.width);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Sprite *tile = reusedTileWithRect(rect);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
setupTileSprite(tile ,pos ,gid);
|
|
|
|
|
|
|
|
// get atlas index
|
2013-12-12 12:07:20 +08:00
|
|
|
ssize_t indexForZ = atlasIndexForExistantZ(z);
|
2012-04-19 14:35:52 +08:00
|
|
|
tile->setAtlasIndex(indexForZ);
|
|
|
|
tile->setDirty(true);
|
|
|
|
tile->updateTransform();
|
2013-06-15 14:03:30 +08:00
|
|
|
_tiles[z] = gid;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
return tile;
|
|
|
|
}
|
|
|
|
|
2016-09-14 12:47:36 +08:00
|
|
|
intptr_t TMXLayer::getZForPos(const Vec2& pos) const
|
|
|
|
{
|
|
|
|
intptr_t z = -1;
|
|
|
|
// fix correct render ordering in Hexagonal maps when stagger axis == x
|
|
|
|
if (_staggerAxis == TMXStaggerAxis_X && _layerOrientation == TMXOrientationHex)
|
|
|
|
{
|
|
|
|
if (_staggerIndex == TMXStaggerIndex_Odd)
|
|
|
|
{
|
|
|
|
if (((int)pos.x % 2) == 0)
|
|
|
|
z = pos.x / 2 + pos.y * _layerSize.width;
|
|
|
|
else
|
|
|
|
z = pos.x / 2 + std::ceil(_layerSize.width / 2) + pos.y * _layerSize.width;
|
|
|
|
} else {
|
|
|
|
// TMXStaggerIndex_Even
|
|
|
|
if (((int)pos.x % 2) == 1)
|
|
|
|
z = pos.x / 2 + pos.y * _layerSize.width;
|
|
|
|
else
|
|
|
|
z = pos.x / 2 + std::floor(_layerSize.width / 2) + pos.y * _layerSize.width;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
z = (pos.x + pos.y * _layerSize.width);
|
|
|
|
}
|
|
|
|
|
|
|
|
CCASSERT(z != -1, "Invalid Z");
|
|
|
|
return z;
|
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// used only when parsing the map. useless after the map was parsed
|
|
|
|
// since lot's of assumptions are no longer true
|
2014-05-15 01:07:09 +08:00
|
|
|
Sprite * TMXLayer::appendTileForGID(uint32_t gid, const Vec2& pos)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-02-08 10:33:30 +08:00
|
|
|
if (gid != 0 && (static_cast<int>((gid & kTMXFlippedMask)) - _tileSet->_firstGid) >= 0)
|
2013-12-23 10:37:46 +08:00
|
|
|
{
|
2014-02-08 10:33:30 +08:00
|
|
|
Rect rect = _tileSet->getRectForGID(gid);
|
2013-12-23 10:37:46 +08:00
|
|
|
rect = CC_RECT_PIXELS_TO_POINTS(rect);
|
2016-09-14 12:47:36 +08:00
|
|
|
|
|
|
|
// Z could be just an integer that gets incremented each time it is called.
|
|
|
|
// but that wouldn't work on layers with empty tiles.
|
|
|
|
// and it is IMPORTANT that Z returns an unique and bigger number than the previous one.
|
|
|
|
// since _atlasIndexArray must be ordered because `bsearch` is used to find the GID for
|
|
|
|
// a given Z. (github issue #16512)
|
|
|
|
intptr_t z = getZForPos(pos);
|
|
|
|
|
2013-12-23 10:37:46 +08:00
|
|
|
Sprite *tile = reusedTileWithRect(rect);
|
|
|
|
|
|
|
|
setupTileSprite(tile ,pos ,gid);
|
|
|
|
|
|
|
|
// optimization:
|
|
|
|
// The difference between appendTileForGID and insertTileforGID is that append is faster, since
|
|
|
|
// it appends the tile at the end of the texture atlas
|
|
|
|
ssize_t indexForZ = _atlasIndexArray->num;
|
|
|
|
|
|
|
|
// don't add it using the "standard" way.
|
|
|
|
insertQuadFromSprite(tile, indexForZ);
|
|
|
|
|
|
|
|
// append should be after addQuadFromSprite since it modifies the quantity values
|
|
|
|
ccCArrayInsertValueAtIndex(_atlasIndexArray, (void*)z, indexForZ);
|
2016-09-14 12:47:36 +08:00
|
|
|
|
|
|
|
// Validation for issue #16512
|
|
|
|
CCASSERT(_atlasIndexArray->num == 1 ||
|
|
|
|
_atlasIndexArray->arr[_atlasIndexArray->num-1] > _atlasIndexArray->arr[_atlasIndexArray->num-2], "Invalid z for _atlasIndexArray");
|
|
|
|
|
2013-12-23 10:37:46 +08:00
|
|
|
return tile;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// TMXLayer - atlasIndex and Z
|
2012-04-19 14:35:52 +08:00
|
|
|
static inline int compareInts(const void * a, const void * b)
|
|
|
|
{
|
2016-09-14 12:47:36 +08:00
|
|
|
const int ia = *(int*)a;
|
|
|
|
const int ib = *(int*)b;
|
|
|
|
return (ia-ib);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-12-03 14:47:35 +08:00
|
|
|
|
2013-12-20 09:50:53 +08:00
|
|
|
ssize_t TMXLayer::atlasIndexForExistantZ(int z)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
int key=z;
|
2013-06-15 14:03:30 +08:00
|
|
|
int *item = (int*)bsearch((void*)&key, (void*)&_atlasIndexArray->arr[0], _atlasIndexArray->num, sizeof(void*), compareInts);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(item, "TMX atlas index not found. Shall not happen");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-12-12 12:07:20 +08:00
|
|
|
ssize_t index = ((size_t)item - (size_t)_atlasIndexArray->arr) / sizeof(void*);
|
2012-04-19 14:35:52 +08:00
|
|
|
return index;
|
|
|
|
}
|
2013-12-03 14:47:35 +08:00
|
|
|
|
2013-12-12 12:07:20 +08:00
|
|
|
ssize_t TMXLayer::atlasIndexForNewZ(int z)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-08-30 03:54:24 +08:00
|
|
|
// FIXME:: This can be improved with a sort of binary search
|
2013-12-12 12:07:20 +08:00
|
|
|
ssize_t i=0;
|
2013-06-15 14:03:30 +08:00
|
|
|
for (i=0; i< _atlasIndexArray->num ; i++)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-12 12:07:20 +08:00
|
|
|
ssize_t val = (size_t) _atlasIndexArray->arr[i];
|
2012-08-20 14:06:29 +08:00
|
|
|
if (z < val)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
break;
|
2012-08-20 14:06:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// TMXLayer - adding / remove tiles
|
2014-05-15 01:07:09 +08:00
|
|
|
void TMXLayer::setTileGID(uint32_t gid, const Vec2& pos)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-02-08 10:11:17 +08:00
|
|
|
setTileGID(gid, pos, (TMXTileFlags)0);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void TMXLayer::setTileGID(uint32_t gid, const Vec2& pos, TMXTileFlags flags)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(pos.x < _layerSize.width && pos.y < _layerSize.height && pos.x >=0 && pos.y >=0, "TMXLayer: invalid position");
|
|
|
|
CCASSERT(_tiles && _atlasIndexArray, "TMXLayer: the tiles map has been released");
|
2014-03-24 10:12:40 +08:00
|
|
|
CCASSERT(gid == 0 || (int)gid >= _tileSet->_firstGid, "TMXLayer: invalid gid" );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-02-08 10:11:17 +08:00
|
|
|
TMXTileFlags currentFlags;
|
2014-02-08 10:33:30 +08:00
|
|
|
uint32_t currentGID = getTileGIDAt(pos, ¤tFlags);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-08-20 14:06:29 +08:00
|
|
|
if (currentGID != gid || currentFlags != flags)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-02-08 10:33:30 +08:00
|
|
|
uint32_t gidAndFlags = gid | flags;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// setting gid=0 is equal to remove the tile
|
2012-08-20 14:06:29 +08:00
|
|
|
if (gid == 0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
removeTileAt(pos);
|
|
|
|
}
|
|
|
|
// empty tile. create a new one
|
2012-08-20 14:06:29 +08:00
|
|
|
else if (currentGID == 0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
insertTileForGID(gidAndFlags, pos);
|
|
|
|
}
|
|
|
|
// modifying an existing tile with a non-empty tile
|
|
|
|
else
|
|
|
|
{
|
2015-10-11 00:05:51 +08:00
|
|
|
int z = (int) pos.x + (int) pos.y * _layerSize.width;
|
2013-07-19 14:57:54 +08:00
|
|
|
Sprite *sprite = static_cast<Sprite*>(getChildByTag(z));
|
2012-08-20 14:06:29 +08:00
|
|
|
if (sprite)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-02-08 10:33:30 +08:00
|
|
|
Rect rect = _tileSet->getRectForGID(gid);
|
2012-04-19 14:35:52 +08:00
|
|
|
rect = CC_RECT_PIXELS_TO_POINTS(rect);
|
|
|
|
|
|
|
|
sprite->setTextureRect(rect, false, rect.size);
|
|
|
|
if (flags)
|
|
|
|
{
|
|
|
|
setupTileSprite(sprite, sprite->getPosition(), gidAndFlags);
|
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
_tiles[z] = gidAndFlags;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
updateTileForGID(gidAndFlags, pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-19 14:57:54 +08:00
|
|
|
|
2016-11-16 09:48:37 +08:00
|
|
|
void TMXLayer::addChild(Node* /*child*/, int /*zOrder*/, int /*tag*/)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(0, "addChild: is not supported on TMXLayer. Instead use setTileGID:at:/tileAt:");
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-07-19 14:57:54 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void TMXLayer::removeChild(Node* node, bool cleanup)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Sprite *sprite = (Sprite*)node;
|
2012-04-19 14:35:52 +08:00
|
|
|
// allows removing nil objects
|
2012-08-20 14:06:29 +08:00
|
|
|
if (! sprite)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return;
|
2012-08-20 14:06:29 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-12-05 10:35:10 +08:00
|
|
|
CCASSERT(_children.contains(sprite), "Tile does not belong to TMXLayer");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-12-12 12:07:20 +08:00
|
|
|
ssize_t atlasIndex = sprite->getAtlasIndex();
|
2013-12-13 16:41:03 +08:00
|
|
|
ssize_t zz = (ssize_t)_atlasIndexArray->arr[atlasIndex];
|
2013-06-15 14:03:30 +08:00
|
|
|
_tiles[zz] = 0;
|
|
|
|
ccCArrayRemoveValueAtIndex(_atlasIndexArray, atlasIndex);
|
2013-06-20 14:13:12 +08:00
|
|
|
SpriteBatchNode::removeChild(sprite, cleanup);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-07-19 14:57:54 +08:00
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void TMXLayer::removeTileAt(const Vec2& pos)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(pos.x < _layerSize.width && pos.y < _layerSize.height && pos.x >=0 && pos.y >=0, "TMXLayer: invalid position");
|
|
|
|
CCASSERT(_tiles && _atlasIndexArray, "TMXLayer: the tiles map has been released");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-12-20 09:50:53 +08:00
|
|
|
int gid = getTileGIDAt(pos);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-08-20 14:06:29 +08:00
|
|
|
if (gid)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-20 09:50:53 +08:00
|
|
|
int z = pos.x + pos.y * _layerSize.width;
|
2013-12-12 12:07:20 +08:00
|
|
|
ssize_t atlasIndex = atlasIndexForExistantZ(z);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// remove tile from GID map
|
2013-06-15 14:03:30 +08:00
|
|
|
_tiles[z] = 0;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// remove tile from atlas position array
|
2013-06-15 14:03:30 +08:00
|
|
|
ccCArrayRemoveValueAtIndex(_atlasIndexArray, atlasIndex);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// remove it from sprites and/or texture atlas
|
2013-06-20 14:13:12 +08:00
|
|
|
Sprite *sprite = (Sprite*)getChildByTag(z);
|
2012-08-20 14:06:29 +08:00
|
|
|
if (sprite)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
SpriteBatchNode::removeChild(sprite, true);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_textureAtlas->removeQuadAtIndex(atlasIndex);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// update possible children
|
2013-12-20 05:34:41 +08:00
|
|
|
for(const auto &obj : _children) {
|
2013-11-28 16:02:03 +08:00
|
|
|
Sprite* child = static_cast<Sprite*>(obj);
|
2013-12-20 05:34:41 +08:00
|
|
|
ssize_t ai = child->getAtlasIndex();
|
|
|
|
if ( ai >= atlasIndex )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-20 05:34:41 +08:00
|
|
|
child->setAtlasIndex(ai-1);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-12-20 05:34:41 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//CCTMXLayer - obtaining positions, offset
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 TMXLayer::calculateLayerOffset(const Vec2& pos)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2015-04-20 01:40:52 +08:00
|
|
|
Vec2 ret;
|
2016-07-02 06:51:00 +08:00
|
|
|
switch (_layerOrientation)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2016-07-02 06:51:00 +08:00
|
|
|
case TMXOrientationOrtho:
|
|
|
|
ret.set( pos.x * _mapTileSize.width, -pos.y *_mapTileSize.height);
|
|
|
|
break;
|
|
|
|
case TMXOrientationIso:
|
|
|
|
ret.set((_mapTileSize.width /2) * (pos.x - pos.y),
|
2013-06-15 14:03:30 +08:00
|
|
|
(_mapTileSize.height /2 ) * (-pos.x - pos.y));
|
2016-07-02 06:51:00 +08:00
|
|
|
break;
|
|
|
|
case TMXOrientationHex:
|
|
|
|
{
|
|
|
|
if(_staggerAxis == TMXStaggerAxis_Y)
|
|
|
|
{
|
|
|
|
int diffX = (_staggerIndex == TMXStaggerIndex_Even) ? _mapTileSize.width/2 : 0;
|
|
|
|
ret.set(pos.x * _mapTileSize.width + diffX, -pos.y * (_mapTileSize.height - (_mapTileSize.width - _hexSideLength) / 2));
|
|
|
|
}
|
|
|
|
else if(_staggerAxis == TMXStaggerAxis_X)
|
|
|
|
{
|
|
|
|
int diffY = (_staggerIndex == TMXStaggerIndex_Odd) ? _mapTileSize.height/2 : 0;
|
|
|
|
ret.set(pos.x * (_mapTileSize.width - (_mapTileSize.width - _hexSideLength) / 2), -pos.y * _mapTileSize.height + diffY);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TMXOrientationStaggered:
|
2014-10-11 14:22:31 +08:00
|
|
|
{
|
|
|
|
float diffX = 0;
|
2015-04-07 03:08:30 +08:00
|
|
|
if ((int)std::abs(pos.y) % 2 == 1)
|
2014-10-11 14:22:31 +08:00
|
|
|
{
|
|
|
|
diffX = _mapTileSize.width/2;
|
|
|
|
}
|
2015-04-20 01:40:52 +08:00
|
|
|
ret.set(pos.x * _mapTileSize.width + diffX,
|
2014-10-11 14:22:31 +08:00
|
|
|
(-pos.y) * _mapTileSize.height/2);
|
|
|
|
}
|
2014-10-10 17:52:13 +08:00
|
|
|
break;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2013-07-19 14:57:54 +08:00
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 TMXLayer::getPositionAt(const Vec2& pos)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2015-04-20 01:40:52 +08:00
|
|
|
Vec2 ret;
|
2013-06-15 14:03:30 +08:00
|
|
|
switch (_layerOrientation)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
case TMXOrientationOrtho:
|
2013-07-19 14:57:54 +08:00
|
|
|
ret = getPositionForOrthoAt(pos);
|
2012-04-19 14:35:52 +08:00
|
|
|
break;
|
2013-06-20 14:13:12 +08:00
|
|
|
case TMXOrientationIso:
|
2013-07-19 14:57:54 +08:00
|
|
|
ret = getPositionForIsoAt(pos);
|
2012-04-19 14:35:52 +08:00
|
|
|
break;
|
2013-06-20 14:13:12 +08:00
|
|
|
case TMXOrientationHex:
|
2013-07-19 14:57:54 +08:00
|
|
|
ret = getPositionForHexAt(pos);
|
2012-04-19 14:35:52 +08:00
|
|
|
break;
|
2014-10-10 17:52:13 +08:00
|
|
|
case TMXOrientationStaggered:
|
|
|
|
ret = getPositionForStaggeredAt(pos);
|
|
|
|
break;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
ret = CC_POINT_PIXELS_TO_POINTS( ret );
|
|
|
|
return ret;
|
|
|
|
}
|
2013-07-19 14:57:54 +08:00
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 TMXLayer::getPositionForOrthoAt(const Vec2& pos)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
return Vec2(pos.x * _mapTileSize.width,
|
2013-06-15 14:03:30 +08:00
|
|
|
(_layerSize.height - pos.y - 1) * _mapTileSize.height);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-07-19 14:57:54 +08:00
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 TMXLayer::getPositionForIsoAt(const Vec2& pos)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
return Vec2(_mapTileSize.width /2 * (_layerSize.width + pos.x - pos.y - 1),
|
2013-06-15 14:03:30 +08:00
|
|
|
_mapTileSize.height /2 * ((_layerSize.height * 2 - pos.x - pos.y) - 2));
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-07-19 14:57:54 +08:00
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 TMXLayer::getPositionForHexAt(const Vec2& pos)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2016-07-02 06:51:00 +08:00
|
|
|
Vec2 xy;
|
|
|
|
Vec2 offset = _tileSet->_tileOffset;
|
|
|
|
|
|
|
|
int odd_even = (_staggerIndex == TMXStaggerIndex_Odd) ? 1 : -1;
|
|
|
|
switch (_staggerAxis)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2016-07-02 06:51:00 +08:00
|
|
|
case TMXStaggerAxis_Y:
|
|
|
|
{
|
|
|
|
float diffX = 0;
|
|
|
|
if ((int)pos.y % 2 == 1)
|
|
|
|
{
|
|
|
|
diffX = _mapTileSize.width/2 * odd_even;
|
|
|
|
}
|
|
|
|
xy = Vec2(pos.x * _mapTileSize.width+diffX+offset.x,
|
|
|
|
(_layerSize.height - pos.y - 1) * (_mapTileSize.height-(_mapTileSize.height-_hexSideLength)/2)-offset.y);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case TMXStaggerAxis_X:
|
|
|
|
{
|
|
|
|
float diffY = 0;
|
|
|
|
if ((int)pos.x % 2 == 1)
|
|
|
|
{
|
|
|
|
diffY = _mapTileSize.height/2 * -odd_even;
|
|
|
|
}
|
|
|
|
|
|
|
|
xy = Vec2(pos.x * (_mapTileSize.width-(_mapTileSize.width-_hexSideLength)/2)+offset.x,
|
|
|
|
(_layerSize.height - pos.y - 1) * _mapTileSize.height + diffY-offset.y);
|
|
|
|
break;
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
return xy;
|
|
|
|
}
|
2013-07-19 14:57:54 +08:00
|
|
|
|
2014-10-10 17:52:13 +08:00
|
|
|
Vec2 TMXLayer::getPositionForStaggeredAt(const Vec2 &pos)
|
|
|
|
{
|
2014-10-11 14:22:31 +08:00
|
|
|
float diffX = 0;
|
|
|
|
if ((int)pos.y % 2 == 1)
|
|
|
|
{
|
|
|
|
diffX = _mapTileSize.width/2;
|
|
|
|
}
|
|
|
|
return Vec2(pos.x * _mapTileSize.width + diffX,
|
|
|
|
(_layerSize.height - pos.y - 1) * _mapTileSize.height/2);
|
2014-10-10 17:52:13 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
int TMXLayer::getVertexZForPos(const Vec2& pos)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
int ret = 0;
|
2013-12-20 09:50:53 +08:00
|
|
|
int maxVal = 0;
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_useAutomaticVertexZ)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
switch (_layerOrientation)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
case TMXOrientationIso:
|
2013-12-20 09:50:53 +08:00
|
|
|
maxVal = static_cast<int>(_layerSize.width + _layerSize.height);
|
|
|
|
ret = static_cast<int>(-(maxVal - (pos.x + pos.y)));
|
2012-04-19 14:35:52 +08:00
|
|
|
break;
|
2013-06-20 14:13:12 +08:00
|
|
|
case TMXOrientationOrtho:
|
2013-12-20 09:50:53 +08:00
|
|
|
ret = static_cast<int>(-(_layerSize.height-pos.y));
|
2012-04-19 14:35:52 +08:00
|
|
|
break;
|
2014-10-10 17:52:13 +08:00
|
|
|
case TMXOrientationStaggered:
|
|
|
|
ret = static_cast<int>(-(_layerSize.height-pos.y));
|
|
|
|
break;
|
2013-06-20 14:13:12 +08:00
|
|
|
case TMXOrientationHex:
|
2016-07-02 06:51:00 +08:00
|
|
|
ret = static_cast<int>(-(_layerSize.height-pos.y));
|
2012-04-19 14:35:52 +08:00
|
|
|
break;
|
|
|
|
default:
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(0, "TMX invalid value");
|
2012-04-19 14:35:52 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
ret = _vertexZvalue;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-08-20 14:06:29 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-12-13 06:30:22 +08:00
|
|
|
std::string TMXLayer::getDescription() const
|
|
|
|
{
|
2013-12-13 16:51:57 +08:00
|
|
|
return StringUtils::format("<TMXLayer | tag = %d, size = %d,%d>", _tag, (int)_mapTileSize.width, (int)_mapTileSize.height);
|
2013-12-13 06:30:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
NS_CC_END
|