Dynamic Tilemap size changer

by paul b   Last Updated March 02, 2018 14:13 PM


Hey I have started coding this project using the library SFML a few months ago.

I have yet implemented a random map generation everytime I load the game thanks to Biome and the Perlin noise Algorithm.

game picture

I wish i could increase the TileMap size on the border of the map according to the player position (Chunk system in minecraft) .

I have managed to do it on the bottom and the right of the map : enter image description here

But this isn't dynamic. There is a snap of my code where i add a new "chunck" :

void TileMap::AddChunckToTileSet(sf::Vector2i PlayerTile, char * direction)
{
    int previousWidth = _width;
    int previousHeight = _height;


    _height = _height + 24;
    _width = _width + 24;
    m_vertices.resize(_width * _height * 4);


    int tileNumber = 384; // ToDO

    // on en déduit sa position dans la texture du tileset
    int tu = tileNumber % (m_tileset.getSize().x / _tileSize.x);
    int tv = tileNumber / (m_tileset.getSize().x / _tileSize.x);




    if (direction == "Down")
    {


        for (int i = previousWidth; i < _width; ++i)
        {
            for (int j = previousHeight; j < _height; ++j)
            {
                // on récupère un pointeur vers le quad à définir dans le tableau de vertex
                sf::Vertex* quad = &m_vertices[(i + j * _width) * 4];

                // on définit ses quatre coins
                quad[0].position = sf::Vector2f((i - previousWidth) + PlayerTile.x * _tileSize.x, j * _tileSize.y);
                quad[1].position = sf::Vector2f(((i - previousWidth) + PlayerTile.x + 1) * _tileSize.x, j * _tileSize.y);
                quad[2].position = sf::Vector2f(((i - previousWidth) + PlayerTile.x + 1) * _tileSize.x, (j + 1) * _tileSize.y);
                quad[3].position = sf::Vector2f((i - previousWidth) + PlayerTile.x * _tileSize.x, (j + 1) * _tileSize.y);


                // on définit ses quatre coordonnées de texture
                quad[0].texCoords = sf::Vector2f(tu * _tileSize.x, tv * _tileSize.y);
                quad[1].texCoords = sf::Vector2f((tu + 1) * _tileSize.x, tv * _tileSize.y);
                quad[2].texCoords = sf::Vector2f((tu + 1) * _tileSize.x, (tv + 1) * _tileSize.y);
                quad[3].texCoords = sf::Vector2f(tu * _tileSize.x, (tv + 1) * _tileSize.y);



            }
        }
    }
}

I call the function AddChunckToTileSet like this :

void TileMap::Update(sf::Vector2i PlayerTile)
{

    if (PlayerTile.y + 15 > _height)
    {
        // Increase map on Down according to our X
        printf("Add Chunck Down\n");
        AddChunckToTileSet(PlayerTile, "Down");
    }


}

Do you have any hints on how I could achieve this correctly ?



Related Questions


Short Question: How to make a noise gradient

Updated February 10, 2018 20:13 PM

How to make my Diamond-Square-algorithm less 'random'?

Updated December 03, 2016 08:05 AM


Game map for platformer megaman-style game

Updated September 09, 2018 22:13 PM