Beispiel #1
0
void TileLayer::parseElement(tinyxml2::XMLElement* layerElement, Map* map)
{
	// Get properties
	auto properties = layerElement->FirstChildElement("properties");
	if(properties != 0)
		parseProperties(properties);

	m_name = layerElement->Attribute("name");
	m_width = layerElement->UnsignedAttribute("width");
	m_height = layerElement->UnsignedAttribute("height");

	// Parse data
	unsigned int x = 0;
	unsigned int y = 0;
	auto tile = layerElement->FirstChildElement("data")->FirstChildElement("tile");
	while(tile != 0)
	{
		unsigned int gid = tile->UnsignedAttribute("gid");

		// Read out the flags
		bool flipped_horizontally = (gid & FLIPPED_HORIZONTALLY_FLAG) != 0;
		bool flipped_vertically = (gid & FLIPPED_VERTICALLY_FLAG) != 0;
        // TODO: implement this
		//bool flipped_diagonally = (gid & FLIPPED_DIAGONALLY_FLAG) != 0;

		// Clear the flags
		gid &= ~(FLIPPED_HORIZONTALLY_FLAG | FLIPPED_VERTICALLY_FLAG
			| FLIPPED_DIAGONALLY_FLAG);

		// Find correct tileset
		for(int i = map->tilesets.size() - 1; i >= 0; --i)
		{
			Tileset* tileset = map->tilesets[i];

			if(tileset->GetFirstGID() <= gid)
			{
				if(x >= map->GetWidth())
				{
					x = 0;
					y++;
				}

				const pmath::Rect t(static_cast<float>(x * tileset->GetTileWidth()), static_cast<float>(y * tileset->GetTileHeight()),
					static_cast<float>(tileset->GetTileWidth()), static_cast<float>(tileset->GetTileHeight()));

				auto tile = new Tile(t);

				if(flipped_horizontally)
					tile->transform.SetScale(-1, tile->transform.GetScale().y);
				if(flipped_vertically)
                    tile->transform.SetScale(tile->transform.GetScale().x, -1);

				tile->parent = static_cast<GameObject*>(map);
				const pmath::Rect texCoords = tileset->GetTile(gid - tileset->GetFirstGID());

				m_tiles.push_back(tile);
				m_spriteBatches.at(tileset->GetTexture())->AddSprite(&tile->transform, "", pmath::Vec4(1,1,1,1),  texCoords);
				x++;
				break;
			}
		}
		tile = tile->NextSiblingElement("tile");
	}
}
void TileLayer::parseElement(tinyxml2::XMLElement* layerElement, Map* map)
{
	// Get properties
	auto properties = layerElement->FirstChildElement("properties");
	if(properties != 0)
		parseProperties(properties);

	m_name = layerElement->Attribute("name");
	m_width = layerElement->UnsignedAttribute("width");
	m_height = layerElement->UnsignedAttribute("height");
    if (layerElement->Attribute("visible") != 0)
    {
        m_active = layerElement->IntAttribute("visible") == 0 ? false : true;
    }
    else
        m_active = true;

	// Parse data
	unsigned int x = 0;
	unsigned int y = 0;
	auto tile = layerElement->FirstChildElement("data")->FirstChildElement("tile");
	while(tile != 0)
	{
		unsigned int gid = tile->UnsignedAttribute("gid");

        // Go to the next "line"
        if (x >= map->GetWidth())
        {
            x = 0;
            y++;
        }

        // Tile is "empty space"
        if (gid == 0)
        {
            x++;
            tile = tile->NextSiblingElement("tile");
            continue;
        }

		// Read out the flags
		const bool flipped_horizontally = (gid & FLIPPED_HORIZONTALLY_FLAG) != 0;
		const bool flipped_vertically = (gid & FLIPPED_VERTICALLY_FLAG) != 0;
		const bool flipped_diagonally = (gid & FLIPPED_DIAGONALLY_FLAG) != 0;

		// Clear the flags
		gid &= ~(FLIPPED_HORIZONTALLY_FLAG | FLIPPED_VERTICALLY_FLAG
			| FLIPPED_DIAGONALLY_FLAG);

		// Find correct tileset
		for(int i = map->tilesets.size() - 1; i >= 0; --i)
		{
			Tileset* tileset = map->tilesets[i].get();

            if (tileset->GetFirstGID() <= gid)
            {
                const pmath::Rect t(static_cast<float>(x * tileset->GetTileWidth()), static_cast<float>(y * tileset->GetTileHeight()),
                    static_cast<float>(tileset->GetTileWidth()), static_cast<float>(tileset->GetTileHeight()));

                auto tile = new Tile(t);

                if (flipped_diagonally)
                {
                    if (flipped_horizontally && flipped_vertically)
                    {
                        tile->transform.Rotate(90);
                        tile->transform.SetScale(-1, tile->transform.GetScale().y);
                    }
                    else if (flipped_horizontally)
                    {
                        tile->transform.Rotate(90);
                    }
                    else if (flipped_vertically)
                    {
                        tile->transform.Rotate(-90);
                    }
                    else
                    {
                        tile->transform.Rotate(-90);
                        tile->transform.SetScale(-1, tile->transform.GetScale().y);
                    }
                        
                }
                else
                {
                    if (flipped_horizontally)
                        tile->transform.SetScale(-1, tile->transform.GetScale().y);
                    if (flipped_vertically)
                        tile->transform.SetScale(tile->transform.GetScale().x, -1);
                }

				map->AddChild(tile);
                const pmath::Rect texCoords = tileset->GetTile(gid - tileset->GetFirstGID());

                m_tiles.push_back(tile);
                m_spriteBatches.at(tileset->GetTexture())->AddSprite(&tile->transform, "", pmath::Vec4(1, 1, 1, 1), texCoords);
                x++;
                break;
            }
		}
		tile = tile->NextSiblingElement("tile");
	}
}