axmol/cocos/2d/CCTMXLayer.cpp

711 lines
21 KiB
C++
Raw Normal View History

/****************************************************************************
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2011 Zynga Inc.
Copyright (c) 2013-2014 Chukong Technologies Inc.
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-10 09:39:25 +08:00
#include "2d/CCTMXLayer.h"
#include "2d/CCTMXXMLParser.h"
#include "2d/CCTMXTiledMap.h"
#include "2d/CCSprite.h"
Squashed commit of the following: commit c16dcfaaea0922039aad05bce1f4efed18e04871 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:05:18 2014 -0700 more linux fixes commit 1553795976c9090a1b46deb53d12910fe0676008 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:04:04 2014 -0700 more linux fixes commit 1e43a8cabff33cbf25aa5eb5412f53a878222d83 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:02:07 2014 -0700 fixes linux isuses commit 723a445dd6411f91846da2b801248ad8298174f1 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:58:50 2014 -0700 more linux fixes commit 533c8025e794fc76cef02f396b3a93b3d7f4cfc8 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:57:33 2014 -0700 more linux fixes commit 4ba1e84959670bcbf044f18d1c0d4b3cb3be4090 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:53:43 2014 -0700 more linux fixes commit 1f8e011f306a47ed4134224e5e349929201f0539 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:49:28 2014 -0700 more linux fixes commit 3e2033100822ff6d532a1b4f012337491dc11920 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:47:43 2014 -0700 more linux fixes commit 2e708863c75fd032f1b2396dfdf1d31f7a62b713 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:46:00 2014 -0700 more linux fixes commit 861b5b92a6efd4de7b926c20d636ce9d749b293f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:43:15 2014 -0700 more linux fixes commit 2a43365a0c1755e9b9cada53301be1a20adb31cf Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:36:06 2014 -0700 more fixes for linux commit 7d332bf911892f87c7824d2a5da7bf73ce7ec411 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:35:29 2014 -0700 more fixes for linux commit f1becc17d3316dfe3678c23c9dcedb7a447d9235 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:34:44 2014 -0700 more fixes for linux commit d2e5959bb0dde921dd5e73be1d8acc3b3f50e51d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:33:45 2014 -0700 fixes for linux commit ad9b633c352107cf0e8b060a0e23d6e6a3f5e80f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:16:48 2014 -0700 compiles on Windows commit 4425ee8e5de8f42a2d6050e4470109600dce8b5d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:07:20 2014 -0700 fix builder
2014-05-01 10:09:13 +08:00
#include "2d/CCTextureCache.h"
#include "2d/ccCArray.h"
2014-05-10 09:39:25 +08:00
#include "renderer/CCGLProgramState.h"
#include "renderer/CCGLProgram.h"
2014-04-30 08:37:36 +08:00
#include "base/CCDirector.h"
2014-04-09 22:30:49 +08:00
#include "deprecated/CCString.h" // For StringUtils::format
NS_CC_BEGIN
// TMXLayer - init & alloc & dealloc
TMXLayer * TMXLayer::create(TMXTilesetInfo *tilesetInfo, TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo)
{
TMXLayer *ret = new TMXLayer();
if (ret->initWithTilesetInfo(tilesetInfo, layerInfo, mapInfo))
{
ret->autorelease();
return ret;
}
return nullptr;
}
bool TMXLayer::initWithTilesetInfo(TMXTilesetInfo *tilesetInfo, TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo)
{
// XXX: is 35% a good estimate ?
Size size = layerInfo->_layerSize;
float totalNumberOfTiles = size.width * size.height;
float capacity = totalNumberOfTiles * 0.35f + 1; // 35 percent is occupied ?
Texture2D *texture = nullptr;
if( tilesetInfo )
{
texture = Director::getInstance()->getTextureCache()->addImage(tilesetInfo->_sourceImage.c_str());
}
2013-12-20 09:50:53 +08:00
if (SpriteBatchNode::initWithTexture(texture, static_cast<ssize_t>(capacity)))
{
// layerInfo
_layerName = layerInfo->_name;
_layerSize = size;
_tiles = layerInfo->_tiles;
_opacity = layerInfo->_opacity;
setProperties(layerInfo->getProperties());
_contentScaleFactor = Director::getInstance()->getContentScaleFactor();
// tilesetInfo
_tileSet = tilesetInfo;
CC_SAFE_RETAIN(_tileSet);
// mapInfo
_mapTileSize = mapInfo->getTileSize();
_layerOrientation = mapInfo->getOrientation();
// offset (after layer orientation is set);
Vector2 offset = this->calculateLayerOffset(layerInfo->_offset);
this->setPosition(CC_POINT_PIXELS_TO_POINTS(offset));
2013-12-20 09:50:53 +08:00
_atlasIndexArray = ccCArrayNew(totalNumberOfTiles);
this->setContentSize(CC_SIZE_PIXELS_TO_POINTS(Size(_layerSize.width * _mapTileSize.width, _layerSize.height * _mapTileSize.height)));
_useAutomaticVertexZ = false;
_vertexZvalue = 0;
return true;
}
return false;
}
TMXLayer::TMXLayer()
:_layerName("")
,_opacity(0)
,_vertexZvalue(0)
,_useAutomaticVertexZ(false)
,_reusedTile(nullptr)
,_atlasIndexArray(nullptr)
,_contentScaleFactor(1.0f)
,_layerSize(Size::ZERO)
,_mapTileSize(Size::ZERO)
,_tiles(nullptr)
,_tileSet(nullptr)
,_layerOrientation(TMXOrientationOrtho)
{}
TMXLayer::~TMXLayer()
{
CC_SAFE_RELEASE(_tileSet);
CC_SAFE_RELEASE(_reusedTile);
if (_atlasIndexArray)
{
ccCArrayFree(_atlasIndexArray);
_atlasIndexArray = nullptr;
}
CC_SAFE_DELETE_ARRAY(_tiles);
}
void TMXLayer::releaseMap()
{
if (_tiles)
{
delete [] _tiles;
_tiles = nullptr;
}
if (_atlasIndexArray)
{
ccCArrayFree(_atlasIndexArray);
_atlasIndexArray = nullptr;
}
}
// TMXLayer - setup Tiles
void TMXLayer::setupTiles()
{
// Optimization: quick hack that sets the image size on the tileset
_tileSet->_imageSize = _textureAtlas->getTexture()->getContentSizeInPixels();
// By default all the tiles are aliased
// pros:
// - easier to render
// cons:
// - difficult to scale / rotate / etc.
_textureAtlas->getTexture()->setAliasTexParameters();
//CFByteOrder o = CFByteOrderGetCurrent();
// Parse cocos2d properties
this->parseInternalProperties();
2013-12-20 09:50:53 +08:00
for (int y=0; y < _layerSize.height; y++)
{
2013-12-20 09:50:53 +08:00
for (int x=0; x < _layerSize.width; x++)
{
int pos = static_cast<int>(x + _layerSize.width * y);
2013-12-20 09:50:53 +08:00
int gid = _tiles[ pos ];
// gid are stored in little endian.
// if host is big endian, then swap
//if( o == CFByteOrderBigEndian )
// gid = CFSwapInt32( gid );
/* We support little endian.*/
// XXX: gid == 0 --> empty tile
if (gid != 0)
{
this->appendTileForGID(gid, Vector2(x, y));
}
}
}
}
// TMXLayer - Properties
Value TMXLayer::getProperty(const std::string& propertyName) const
{
if (_properties.find(propertyName) != _properties.end())
return _properties.at(propertyName);
return Value();
}
void TMXLayer::parseInternalProperties()
{
// if cc_vertex=automatic, then tiles will be rendered using vertexz
auto vertexz = getProperty("cc_vertexz");
if (!vertexz.isNull())
{
std::string vertexZStr = vertexz.asString();
// If "automatic" is on, then parse the "cc_alpha_func" too
if (vertexZStr == "automatic")
{
_useAutomaticVertexZ = true;
auto alphaFuncVal = getProperty("cc_alpha_func");
float alphaFuncValue = alphaFuncVal.asFloat();
setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_ALPHA_TEST));
2014-05-09 07:42:36 +08:00
GLint alphaValueLocation = glGetUniformLocation(getGLProgram()->getProgram(), GLProgram::UNIFORM_NAME_ALPHA_TEST_VALUE);
// NOTE: alpha test shader is hard-coded to use the equivalent of a glAlphaFunc(GL_GREATER) comparison
// use shader program to set uniform
2014-05-09 07:42:36 +08:00
getGLProgram()->use();
getGLProgram()->setUniformLocationWith1f(alphaValueLocation, alphaFuncValue);
CHECK_GL_ERROR_DEBUG();
}
else
{
_vertexZvalue = vertexz.asInt();
}
}
}
void TMXLayer::setupTileSprite(Sprite* sprite, Vector2 pos, int gid)
{
sprite->setPosition(getPositionAt(pos));
2014-03-14 15:38:43 +08:00
sprite->setPositionZ((float)getVertexZForPos(pos));
sprite->setAnchorPoint(Vector2::ZERO);
sprite->setOpacity(_opacity);
//issue 1264, flip can be undone as well
sprite->setFlippedX(false);
sprite->setFlippedY(false);
sprite->setRotation(0.0f);
sprite->setAnchorPoint(Vector2(0,0));
// Rotation in tiled is achieved using 3 flipped states, flipping across the horizontal, vertical, and diagonal axes of the tiles.
if (gid & kTMXTileDiagonalFlag)
{
// put the anchor in the middle for ease of rotation.
sprite->setAnchorPoint(Vector2(0.5f,0.5f));
sprite->setPosition(Vector2(getPositionAt(pos).x + sprite->getContentSize().height/2,
getPositionAt(pos).y + sprite->getContentSize().width/2 ) );
2013-12-20 09:50:53 +08:00
int flag = gid & (kTMXTileHorizontalFlag | kTMXTileVerticalFlag );
// handle the 4 diagonally flipped states.
if (flag == kTMXTileHorizontalFlag)
{
sprite->setRotation(90.0f);
}
else if (flag == kTMXTileVerticalFlag)
{
sprite->setRotation(270.0f);
}
else if (flag == (kTMXTileVerticalFlag | kTMXTileHorizontalFlag) )
{
sprite->setRotation(90.0f);
sprite->setFlippedX(true);
}
else
{
sprite->setRotation(270.0f);
sprite->setFlippedX(true);
}
}
else
{
if (gid & kTMXTileHorizontalFlag)
{
sprite->setFlippedX(true);
}
if (gid & kTMXTileVerticalFlag)
{
sprite->setFlippedY(true);
}
}
}
Sprite* TMXLayer::reusedTileWithRect(Rect rect)
{
if (! _reusedTile)
{
2013-11-14 07:55:36 +08:00
_reusedTile = Sprite::createWithTexture(_textureAtlas->getTexture(), rect);
_reusedTile->setBatchNode(this);
2013-11-14 07:55:36 +08:00
_reusedTile->retain();
}
else
{
2012-11-14 18:05:15 +08:00
// XXX HACK: Needed because if "batch node" is nil,
// then the Sprite'squad will be reset
_reusedTile->setBatchNode(nullptr);
2012-11-14 18:05:15 +08:00
// Re-init the sprite
_reusedTile->setTextureRect(rect, false, rect.size);
2012-11-14 18:05:15 +08:00
// restore the batch node
_reusedTile->setBatchNode(this);
}
return _reusedTile;
}
// TMXLayer - obtaining tiles/gids
Sprite * TMXLayer::getTileAt(const Vector2& pos)
{
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");
Sprite *tile = nullptr;
2013-12-20 09:50:53 +08:00
int gid = this->getTileGIDAt(pos);
// if GID == 0, then no tile is present
if (gid)
{
int z = (int)(pos.x + pos.y * _layerSize.width);
tile = static_cast<Sprite*>(this->getChildByTag(z));
// tile not created yet. create it
if (! tile)
{
2014-02-08 10:33:30 +08:00
Rect rect = _tileSet->getRectForGID(gid);
rect = CC_RECT_PIXELS_TO_POINTS(rect);
2013-11-14 07:55:36 +08:00
tile = Sprite::createWithTexture(this->getTexture(), rect);
tile->setBatchNode(this);
tile->setPosition(getPositionAt(pos));
2014-03-14 15:38:43 +08:00
tile->setPositionZ((float)getVertexZForPos(pos));
tile->setAnchorPoint(Vector2::ZERO);
tile->setOpacity(_opacity);
2013-12-12 12:07:20 +08:00
ssize_t indexForZ = atlasIndexForExistantZ(z);
this->addSpriteWithoutQuad(tile, static_cast<int>(indexForZ), z);
}
}
return tile;
}
uint32_t TMXLayer::getTileGIDAt(const Vector2& pos, TMXTileFlags* flags/* = nullptr*/)
{
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-02-08 10:33:30 +08:00
ssize_t idx = static_cast<int>((pos.x + pos.y * _layerSize.width));
// 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];
// issue1264, flipped tiles can be changed dynamically
if (flags)
{
2014-02-08 10:33:30 +08:00
*flags = (TMXTileFlags)(tile & kTMXFlipedAll);
}
2014-02-08 10:33:30 +08:00
return (tile & kTMXFlippedMask);
}
// TMXLayer - adding helper methods
Sprite * TMXLayer::insertTileForGID(uint32_t gid, const Vector2& pos)
{
2014-02-08 10:33:30 +08:00
if (gid != 0 && (static_cast<int>((gid & kTMXFlippedMask)) - _tileSet->_firstGid) >= 0)
{
2014-02-08 10:33:30 +08:00
Rect rect = _tileSet->getRectForGID(gid);
rect = CC_RECT_PIXELS_TO_POINTS(rect);
intptr_t z = (intptr_t)(pos.x + pos.y * _layerSize.width);
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
for(const auto &child : _children) {
Sprite* sp = static_cast<Sprite*>(child);
ssize_t ai = sp->getAtlasIndex();
if ( ai >= indexForZ )
{
sp->setAtlasIndex(ai+1);
}
}
_tiles[z] = gid;
return tile;
}
return nullptr;
}
Sprite * TMXLayer::updateTileForGID(uint32_t gid, const Vector2& pos)
{
2014-02-08 10:33:30 +08:00
Rect rect = _tileSet->getRectForGID(gid);
rect = Rect(rect.origin.x / _contentScaleFactor, rect.origin.y / _contentScaleFactor, rect.size.width/ _contentScaleFactor, rect.size.height/ _contentScaleFactor);
int z = (int)(pos.x + pos.y * _layerSize.width);
Sprite *tile = reusedTileWithRect(rect);
setupTileSprite(tile ,pos ,gid);
// get atlas index
2013-12-12 12:07:20 +08:00
ssize_t indexForZ = atlasIndexForExistantZ(z);
tile->setAtlasIndex(indexForZ);
tile->setDirty(true);
tile->updateTransform();
_tiles[z] = gid;
return tile;
}
// used only when parsing the map. useless after the map was parsed
// since lot's of assumptions are no longer true
Sprite * TMXLayer::appendTileForGID(uint32_t gid, const Vector2& pos)
{
2014-02-08 10:33:30 +08:00
if (gid != 0 && (static_cast<int>((gid & kTMXFlippedMask)) - _tileSet->_firstGid) >= 0)
{
2014-02-08 10:33:30 +08:00
Rect rect = _tileSet->getRectForGID(gid);
rect = CC_RECT_PIXELS_TO_POINTS(rect);
intptr_t z = (intptr_t)(pos.x + pos.y * _layerSize.width);
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);
return tile;
}
return nullptr;
}
// TMXLayer - atlasIndex and Z
static inline int compareInts(const void * a, const void * b)
{
return ((*(int*)a) - (*(int*)b));
}
2013-12-20 09:50:53 +08:00
ssize_t TMXLayer::atlasIndexForExistantZ(int z)
{
int key=z;
int *item = (int*)bsearch((void*)&key, (void*)&_atlasIndexArray->arr[0], _atlasIndexArray->num, sizeof(void*), compareInts);
CCASSERT(item, "TMX atlas index not found. Shall not happen");
2013-12-12 12:07:20 +08:00
ssize_t index = ((size_t)item - (size_t)_atlasIndexArray->arr) / sizeof(void*);
return index;
}
2013-12-12 12:07:20 +08:00
ssize_t TMXLayer::atlasIndexForNewZ(int z)
{
// XXX: This can be improved with a sort of binary search
2013-12-12 12:07:20 +08:00
ssize_t i=0;
for (i=0; i< _atlasIndexArray->num ; i++)
{
2013-12-12 12:07:20 +08:00
ssize_t val = (size_t) _atlasIndexArray->arr[i];
if (z < val)
{
break;
}
}
return i;
}
// TMXLayer - adding / remove tiles
void TMXLayer::setTileGID(uint32_t gid, const Vector2& pos)
{
setTileGID(gid, pos, (TMXTileFlags)0);
}
void TMXLayer::setTileGID(uint32_t gid, const Vector2& pos, TMXTileFlags flags)
{
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" );
TMXTileFlags currentFlags;
2014-02-08 10:33:30 +08:00
uint32_t currentGID = getTileGIDAt(pos, &currentFlags);
if (currentGID != gid || currentFlags != flags)
{
2014-02-08 10:33:30 +08:00
uint32_t gidAndFlags = gid | flags;
// setting gid=0 is equal to remove the tile
if (gid == 0)
{
removeTileAt(pos);
}
// empty tile. create a new one
else if (currentGID == 0)
{
insertTileForGID(gidAndFlags, pos);
}
// modifying an existing tile with a non-empty tile
else
{
2013-12-20 09:50:53 +08:00
int z = pos.x + pos.y * _layerSize.width;
Sprite *sprite = static_cast<Sprite*>(getChildByTag(z));
if (sprite)
{
2014-02-08 10:33:30 +08:00
Rect rect = _tileSet->getRectForGID(gid);
rect = CC_RECT_PIXELS_TO_POINTS(rect);
sprite->setTextureRect(rect, false, rect.size);
if (flags)
{
setupTileSprite(sprite, sprite->getPosition(), gidAndFlags);
}
_tiles[z] = gidAndFlags;
}
else
{
updateTileForGID(gidAndFlags, pos);
}
}
}
}
void TMXLayer::addChild(Node * child, int zOrder, int tag)
{
CC_UNUSED_PARAM(child);
CC_UNUSED_PARAM(zOrder);
CC_UNUSED_PARAM(tag);
CCASSERT(0, "addChild: is not supported on TMXLayer. Instead use setTileGID:at:/tileAt:");
}
void TMXLayer::removeChild(Node* node, bool cleanup)
{
Sprite *sprite = (Sprite*)node;
// allows removing nil objects
if (! sprite)
{
return;
}
CCASSERT(_children.contains(sprite), "Tile does not belong to TMXLayer");
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];
_tiles[zz] = 0;
ccCArrayRemoveValueAtIndex(_atlasIndexArray, atlasIndex);
SpriteBatchNode::removeChild(sprite, cleanup);
}
void TMXLayer::removeTileAt(const Vector2& pos)
{
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");
2013-12-20 09:50:53 +08:00
int gid = getTileGIDAt(pos);
if (gid)
{
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);
// remove tile from GID map
_tiles[z] = 0;
// remove tile from atlas position array
ccCArrayRemoveValueAtIndex(_atlasIndexArray, atlasIndex);
// remove it from sprites and/or texture atlas
Sprite *sprite = (Sprite*)getChildByTag(z);
if (sprite)
{
SpriteBatchNode::removeChild(sprite, true);
}
else
{
_textureAtlas->removeQuadAtIndex(atlasIndex);
// update possible children
for(const auto &obj : _children) {
Sprite* child = static_cast<Sprite*>(obj);
ssize_t ai = child->getAtlasIndex();
if ( ai >= atlasIndex )
{
child->setAtlasIndex(ai-1);
}
}
}
}
}
//CCTMXLayer - obtaining positions, offset
Vector2 TMXLayer::calculateLayerOffset(const Vector2& pos)
{
Vector2 ret = Vector2::ZERO;
switch (_layerOrientation)
{
case TMXOrientationOrtho:
ret = Vector2( pos.x * _mapTileSize.width, -pos.y *_mapTileSize.height);
break;
case TMXOrientationIso:
ret = Vector2((_mapTileSize.width /2) * (pos.x - pos.y),
(_mapTileSize.height /2 ) * (-pos.x - pos.y));
break;
case TMXOrientationHex:
CCASSERT(pos.equals(Vector2::ZERO), "offset for hexagonal map not implemented yet");
break;
}
return ret;
}
Vector2 TMXLayer::getPositionAt(const Vector2& pos)
{
Vector2 ret = Vector2::ZERO;
switch (_layerOrientation)
{
case TMXOrientationOrtho:
ret = getPositionForOrthoAt(pos);
break;
case TMXOrientationIso:
ret = getPositionForIsoAt(pos);
break;
case TMXOrientationHex:
ret = getPositionForHexAt(pos);
break;
}
ret = CC_POINT_PIXELS_TO_POINTS( ret );
return ret;
}
Vector2 TMXLayer::getPositionForOrthoAt(const Vector2& pos)
{
return Vector2(pos.x * _mapTileSize.width,
(_layerSize.height - pos.y - 1) * _mapTileSize.height);
}
Vector2 TMXLayer::getPositionForIsoAt(const Vector2& pos)
{
return Vector2(_mapTileSize.width /2 * (_layerSize.width + pos.x - pos.y - 1),
_mapTileSize.height /2 * ((_layerSize.height * 2 - pos.x - pos.y) - 2));
}
Vector2 TMXLayer::getPositionForHexAt(const Vector2& pos)
{
float diffY = 0;
if ((int)pos.x % 2 == 1)
{
diffY = -_mapTileSize.height/2 ;
}
Vector2 xy = Vector2(pos.x * _mapTileSize.width*3/4,
(_layerSize.height - pos.y - 1) * _mapTileSize.height + diffY);
return xy;
}
int TMXLayer::getVertexZForPos(const Vector2& pos)
{
int ret = 0;
2013-12-20 09:50:53 +08:00
int maxVal = 0;
if (_useAutomaticVertexZ)
{
switch (_layerOrientation)
{
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)));
break;
case TMXOrientationOrtho:
2013-12-20 09:50:53 +08:00
ret = static_cast<int>(-(_layerSize.height-pos.y));
break;
case TMXOrientationHex:
CCASSERT(0, "TMX Hexa zOrder not supported");
break;
default:
CCASSERT(0, "TMX invalid value");
break;
}
}
else
{
ret = _vertexZvalue;
}
return ret;
}
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);
}
NS_CC_END