Basic tile map implemented.
This commit is contained in:
parent
290affa49a
commit
b66a3154c7
9 changed files with 135 additions and 12 deletions
4
.gdbinit
4
.gdbinit
|
@ -5,6 +5,6 @@ set logging overwrite on
|
||||||
set print pretty on
|
set print pretty on
|
||||||
set pagination off
|
set pagination off
|
||||||
break abort
|
break abort
|
||||||
break _invalid_parameter_noinfo
|
#break _invalid_parameter_noinfo
|
||||||
break _invalid_parameter
|
#break _invalid_parameter
|
||||||
catch throw
|
catch throw
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
#include "config.hpp"
|
#include "config.hpp"
|
||||||
|
|
||||||
|
using nlohmann::json;
|
||||||
|
|
||||||
Config::Config(const std::string src_path) : $src_path(src_path) {
|
Config::Config(const std::string src_path) : $src_path(src_path) {
|
||||||
std::ifstream infile($src_path);
|
std::ifstream infile($src_path);
|
||||||
$config = json::parse(infile);
|
$config = json::parse(infile);
|
||||||
|
@ -9,6 +11,12 @@ json &Config::operator[](const std::string &key) {
|
||||||
return $config[key];
|
return $config[key];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::wstring Config::wstring(const std::string key) {
|
||||||
|
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> $converter;
|
||||||
|
const std::string& str_val = $config[key];
|
||||||
|
return $converter.from_bytes(str_val);
|
||||||
|
}
|
||||||
|
|
||||||
std::wstring Config::wstring(const std::string main_key, const std::string sub_key) {
|
std::wstring Config::wstring(const std::string main_key, const std::string sub_key) {
|
||||||
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> $converter;
|
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> $converter;
|
||||||
const std::string& str_val = $config[main_key][sub_key];
|
const std::string& str_val = $config[main_key][sub_key];
|
||||||
|
|
10
config.hpp
10
config.hpp
|
@ -3,19 +3,17 @@
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <codecvt>
|
#include <codecvt>
|
||||||
|
|
||||||
|
|
||||||
using namespace nlohmann;
|
|
||||||
|
|
||||||
struct Config {
|
struct Config {
|
||||||
json $config;
|
nlohmann::json $config;
|
||||||
std::string $src_path;
|
std::string $src_path;
|
||||||
|
|
||||||
Config(const std::string src_path);
|
Config(const std::string src_path);
|
||||||
|
|
||||||
Config(json config, std::string src_path)
|
Config(nlohmann::json config, std::string src_path)
|
||||||
: $config(config), $src_path(src_path) {}
|
: $config(config), $src_path(src_path) {}
|
||||||
|
|
||||||
json &operator[](const std::string &key);
|
nlohmann::json &operator[](const std::string &key);
|
||||||
|
|
||||||
|
std::wstring wstring(const std::string main_key);
|
||||||
std::wstring wstring(const std::string main_key, const std::string sub_key);
|
std::wstring wstring(const std::string main_key, const std::string sub_key);
|
||||||
};
|
};
|
||||||
|
|
4
map.cpp
4
map.cpp
|
@ -13,8 +13,8 @@ using namespace fmt;
|
||||||
Map::Map(size_t width, size_t height) :
|
Map::Map(size_t width, size_t height) :
|
||||||
$width(width),
|
$width(width),
|
||||||
$height(height),
|
$height(height),
|
||||||
$tiles(height, matrix::Row(width, INV_WALL)),
|
$tiles(height, matrix::Row(width, SPACE_VALUE)),
|
||||||
$walls(height, matrix::Row(width, INV_WALL)),
|
$walls(height, matrix::Row(width, SPACE_VALUE)),
|
||||||
$paths(width, height)
|
$paths(width, height)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
|
|
|
@ -76,6 +76,14 @@ namespace matrix {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline size_t width(Matrix &mat) {
|
||||||
|
return mat[0].size();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline size_t height(Matrix &mat) {
|
||||||
|
return mat.size();
|
||||||
|
}
|
||||||
|
|
||||||
void dump(const std::string &msg, Matrix &map, int show_x=-1, int show_y=-1);
|
void dump(const std::string &msg, Matrix &map, int show_x=-1, int show_y=-1);
|
||||||
|
|
||||||
struct flood {
|
struct flood {
|
||||||
|
|
|
@ -22,6 +22,7 @@ dependencies = [
|
||||||
runtests = executable('runtests', [
|
runtests = executable('runtests', [
|
||||||
'matrix.cpp',
|
'matrix.cpp',
|
||||||
'dbc.cpp',
|
'dbc.cpp',
|
||||||
|
'tilemap.cpp',
|
||||||
'map.cpp',
|
'map.cpp',
|
||||||
'rand.cpp',
|
'rand.cpp',
|
||||||
'sound.cpp',
|
'sound.cpp',
|
||||||
|
@ -37,6 +38,7 @@ runtests = executable('runtests', [
|
||||||
'systems.cpp',
|
'systems.cpp',
|
||||||
'gui.cpp',
|
'gui.cpp',
|
||||||
'worldbuilder.cpp',
|
'worldbuilder.cpp',
|
||||||
|
'tests/tilemap.cpp',
|
||||||
'tests/matrix.cpp',
|
'tests/matrix.cpp',
|
||||||
'tests/fsm.cpp',
|
'tests/fsm.cpp',
|
||||||
'tests/dbc.cpp',
|
'tests/dbc.cpp',
|
||||||
|
@ -58,10 +60,11 @@ runtests = executable('runtests', [
|
||||||
dependencies: dependencies)
|
dependencies: dependencies)
|
||||||
|
|
||||||
roguish = executable('roguish', [
|
roguish = executable('roguish', [
|
||||||
'matrix.cpp',
|
|
||||||
'dbc.cpp',
|
'dbc.cpp',
|
||||||
'main.cpp',
|
'matrix.cpp',
|
||||||
|
'tilemap.cpp',
|
||||||
'map.cpp',
|
'map.cpp',
|
||||||
|
'main.cpp',
|
||||||
'gui.cpp',
|
'gui.cpp',
|
||||||
'rand.cpp',
|
'rand.cpp',
|
||||||
'sound.cpp',
|
'sound.cpp',
|
||||||
|
|
26
tests/tilemap.cpp
Normal file
26
tests/tilemap.cpp
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
#include <catch2/catch_test_macros.hpp>
|
||||||
|
#include <fmt/core.h>
|
||||||
|
#include "map.hpp"
|
||||||
|
#include "worldbuilder.hpp"
|
||||||
|
#include "tilemap.hpp"
|
||||||
|
#include "config.hpp"
|
||||||
|
#include "rand.hpp"
|
||||||
|
|
||||||
|
using namespace fmt;
|
||||||
|
using std::string;
|
||||||
|
|
||||||
|
TEST_CASE("tilemap can load tiles and make a map", "[tilemap]") {
|
||||||
|
size_t width = Random::uniform<size_t>(10, 25);
|
||||||
|
size_t height = Random::uniform<size_t>(10, 33);
|
||||||
|
|
||||||
|
Map map(width,height);
|
||||||
|
WorldBuilder builder(map);
|
||||||
|
builder.generate();
|
||||||
|
|
||||||
|
Config config("./assets/tiles.json");
|
||||||
|
TileMap tiles(config, width, height);
|
||||||
|
auto& walls = map.walls();
|
||||||
|
tiles.load(walls);
|
||||||
|
tiles.dump();
|
||||||
|
REQUIRE(tiles.INVARIANT());
|
||||||
|
}
|
47
tilemap.cpp
Normal file
47
tilemap.cpp
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
#include "tilemap.hpp"
|
||||||
|
#include "dbc.hpp"
|
||||||
|
#include "constants.hpp"
|
||||||
|
#include "render.hpp"
|
||||||
|
|
||||||
|
TileMap::TileMap(Config& config, size_t width, size_t height)
|
||||||
|
: $config(config),
|
||||||
|
$width(width),
|
||||||
|
$height(height),
|
||||||
|
$tile_ids(height, matrix::Row(width, SPACE_VALUE)),
|
||||||
|
$display(height, TileRow(width, ""))
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void TileMap::dump(int show_x, int show_y) {
|
||||||
|
SFMLRender::init_terminal();
|
||||||
|
for(matrix::each_row it{$tile_ids}; it.next();) {
|
||||||
|
string cell = $display[it.y][it.x];
|
||||||
|
|
||||||
|
if(int(it.x) == show_x && int(it.y) == show_y) {
|
||||||
|
fmt::print("{}<", cell);
|
||||||
|
} else {
|
||||||
|
fmt::print("{} ", cell);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(it.row) fmt::print("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void TileMap::load(matrix::Matrix &walls) {
|
||||||
|
for(matrix::each_cell it{walls}; it.next();) {
|
||||||
|
string tile_name = walls[it.y][it.x] == SPACE_VALUE ? "FLOOR_TILE" : "WALL_TILE";
|
||||||
|
|
||||||
|
std::wstring tile = $config.wstring(tile_name);
|
||||||
|
std::string tile_s = $config[tile_name];
|
||||||
|
|
||||||
|
$tile_ids[it.y][it.x] = tile[0];
|
||||||
|
$display[it.y][it.x] = tile_s;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool TileMap::INVARIANT() {
|
||||||
|
dbc::check(matrix::height($tile_ids) == $height, "$tile_ids has wrong height");
|
||||||
|
dbc::check(matrix::width($tile_ids) == $width, "$tile_ids has wrong width");
|
||||||
|
return true;
|
||||||
|
}
|
33
tilemap.hpp
Normal file
33
tilemap.hpp
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
#pragma once
|
||||||
|
#include <vector>
|
||||||
|
#include <utility>
|
||||||
|
#include <string>
|
||||||
|
#include <algorithm>
|
||||||
|
#include <fmt/core.h>
|
||||||
|
#include "point.hpp"
|
||||||
|
#include "matrix.hpp"
|
||||||
|
#include "config.hpp"
|
||||||
|
|
||||||
|
typedef std::vector<std::string> TileRow;
|
||||||
|
typedef std::vector<TileRow> TileDisplay;
|
||||||
|
|
||||||
|
class TileMap {
|
||||||
|
public:
|
||||||
|
Config &$config;
|
||||||
|
size_t $width;
|
||||||
|
size_t $height;
|
||||||
|
matrix::Matrix $tile_ids;
|
||||||
|
TileDisplay $display;
|
||||||
|
|
||||||
|
TileMap(Config &config, size_t width, size_t height);
|
||||||
|
|
||||||
|
// disable copying
|
||||||
|
TileMap(TileMap &map) = delete;
|
||||||
|
|
||||||
|
size_t width() { return $width; }
|
||||||
|
size_t height() { return $height; }
|
||||||
|
void load(matrix::Matrix &walls);
|
||||||
|
|
||||||
|
void dump(int show_x=-1, int show_y=-1);
|
||||||
|
bool INVARIANT();
|
||||||
|
};
|
Loading…
Add table
Add a link
Reference in a new issue