Implement a very simple map file format for further testing
This commit is contained in:
parent
124116a8bf
commit
144d620192
5 changed files with 170 additions and 20 deletions
104
resources/map/test.map
Normal file
104
resources/map/test.map
Normal file
|
@ -0,0 +1,104 @@
|
||||||
|
32 32 2
|
||||||
|
|
||||||
|
G 0 grass
|
||||||
|
< 0 road_left
|
||||||
|
> 0 road_right
|
||||||
|
|
||||||
|
11111111111111111111111111111111
|
||||||
|
11111111111111111111111111111111
|
||||||
|
11111111111111111111111111111111
|
||||||
|
11111111111111111111111111111111
|
||||||
|
11111111111111111111111111111111
|
||||||
|
11111111111111111111111111111111
|
||||||
|
11111111111111111111111111111111
|
||||||
|
11111111111111111111111111111111
|
||||||
|
11111111111111111111111111111111
|
||||||
|
11111111111111111111111111111111
|
||||||
|
11111111111111111111111111111111
|
||||||
|
11111111111111111111111111111111
|
||||||
|
11111111111111111111111111111111
|
||||||
|
11111111111111111111111111111111
|
||||||
|
11111111111111111111111111111111
|
||||||
|
11111111111111111111111111111111
|
||||||
|
11111111111111111111111111111111
|
||||||
|
11111111111111111111111111111111
|
||||||
|
11111111111111111111111111111111
|
||||||
|
11111111111111111111111111111111
|
||||||
|
11111111111111111111111111111111
|
||||||
|
11111111111111111111111111111111
|
||||||
|
11111111111111111111111111111111
|
||||||
|
11111111111111111111111111111111
|
||||||
|
11111111111111111111111111111111
|
||||||
|
11111111111111111111111111111111
|
||||||
|
11111111111111111111111111111111
|
||||||
|
11111111111111111111111111111111
|
||||||
|
11111111111111111111111111111111
|
||||||
|
11111111111111111111111111111111
|
||||||
|
11111111111111111111111111111111
|
||||||
|
11111111111111111111111111111111
|
||||||
|
|
||||||
|
GGGGGGGGGGG<>GGGGGGGGGGGGGGGGGGG
|
||||||
|
GGGGGGGGGGG<>GGGGGGGGGGGGGGGGGGG
|
||||||
|
GGGGGGGGGGG<>GGGGGGGGGGGGGGGGGGG
|
||||||
|
GGGGGGGGGGG<>GGGGGGGGGGGGGGGGGGG
|
||||||
|
GGGGGGGGGGG<>GGGG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GGGGGGGGGGGGGGGG
|
||||||
|
GGGGGGGGGGG<>GGGGGGGGGGGGGGGGGGG
|
||||||
|
GGGGGGGGGGG<>GGGGGGGGGGGGGGGGGGG
|
||||||
|
|
||||||
|
................................
|
||||||
|
................................
|
||||||
|
................................
|
||||||
|
................................
|
||||||
|
................................
|
||||||
|
................................
|
||||||
|
................................
|
||||||
|
................................
|
||||||
|
................................
|
||||||
|
................................
|
||||||
|
................................
|
||||||
|
................................
|
||||||
|
................................
|
||||||
|
................................
|
||||||
|
................................
|
||||||
|
................................
|
||||||
|
................................
|
||||||
|
................................
|
||||||
|
................................
|
||||||
|
................................
|
||||||
|
................................
|
||||||
|
................................
|
||||||
|
................................
|
||||||
|
................................
|
||||||
|
................................
|
||||||
|
................................
|
||||||
|
................................
|
||||||
|
................................
|
||||||
|
................................
|
||||||
|
................................
|
||||||
|
................................
|
||||||
|
................................
|
BIN
resources/sprite/tile/grass.png
Normal file
BIN
resources/sprite/tile/grass.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.3 KiB |
BIN
resources/sprite/tile/road_left.png
Normal file
BIN
resources/sprite/tile/road_left.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 245 B |
BIN
resources/sprite/tile/road_right.png
Normal file
BIN
resources/sprite/tile/road_right.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 242 B |
|
@ -26,6 +26,10 @@
|
||||||
|
|
||||||
#include "Map.hpp"
|
#include "Map.hpp"
|
||||||
|
|
||||||
|
#include <cstdio>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
|
|
||||||
namespace RPGEdit {
|
namespace RPGEdit {
|
||||||
|
|
||||||
|
@ -85,32 +89,74 @@ void Map::finishEntityTransition(Entity *entity) {
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<Map> Map::load(__attribute__((unused)) const std::string &name) {
|
std::unique_ptr<Map> Map::load(__attribute__((unused)) const std::string &name) {
|
||||||
std::unique_ptr<Map> map(new Map(16, 16, 2));
|
std::string filename = "../resources/map/" + name + ".map";
|
||||||
|
|
||||||
map->tileset.emplace_back("dirt", 0);
|
std::ifstream file;
|
||||||
map->tileset.emplace_back("horizontal_bar", 0);
|
|
||||||
map->tileset.emplace_back("horizontal_bar", 2);
|
|
||||||
|
|
||||||
for (int i = 0; i < 16; i++) {
|
file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
|
||||||
for (int j = 0; j < 16; j++) {
|
file.open(filename.c_str());
|
||||||
if (4 <= i && i < 12 && 4 <= j && j < 12) {
|
|
||||||
map->setTileAt(0, Position<int>{i, j}, 1);
|
|
||||||
map->setCollisionAt(Position<int>{i, j}, CollisionType::EMPTY);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
map->setTileAt(0, Position<int>{i, j}, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (4 <= i && i < 12 && j == 7)
|
std::string line;
|
||||||
map->setTileAt(1, Position<int>{i, j}, 3);
|
|
||||||
else if (4 <= i && i < 12 && j == 8)
|
std::getline(file, line);
|
||||||
map->setTileAt(1, Position<int>{i, j}, 2);
|
size_t w, h, layers;
|
||||||
else
|
if (std::sscanf(line.c_str(), "%zu %zu %zu", &w, &h, &layers) != 3)
|
||||||
map->setTileAt(1, Position<int>{i, j}, 0);
|
throw std::invalid_argument("invalid map file");
|
||||||
|
|
||||||
|
std::unique_ptr<Map> map(new Map(w, h, layers));
|
||||||
|
|
||||||
|
std::getline(file, line);
|
||||||
|
if (line.length())
|
||||||
|
throw std::invalid_argument("invalid map file");
|
||||||
|
|
||||||
|
size_t n = 1;
|
||||||
|
std::unordered_map<char, size_t> tileset;
|
||||||
|
|
||||||
|
for (std::getline(file, line); line.length(); std::getline(file, line)) {
|
||||||
|
char c;
|
||||||
|
unsigned rot;
|
||||||
|
char *tile = nullptr;
|
||||||
|
|
||||||
|
if (std::sscanf(line.c_str(), "%c %u %ms", &c, &rot, &tile) != 3) {
|
||||||
|
std::free(tile);
|
||||||
|
throw std::invalid_argument("invalid map file");
|
||||||
|
}
|
||||||
|
|
||||||
|
tileset.emplace(c, n++);
|
||||||
|
map->tileset.emplace_back(tile, rot);
|
||||||
|
std::free(tile);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (size_t i = 0; i < h; i++) {
|
||||||
|
std::getline(file, line);
|
||||||
|
if (line.length() != w)
|
||||||
|
throw std::invalid_argument("invalid map file");
|
||||||
|
|
||||||
|
for (size_t j = 0; j < w; j++) {
|
||||||
|
if (line[j] == '1')
|
||||||
|
map->setCollisionAt(Position<int>{int(j), int(i)}, CollisionType::EMPTY);
|
||||||
|
else if (line[j] != '0')
|
||||||
|
throw std::invalid_argument("invalid map file");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
map->addEntity("square", Model::Position<int>{6, 6});
|
for (size_t layer = 0; layer < layers; layer++) {
|
||||||
|
std::getline(file, line);
|
||||||
|
if (line.length())
|
||||||
|
throw std::invalid_argument("invalid map file");
|
||||||
|
|
||||||
|
for (size_t i = 0; i < h; i++) {
|
||||||
|
std::getline(file, line);
|
||||||
|
if (line.length() != w)
|
||||||
|
throw std::invalid_argument("invalid map file");
|
||||||
|
|
||||||
|
for (size_t j = 0; j < w; j++) {
|
||||||
|
auto it = tileset.find(line[j]);
|
||||||
|
if (it != tileset.end())
|
||||||
|
map->setTileAt(layer, Position<int>{int(j), int(i)}, it->second);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue