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.
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2013-2014 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.
|
|
|
|
****************************************************************************/
|
|
|
|
#include "CCTMXLayer.h"
|
|
|
|
#include "CCTMXXMLParser.h"
|
|
|
|
#include "CCTMXTiledMap.h"
|
2013-10-14 14:01:00 +08:00
|
|
|
#include "CCSprite.h"
|
|
|
|
#include "CCTextureCache.h"
|
|
|
|
#include "CCShaderCache.h"
|
|
|
|
#include "CCGLProgram.h"
|
|
|
|
#include "ccCArray.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
#include "CCDirector.h"
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
TMXLayer *ret = new TMXLayer();
|
|
|
|
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
|
|
|
}
|
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
|
|
|
{
|
|
|
|
// XXX: 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 )
|
|
|
|
{
|
2013-11-07 19:11:09 +08:00
|
|
|
texture = Director::getInstance()->getTextureCache()->addImage(tilesetInfo->_sourceImage.c_str());
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
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();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// offset (after layer orientation is set);
|
2013-06-20 14:13:12 +08:00
|
|
|
Point 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
|
|
|
|
2013-07-12 14:30:26 +08:00
|
|
|
this->setContentSize(CC_SIZE_PIXELS_TO_POINTS(Size(_layerSize.width * _mapTileSize.width, _layerSize.height * _mapTileSize.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)
|
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
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_DELETE_ARRAY(_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
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
delete [] _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
|
|
|
{
|
2013-12-23 10:37:46 +08:00
|
|
|
int pos = static_cast<int>(x + _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.*/
|
|
|
|
|
|
|
|
// XXX: gid == 0 --> empty tile
|
2012-08-20 14:06:29 +08:00
|
|
|
if (gid != 0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-12 14:11:55 +08:00
|
|
|
this->appendTileForGID(gid, Point(x, 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();
|
2013-09-16 20:38:03 +08:00
|
|
|
setShaderProgram(ShaderCache::getInstance()->getProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_ALPHA_TEST));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-25 17:48:22 +08:00
|
|
|
GLint alphaValueLocation = glGetUniformLocation(getShaderProgram()->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
|
|
|
|
getShaderProgram()->use();
|
2012-04-19 14:35:52 +08:00
|
|
|
getShaderProgram()->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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-20 09:50:53 +08:00
|
|
|
void TMXLayer::setupTileSprite(Sprite* sprite, Point pos, int 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));
|
2013-07-12 14:47:36 +08:00
|
|
|
sprite->setAnchorPoint(Point::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);
|
2013-07-12 14:11:55 +08:00
|
|
|
sprite->setAnchorPoint(Point(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.
|
2013-07-12 14:11:55 +08:00
|
|
|
sprite->setAnchorPoint(Point(0.5f,0.5f));
|
2013-07-19 14:57:54 +08:00
|
|
|
sprite->setPosition(Point(getPositionAt(pos).x + sprite->getContentSize().height/2,
|
|
|
|
getPositionAt(pos).y + sprite->getContentSize().width/2 ) );
|
2012-06-14 05:26:28 +08:00
|
|
|
|
2013-12-20 09:50:53 +08:00
|
|
|
int 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Sprite* TMXLayer::reusedTileWithRect(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
|
|
|
|
{
|
2012-11-14 18:05:15 +08:00
|
|
|
// XXX HACK: Needed because if "batch node" is nil,
|
|
|
|
// 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
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
// 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
|
2013-07-19 14:57:54 +08:00
|
|
|
Sprite * TMXLayer::getTileAt(const Point& 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));
|
2013-07-12 14:47:36 +08:00
|
|
|
tile->setAnchorPoint(Point::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-02-08 10:33:30 +08:00
|
|
|
uint32_t TMXLayer::getTileGIDAt(const Point& 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
|
|
|
|
2014-02-08 10:33:30 +08:00
|
|
|
ssize_t idx = static_cast<int>((pos.x + 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-02-08 10:33:30 +08:00
|
|
|
Sprite * TMXLayer::insertTileForGID(uint32_t gid, const Point& 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);
|
|
|
|
|
|
|
|
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
|
|
|
|
|
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-02-08 10:33:30 +08:00
|
|
|
Sprite * TMXLayer::updateTileForGID(uint32_t gid, const Point& 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);
|
2013-06-15 14:03:30 +08:00
|
|
|
int z = (int)(pos.x + 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// used only when parsing the map. useless after the map was parsed
|
|
|
|
// since lot's of assumptions are no longer true
|
2014-02-08 10:33:30 +08:00
|
|
|
Sprite * TMXLayer::appendTileForGID(uint32_t gid, const Point& 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);
|
|
|
|
|
|
|
|
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;
|
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)
|
|
|
|
{
|
2012-08-20 14:06:29 +08:00
|
|
|
return ((*(int*)a) - (*(int*)b));
|
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
|
|
|
{
|
|
|
|
// XXX: 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-02-08 10:33:30 +08:00
|
|
|
void TMXLayer::setTileGID(uint32_t gid, const Point& 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-02-08 10:33:30 +08:00
|
|
|
void TMXLayer::setTileGID(uint32_t gid, const Point& 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
|
|
|
|
{
|
2013-12-20 09:50:53 +08:00
|
|
|
int z = pos.x + 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
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void TMXLayer::addChild(Node * child, int zOrder, int tag)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
CC_UNUSED_PARAM(child);
|
|
|
|
CC_UNUSED_PARAM(zOrder);
|
|
|
|
CC_UNUSED_PARAM(tag);
|
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
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void TMXLayer::removeTileAt(const Point& 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
|
2013-06-20 14:13:12 +08:00
|
|
|
Point TMXLayer::calculateLayerOffset(const Point& pos)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-12 14:47:36 +08:00
|
|
|
Point ret = Point::ZERO;
|
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-12 14:11:55 +08:00
|
|
|
ret = Point( pos.x * _mapTileSize.width, -pos.y *_mapTileSize.height);
|
2012-04-19 14:35:52 +08:00
|
|
|
break;
|
2013-06-20 14:13:12 +08:00
|
|
|
case TMXOrientationIso:
|
2013-07-12 14:11:55 +08:00
|
|
|
ret = Point((_mapTileSize.width /2) * (pos.x - pos.y),
|
2013-06-15 14:03:30 +08:00
|
|
|
(_mapTileSize.height /2 ) * (-pos.x - pos.y));
|
2012-04-19 14:35:52 +08:00
|
|
|
break;
|
2013-06-20 14:13:12 +08:00
|
|
|
case TMXOrientationHex:
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(pos.equals(Point::ZERO), "offset for hexagonal map not implemented yet");
|
2012-04-19 14:35:52 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2013-07-19 14:57:54 +08:00
|
|
|
|
|
|
|
Point TMXLayer::getPositionAt(const Point& pos)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-12 14:47:36 +08:00
|
|
|
Point ret = Point::ZERO;
|
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;
|
|
|
|
}
|
|
|
|
ret = CC_POINT_PIXELS_TO_POINTS( ret );
|
|
|
|
return ret;
|
|
|
|
}
|
2013-07-19 14:57:54 +08:00
|
|
|
|
|
|
|
Point TMXLayer::getPositionForOrthoAt(const Point& pos)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-19 14:57:54 +08:00
|
|
|
return Point(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
|
|
|
|
|
|
|
Point TMXLayer::getPositionForIsoAt(const Point& pos)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-19 14:57:54 +08:00
|
|
|
return Point(_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
|
|
|
|
|
|
|
Point TMXLayer::getPositionForHexAt(const Point& pos)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
float diffY = 0;
|
2012-08-20 14:06:29 +08:00
|
|
|
if ((int)pos.x % 2 == 1)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
diffY = -_mapTileSize.height/2 ;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-07-12 14:30:26 +08:00
|
|
|
Point xy = Point(pos.x * _mapTileSize.width*3/4,
|
2013-06-15 14:03:30 +08:00
|
|
|
(_layerSize.height - pos.y - 1) * _mapTileSize.height + diffY);
|
2012-04-19 14:35:52 +08:00
|
|
|
return xy;
|
|
|
|
}
|
2013-07-19 14:57:54 +08:00
|
|
|
|
|
|
|
int TMXLayer::getVertexZForPos(const Point& 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;
|
2013-06-20 14:13:12 +08:00
|
|
|
case TMXOrientationHex:
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(0, "TMX Hexa zOrder not supported");
|
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
|
|
|
|
|