// 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; }
void CCTMXLayer::setupTileSprite(CCSprite* sprite, CCPoint pos, unsigned int gid, int offsetX, int offsetY) { CCPoint sppos = positionAt(pos); sppos.x -= offsetX; sppos.y -= offsetY; sprite->setPosition(sppos); sprite->setVertexZ((float)vertexZForPos(pos)); sprite->setAnchorPoint(CCPointZero); sprite->setOpacity(m_cOpacity); //issue 1264, flip can be undone as well sprite->setFlipX(false); sprite->setFlipY(false); sprite->setRotation(0.0f); sprite->setAnchorPoint(ccp(0,0)); // Rotation in tiled is achieved using 3 flipped states, flipping across the horizontal, vertical, and diagonal axes of the tiles. if (gid & kCCTMXTileDiagonalFlag) { // put the anchor in the middle for ease of rotation. sprite->setAnchorPoint(ccp(0.5f,0.5f)); sprite->setPosition(ccp(sppos.x + sprite->getContentSize().height/2, sppos.y + sprite->getContentSize().width/2 ) ); unsigned int flag = gid & (kCCTMXTileHorizontalFlag | kCCTMXTileVerticalFlag ); // handle the 4 diagonally flipped states. if (flag == kCCTMXTileHorizontalFlag) { sprite->setRotation(90.0f); } else if (flag == kCCTMXTileVerticalFlag) { sprite->setRotation(270.0f); } else if (flag == (kCCTMXTileVerticalFlag | kCCTMXTileHorizontalFlag) ) { sprite->setRotation(90.0f); sprite->setFlipX(true); } else { sprite->setRotation(270.0f); sprite->setFlipX(true); } } else { if (gid & kCCTMXTileHorizontalFlag) { sprite->setFlipX(true); } if (gid & kCCTMXTileVerticalFlag) { sprite->setFlipY(true); } } }
// CCTMXLayer - adding helper methods CCSprite * CCTMXLayer::insertTileForGID(unsigned int gid, const CCPoint& pos) { 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); int z = (int)(pos.x + pos.y * m_tLayerSize.width); if( ! m_pReusedTile ) { m_pReusedTile = new CCSprite(); m_pReusedTile->initWithBatchNode(this, rect); } else { m_pReusedTile->initWithBatchNode(this, rect); } m_pReusedTile->setPositionInPixels(positionAt(pos)); m_pReusedTile->setVertexZ((float)vertexZForPos(pos)); m_pReusedTile->setAnchorPoint(CCPointZero); m_pReusedTile->setOpacity(m_cOpacity); // get atlas index unsigned int indexForZ = atlasIndexForNewZ(z); // Optimization: add the quad without adding a child this->addQuadFromSprite(m_pReusedTile, indexForZ); // insert it into the local atlasindex array ccCArrayInsertValueAtIndex(m_pAtlasIndexArray, (void*)z, indexForZ); // update possible children if (m_pChildren && m_pChildren->count()>0) { CCObject* pObject = NULL; CCARRAY_FOREACH(m_pChildren, pObject) { CCSprite* pChild = (CCSprite*) pObject; if (pChild) { unsigned int ai = pChild->getAtlasIndex(); if ( ai >= indexForZ ) { pChild->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; }