std::set<uint32>* MapBuilder::getTileList(uint32 mapID)
    {
        TileList::iterator itr = std::find(m_tiles.begin(), m_tiles.end(), mapID);
        if (itr != m_tiles.end())
            return (*itr).m_tiles;

        std::set<uint32>* tiles = new std::set<uint32>();
        m_tiles.emplace_back(MapTiles(mapID, tiles));
        return tiles;
    }
    void MapBuilder::discoverTiles()
    {
        std::vector<std::string> files;
        uint32 mapID, tileX, tileY, tileID, count = 0;

        printf("Discovering maps... ");
        getDirContents(files, "maps");
        for (uint32 i = 0; i < files.size(); ++i)
        {
            mapID = uint32(atoi(files[i].substr(0, 4).c_str()));
            if (std::find(m_tiles.begin(), m_tiles.end(), mapID) == m_tiles.end())
            {
                m_tiles.emplace_back(MapTiles(mapID, new std::set<uint32>));
                count++;
            }
        }

        files.clear();
        getDirContents(files, "vmaps", "*.vmtree");
        for (uint32 i = 0; i < files.size(); ++i)
        {
            mapID = uint32(atoi(files[i].substr(0, 4).c_str()));
            if (std::find(m_tiles.begin(), m_tiles.end(), mapID) == m_tiles.end())
            {
                m_tiles.emplace_back(MapTiles(mapID, new std::set<uint32>));
                count++;
            }
        }
        printf("found %u.\n", count);

        count = 0;
        printf("Discovering tiles... ");
        for (TileList::iterator itr = m_tiles.begin(); itr != m_tiles.end(); ++itr)
        {
            std::set<uint32>* tiles = (*itr).m_tiles;
            mapID = (*itr).m_mapId;

            files.clear();
            getDirContents(files, "vmaps", Trinity::StringFormat("%04u*.vmtile", mapID));
            for (uint32 i = 0; i < files.size(); ++i)
            {
                tileX = uint32(atoi(files[i].substr(8, 2).c_str()));
                tileY = uint32(atoi(files[i].substr(5, 2).c_str()));
                tileID = StaticMapTree::packTileID(tileY, tileX);

                tiles->insert(tileID);
                count++;
            }

            files.clear();
            getDirContents(files, "maps", Trinity::StringFormat("%04u*", mapID));
            for (uint32 i = 0; i < files.size(); ++i)
            {
                tileY = uint32(atoi(files[i].substr(5, 2).c_str()));
                tileX = uint32(atoi(files[i].substr(8, 2).c_str()));
                tileID = StaticMapTree::packTileID(tileX, tileY);

                if (tiles->insert(tileID).second)
                    count++;
            }
        }
        printf("found %u.\n\n", count);
    }
Beispiel #3
0
int main()
{
	sf::RenderWindow AppWindow(sf::VideoMode(g_WndWidth, g_WndHeight), "Mario by Roman P.");

	sf::Texture tx;
	tx.loadFromFile("..\\Data\\Mario_tileset.png");
	//tx.loadFromFile("..\\Data\\fang.png");

	sf::Sprite MapTiles(tx);

	float fCurrentFrame = 0.0f;

	Player player(tx);
	Enemy  enemy;
	enemy.Set(tx, 750, 208);

	sf::SoundBuffer sb;
	sb.loadFromFile("..\\Data\\Jump.ogg");
	sf::Sound soundJump(sb);

	sf::Music music;
	music.openFromFile("..\\Data\\Mario_Theme.ogg");
	music.setLoop(true);
	music.play();

	sf::Clock clock;

	//sf::RectangleShape RecShape(sf::Vector2f(SIZE_TILE, SIZE_TILE));

	g_fOffsetY = player.FRect.top - g_WndHeight/ 2;

	while (AppWindow.isOpen())
	{ 
		float time = (float)clock.getElapsedTime().asMicroseconds();
		clock.restart();

		time = time / 700;

		sf::Event ev;
		while (AppWindow.pollEvent(ev))
		{
			if (ev.type == sf::Event::Closed)
				AppWindow.close();
		}

		if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
		{ 
			player.dx = -0.1f;
		}
		if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
		{
			player.dx = 0.1f;
		}
		if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
		{
			if (player.bOnGround)
			{
				player.dy = -0.25f;
				player.bOnGround = false;
				soundJump.play();
			}
		}

		player.Update(time);
		enemy.Update(time);

		if (player.FRect.intersects(enemy.FRect))
		{
			if (enemy.bLife)
			{
				if (player.dy > 0)
				{ 
					enemy.dx = 0;
					player.dy = -0.2f;
					enemy.bLife = false;
				}
				else
					player.SSprite.setColor(sf::Color::Red);
			}

			//player.SSprite.setColor(sf::Color::Red);
		}

		if (player.FRect.left > g_WndWidth / 2)
			g_fOffsetX = player.FRect.left - g_WndWidth/2;
		//if (player.FRect.top > g_WndHeight/ 2)
		//	g_fOffsetY = player.FRect.top - g_WndHeight / 2;
		
		AppWindow.clear(sf::Color(107, 140, 255, 255));

		// load map
		for (int i = 0; i < H; i++)
		{
			for (int j = 0; j < W; j++)
			{
				if (TileMap[i][j] == 'P')
					MapTiles.setTextureRect(sf::IntRect(96, 112, SIZE_TILE, SIZE_TILE));
				if (TileMap[i][j] == 'r')
					MapTiles.setTextureRect(sf::IntRect(112, 112, SIZE_TILE, SIZE_TILE));
				if (TileMap[i][j] == 'k')
					MapTiles.setTextureRect(sf::IntRect(144, 112, SIZE_TILE, SIZE_TILE));
				if (TileMap[i][j] == 'c')
					MapTiles.setTextureRect(sf::IntRect(127, 112, SIZE_TILE, SIZE_TILE));
				if (TileMap[i][j] == 'd')
					MapTiles.setTextureRect(sf::IntRect(0, 106, 74, 127 - 106));
				if (TileMap[i][j] == 'g')
					MapTiles.setTextureRect(sf::IntRect(0,16*9-5,3*16,16*2+5));
				if (TileMap[i][j] == 'G')  
					MapTiles.setTextureRect(sf::IntRect(145, 222, 222 - 145, 255 - 222));
				if (TileMap[i][j] == 't')  
					MapTiles.setTextureRect(sf::IntRect(0, 47, 32, 95 - 47));
				if (TileMap[i][j] == 'w') 
					MapTiles.setTextureRect(sf::IntRect(99, 224, 140 - 99, 255 - 224));
				if (TileMap[i][j] == 'h')
					MapTiles.setTextureRect(sf::IntRect(96, 6, 108, 106));
				if (TileMap[i][j] == ' ' || TileMap[i][j] == '0')
					continue;

				MapTiles.setPosition(j * SIZE_TILE - g_fOffsetX, i * SIZE_TILE - g_fOffsetY);
				AppWindow.draw(MapTiles);
			}
		}

		AppWindow.draw(player.SSprite);
		AppWindow.draw(enemy.SSprite);
		AppWindow.display();
	}

	return 0;
}