// CCTMXLayer - obtaining tiles/gids CCSprite * CCTMXLayer::tileAt(const CCPoint& pos) { CCAssert( pos.x < m_tLayerSize.width && pos.y < m_tLayerSize.height && pos.x >=0 && pos.y >=0, "TMXLayer: invalid position"); CCAssert( m_pTiles && m_pAtlasIndexArray, "TMXLayer: the tiles map has been released"); CCSprite *tile = NULL; unsigned int gid = this->tileGIDAt(pos); // if GID == 0, then no tile is present if( gid ) { int z = (int)(pos.x + pos.y * m_tLayerSize.width); tile = (CCSprite*) this->getChildByTag(z); // tile not created yet. create it if( ! tile ) { CCRect rect = m_pTileSet->rectForGID(gid); rect = CCRectMake(rect.origin.x / m_fContentScaleFactor, rect.origin.y / m_fContentScaleFactor, rect.size.width/ m_fContentScaleFactor, rect.size.height/ m_fContentScaleFactor); tile = new CCSprite(); tile->initWithBatchNode(this, rect); tile->setPosition(positionAt(pos)); tile->setVertexZ((float)vertexZForPos(pos)); tile->setAnchorPoint(CCPointZero); tile->setOpacity(m_cOpacity); unsigned int indexForZ = atlasIndexForExistantZ(z); this->addSpriteWithoutQuad(tile, indexForZ, z); tile->release(); } } return tile; }
// TMXLayer - obtaining tiles/gids Sprite * TMXLayer::getTileAt(const Point& 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; 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) { Rect rect = _tileSet->rectForGID(gid); rect = CC_RECT_PIXELS_TO_POINTS(rect); tile = Sprite::createWithTexture(this->getTexture(), rect); tile->setBatchNode(this); tile->setPosition(getPositionAt(pos)); tile->setVertexZ((float)getVertexZForPos(pos)); tile->setAnchorPoint(Point::ZERO); tile->setOpacity(_opacity); ssize_t indexForZ = atlasIndexForExistantZ(z); this->addSpriteWithoutQuad(tile, static_cast<int>(indexForZ), z); } } return tile; }
Sprite * TMXLayer::updateTileForGID(int gid, const Point& pos) { Rect rect = _tileSet->rectForGID(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 ssize_t indexForZ = atlasIndexForExistantZ(z); tile->setAtlasIndex(indexForZ); tile->setDirty(true); tile->updateTransform(); _tiles[z] = gid; return tile; }
void TMXLayer::removeTileAt(const Point& 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"); int gid = getTileGIDAt(pos); if (gid) { int z = pos.x + pos.y * _layerSize.width; 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 tiles/gids CCSprite * CCTMXLayer::tileAt(const CCPoint& pos) { CCAssert(pos.x < m_tLayerSize.width && pos.y < m_tLayerSize.height && pos.x >=0 && pos.y >=0, "TMXLayer: invalid position"); CCAssert(m_pTiles, "TMXLayer: the tiles map has been released"); CCSprite *tile = NULL; unsigned int gid = this->tileGIDAt(pos); // if GID == 0, then no tile is present if (gid) { int z = (int)(pos.x + pos.y * m_tLayerSize.width); tile = (CCSprite*) this->getChildByTag(z); // tile not created yet. create it if (! tile) { // when not using quad optimization, tile must exist CCAssert(m_pAtlasIndexArray, "Quad optimization but no m_pAtlasIndexArray"); CCRect rect = m_pTileSet->rectForGID(gid); rect = CC_RECT_PIXELS_TO_POINTS(rect); tile = new CCSprite(); tile->initWithTexture(this->getTexture(), rect); tile->setBatchNode(this); tile->setPosition(positionAt(pos)); tile->setVertexZ((float)vertexZForPos(pos)); tile->setAnchorPoint(CCPointZero); tile->setOpacity(m_cOpacity); unsigned int indexForZ = atlasIndexForExistantZ(z); this->addSpriteWithoutQuad(tile, indexForZ, z); tile->release(); } } return tile; }