Improve path generation
This commit is contained in:
parent
fae8959ef2
commit
a9c6ec5145
23
src/Map.cpp
23
src/Map.cpp
@ -11,6 +11,7 @@ Map::Map(GameRenderer *renderer, int tile_size) :
|
||||
grass_sprite = renderer->load_sprite("./assets/grass_tile.png", tile_size, tile_size);
|
||||
path_sprite = renderer->load_sprite("./assets/path_tile.png", tile_size, tile_size);
|
||||
|
||||
// Create horizontal starting path
|
||||
int y = renderer->screen_height / tile_size / 2;
|
||||
for (int x = 0; x < tiles.size(); x++) {
|
||||
if (rand() % 2 == 0) y += rand() % 3 - 1;
|
||||
@ -18,6 +19,7 @@ Map::Map(GameRenderer *renderer, int tile_size) :
|
||||
tiles[x][y] = path;
|
||||
}
|
||||
|
||||
// Create vertical starting path
|
||||
int x = renderer->screen_width / tile_size / 2;
|
||||
for (int y = 0; y < tiles[x].size(); y++) {
|
||||
if (rand() % 2 == 0) x += rand() % 3 - 1;
|
||||
@ -28,24 +30,17 @@ Map::Map(GameRenderer *renderer, int tile_size) :
|
||||
|
||||
std::vector<Tile> Map::generate_tiles(std::vector<Tile> prev_tiles) const
|
||||
{
|
||||
std::vector<Tile> new_tiles;
|
||||
std::vector<Tile> new_tiles = std::vector(prev_tiles.size(), grass);
|
||||
|
||||
int path_idx = 0;
|
||||
for (int i = 0; i < prev_tiles.size(); i++) {
|
||||
Tile new_tile = grass;
|
||||
|
||||
if (
|
||||
(i > 0 && prev_tiles[i-1] == path) ||
|
||||
prev_tiles[i] == path ||
|
||||
(i < prev_tiles.size() - 1 && prev_tiles[i+1]) == path
|
||||
) {
|
||||
if (rand() % 2 == 0) {
|
||||
new_tile = path;
|
||||
}
|
||||
}
|
||||
|
||||
new_tiles.push_back(new_tile);
|
||||
if (prev_tiles[i] == path) path_idx = i;
|
||||
}
|
||||
|
||||
if (rand() % 2 == 0) path_idx += rand() % 3 - 1;
|
||||
|
||||
new_tiles[path_idx] = path;
|
||||
|
||||
return new_tiles;
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user