Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Tiled map with vectors  (Read 1158 times)

0 Members and 1 Guest are viewing this topic.

Jeckie

  • Newbie
  • *
  • Posts: 22
    • View Profile
    • Email
Tiled map with vectors
« on: September 03, 2013, 04:30:09 pm »
I made this tiled map with vectors.
First I made vectors for 4x4 map:
        std::vector< std::vector<int> > map(4, std::vector<int>(4));
    // Define the map
    map[0][0] = 0;
    map[0][1] = 1;
    map[0][2] = 0;
    map[0][3] = 0;
    map[1][0] = 0;
    map[1][1] = 0;
    map[1][2] = 0;
    map[1][3] = 0;
    map[2][0] = 0;
    map[2][1] = 0;
    map[2][2] = 0;
    map[2][3] = 0;
    map[3][0] = 0;
    map[3][1] = 0;
    map[3][2] = 0;
    map[3][3] = 1;
 
Then I made sprites for grass and water:
        std::vector<sf::Sprite> tiles(2);
        tiles[0] = sf::Sprite(grass);
        tiles[1] = sf::Sprite(water);
And then draw it:
                        for (int x = 0; x < map.size(); x++)
                        {
            for (int y = 0; y < map[x].size(); y++)
                        {
                int tileId = map[x][y];

                                tiles[tileId].setPosition(x * 100, y * 100);
                                window.draw(tiles[tileId]);
            }
        }
I also added a rectangle and movement.
Now I don't know how to acces water tiles and add collision to them.
Sorry for my english.
Reply if you need full code.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Tiled map with vectors
« Reply #1 on: September 03, 2013, 09:06:29 pm »
Since you're asking about collision I *assume* you have a character controlled by user input somewhere.  Handling collision would be part of processing the user input that makes him move.  Instead of always moving him, check if he'd move into a water tile and only move him if he won't.

Gobbles

  • Full Member
  • ***
  • Posts: 132
    • View Profile
    • Email
Re: Tiled map with vectors
« Reply #2 on: September 04, 2013, 09:51:41 pm »
Also depends on what kind of movement you are doing. Is moving around tile to tile? If so then you just need to check the tile in the direction you are moving.

If you plan on having free unrestrained movement, then your going to need to get a little tricker. You could do something along the lines of: Check to see what location your player is going to end up, is that new spot on impassable block (like water)? If so then don't move, otherwise apply the move.