Level Generator Help

Okay so I dont really know how to explain this but I'll try.

I currently have a platformer game with hardcoded platforms in specific places.

I want something like a level generator with 0 representing no platform and 1 platform.

Struct?
1,0,0,0,0,1,1,1,0,1
1,0,0,0,1,1,1,0,0,1
1,1,1,1,1,0,0,0,0,1

and so on

Then have a function that will search this struct or list or something for thoes 1's and 0's and will create a platform for 1 and nothing for 0.

All I need is to know how to even search for something like this on the internet since im not finding anything that I need.
I dont really know how to explain

A proverb says, if you can't tell in words you did not sufficiently think through it.

In case it's only about storing a lot of 0s and 1s, try bitset -- http://www.cplusplus.com/reference/bitset/bitset/
You won't, because that isn't how people do it.

Use a level-generator program to create the level you want.
Then save it to a level file.
Then include the level file's data in your code (or in your program's assets folder).
Presumably your code has a function to read a level from file/stream, right?
Last edited on
I currently have a platformer game with hardcoded platforms in specific places.
And how you currently storing those hardcoded platforms?

Edit: For example, you might have a list/array of platforms or something like that. But with load-from-a-file changes, you can still have this list of platforms, but you should set the values of those platforms based off the values in a file.
Note that data containers like std::vector are resizable at run-time, unlike C arrays.
Last edited on
I do indeed have a data container

std::vector<Platform> platforms;

and then

platforms.push_back(Platform(nullptr, sf::Vector2f(900.0f, 25.0f), sf::Vector2f(450.0f, 890.0f)));

With Texture, size and position argument
Okay good, so from there, you can hopefully imagine doing something like this:

1
2
3
4
5
6
7
8
9
10
std::vector<Platform> platforms;

std::ifstream fin("level.txt");

sf::Vector2f size;
sf::Vector2f position;
while (fin >> size.x >> size.y >> position.x >> position.y) // extract information from file
{
    platforms.push_back(Platform(nullptr, size, position));
}


And your level.txt might look like this:
900.0 25.0 450.0 890.0
123.0 456.0 789.0 1000.0

(two platforms)

That's one way to do it, and it would be a good start. But it sounds like you want to store the entire map in a file in a different way, where you have a tile-based map?

If that's the case, then you'd loop through your file in a different way.

You could do something like this:

level_grid.txt
7 3
0 0 0 0 0 0 1
0 1 1 0 1 1 1
1 1 0 0 0 0 0


I put the width and height of the map in there for easier parsing (imo).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

#include <iostream>
#include <string>
#include <vector>
#include <fstream>

/// BEGIN BOILERPLATE -- DON'T COPY THIS ///

struct Texture { };

namespace sf {
    struct Vector2f {
        Vector2f(float x, float y) { }
    };
}

struct Platform {
    Platform(Texture* texture, const sf::Vector2f& size, const sf::Vector2f& pos)
    {
        // ...
    }
};

/// END BOILERPLATE ///

int main()
{
    std::vector<Platform> platforms;
   
    /*
        7 3
        0 0 0 0 0 0 1
        0 1 1 0 1 1 1
        1 1 0 0 0 0 0
    */
    std::ifstream fin("level_grid.txt");
    
    int width;
    int height;
    
    fin >> width >> height;


    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {
            char tile;
            fin >> tile;
            
            if (tile == '1')
            {
                sf::Vector2f size(1.0, 1.0); // ???
                sf::Vector2f position(x, y); // idk if this is what you want
                platforms.push_back(Platform(nullptr, size, position));

                std::cout << "platform at (" << x << ", " << y << ")\n";
            }
        }
    }

}


If you wanted to combine multiple horizontal 1s into one platform, or something like that, you'd have to do the logic for that as well.
Last edited on
Topic archived. No new replies allowed.