Now have a configurable displayable tilemap to do better tiles.
This commit is contained in:
parent
b66a3154c7
commit
7fe6ad174d
10 changed files with 40 additions and 16 deletions
|
@ -1 +1 @@
|
||||||
set makeprg=meson\ compile\ -j\ 4\ -C\ .
|
set makeprg=meson\ compile\ -C\ .
|
||||||
|
|
8
assets/tiles.json
Normal file
8
assets/tiles.json
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
{
|
||||||
|
"WALL_TILE": "\ua5b8",
|
||||||
|
"FLOOR_TILE": "\u2849",
|
||||||
|
"PLAYER_TILE": "\ua66b",
|
||||||
|
"ENEMY_TILE": "\u1d5c",
|
||||||
|
"BG_TILE": "█",
|
||||||
|
"WATER_TILE": "\u224b"
|
||||||
|
}
|
|
@ -18,3 +18,4 @@ const int MIN_FONT_SIZE = 20;
|
||||||
const int SCREEN_WIDTH = 40;
|
const int SCREEN_WIDTH = 40;
|
||||||
const int SCREEN_HEIGHT = 30;
|
const int SCREEN_HEIGHT = 30;
|
||||||
#define FONT_FILE_NAME "./assets/text.otf"
|
#define FONT_FILE_NAME "./assets/text.otf"
|
||||||
|
#define TILE_MAP_CONFIG "./assets/tiles.json"
|
||||||
|
|
11
map.cpp
11
map.cpp
|
@ -13,17 +13,18 @@ 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, SPACE_VALUE)),
|
$tiles(width, height),
|
||||||
$walls(height, matrix::Row(width, SPACE_VALUE)),
|
$walls(height, matrix::Row(width, SPACE_VALUE)),
|
||||||
$paths(width, height)
|
$paths(width, height)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
Map::Map(Matrix &walls, Pathing &paths) :
|
Map::Map(Matrix &walls, Pathing &paths) :
|
||||||
|
$tiles(matrix::width(walls), matrix::height(walls)),
|
||||||
$walls(walls),
|
$walls(walls),
|
||||||
$paths(paths)
|
$paths(paths)
|
||||||
{
|
{
|
||||||
$width = walls[0].size();
|
$width = matrix::width(walls);
|
||||||
$height = walls.size();
|
$height = matrix::height(walls);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Map::make_paths() {
|
void Map::make_paths() {
|
||||||
|
@ -175,3 +176,7 @@ bool Map::INVARIANT() {
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Map::load_tiles() {
|
||||||
|
$tiles.load($walls);
|
||||||
|
}
|
||||||
|
|
7
map.hpp
7
map.hpp
|
@ -11,6 +11,7 @@
|
||||||
#include "pathing.hpp"
|
#include "pathing.hpp"
|
||||||
#include "matrix.hpp"
|
#include "matrix.hpp"
|
||||||
#include "constants.hpp"
|
#include "constants.hpp"
|
||||||
|
#include "tilemap.hpp"
|
||||||
|
|
||||||
using lighting::LightSource;
|
using lighting::LightSource;
|
||||||
|
|
||||||
|
@ -30,7 +31,7 @@ class Map {
|
||||||
public:
|
public:
|
||||||
size_t $width;
|
size_t $width;
|
||||||
size_t $height;
|
size_t $height;
|
||||||
Matrix $tiles;
|
TileMap $tiles;
|
||||||
Matrix $walls;
|
Matrix $walls;
|
||||||
Pathing $paths;
|
Pathing $paths;
|
||||||
std::vector<Room> $rooms;
|
std::vector<Room> $rooms;
|
||||||
|
@ -43,7 +44,7 @@ public:
|
||||||
Map(Map &map) = delete;
|
Map(Map &map) = delete;
|
||||||
|
|
||||||
Matrix& paths() { return $paths.paths(); }
|
Matrix& paths() { return $paths.paths(); }
|
||||||
Matrix& tiles() { return $tiles; }
|
TileMap& tiles() { return $tiles; }
|
||||||
Matrix& input_map() { return $paths.input(); }
|
Matrix& input_map() { return $paths.input(); }
|
||||||
Matrix& walls() { return $walls; }
|
Matrix& walls() { return $walls; }
|
||||||
size_t width() { return $width; }
|
size_t width() { return $width; }
|
||||||
|
@ -68,4 +69,6 @@ public:
|
||||||
|
|
||||||
void dump(int show_x=-1, int show_y=-1);
|
void dump(int show_x=-1, int show_y=-1);
|
||||||
bool INVARIANT();
|
bool INVARIANT();
|
||||||
|
|
||||||
|
void load_tiles();
|
||||||
};
|
};
|
||||||
|
|
|
@ -188,7 +188,7 @@ void System::draw_map(DinkyECS::World &world, Map &game_map, const Matrix &light
|
||||||
|
|
||||||
for(size_t y = 0; y < end_y; ++y) {
|
for(size_t y = 0; y < end_y; ++y) {
|
||||||
for(size_t x = 0; x < end_x; ++x) {
|
for(size_t x = 0; x < end_x; ++x) {
|
||||||
string tile = tiles[start.y+y][start.x+x] == L'#' ? config.WALL_TILE : config.FLOOR_TILE;
|
const string& tile = tiles.at(start.x+x, start.y+y);
|
||||||
int light_value = debug.LIGHT ? 160 : lighting[start.y+y][start.x+x];
|
int light_value = debug.LIGHT ? 160 : lighting[start.y+y][start.x+x];
|
||||||
|
|
||||||
if(tile == config.WALL_TILE) {
|
if(tile == config.WALL_TILE) {
|
||||||
|
|
|
@ -17,8 +17,7 @@ TEST_CASE("tilemap can load tiles and make a map", "[tilemap]") {
|
||||||
WorldBuilder builder(map);
|
WorldBuilder builder(map);
|
||||||
builder.generate();
|
builder.generate();
|
||||||
|
|
||||||
Config config("./assets/tiles.json");
|
TileMap tiles(width, height);
|
||||||
TileMap tiles(config, width, height);
|
|
||||||
auto& walls = map.walls();
|
auto& walls = map.walls();
|
||||||
tiles.load(walls);
|
tiles.load(walls);
|
||||||
tiles.dump();
|
tiles.dump();
|
||||||
|
|
13
tilemap.cpp
13
tilemap.cpp
|
@ -3,16 +3,16 @@
|
||||||
#include "constants.hpp"
|
#include "constants.hpp"
|
||||||
#include "render.hpp"
|
#include "render.hpp"
|
||||||
|
|
||||||
TileMap::TileMap(Config& config, size_t width, size_t height)
|
TileMap::TileMap(size_t width, size_t height) :
|
||||||
: $config(config),
|
$config("./assets/tiles.json"),
|
||||||
$width(width),
|
$width(width),
|
||||||
$height(height),
|
$height(height),
|
||||||
$tile_ids(height, matrix::Row(width, SPACE_VALUE)),
|
$tile_ids(height, matrix::Row(width, SPACE_VALUE)),
|
||||||
$display(height, TileRow(width, ""))
|
$display(height, TileRow(width, ""))
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void TileMap::dump(int show_x, int show_y) {
|
void TileMap::dump(int show_x, int show_y) {
|
||||||
SFMLRender::init_terminal();
|
SFMLRender::init_terminal();
|
||||||
for(matrix::each_row it{$tile_ids}; it.next();) {
|
for(matrix::each_row it{$tile_ids}; it.next();) {
|
||||||
|
@ -33,13 +33,18 @@ void TileMap::load(matrix::Matrix &walls) {
|
||||||
string tile_name = walls[it.y][it.x] == SPACE_VALUE ? "FLOOR_TILE" : "WALL_TILE";
|
string tile_name = walls[it.y][it.x] == SPACE_VALUE ? "FLOOR_TILE" : "WALL_TILE";
|
||||||
|
|
||||||
std::wstring tile = $config.wstring(tile_name);
|
std::wstring tile = $config.wstring(tile_name);
|
||||||
std::string tile_s = $config[tile_name];
|
string tile_s = $config[tile_name];
|
||||||
|
|
||||||
$tile_ids[it.y][it.x] = tile[0];
|
$tile_ids[it.y][it.x] = tile[0];
|
||||||
$display[it.y][it.x] = tile_s;
|
$display[it.y][it.x] = tile_s;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
const string &TileMap::at(size_t x, size_t y) {
|
||||||
|
return $display[y][x];
|
||||||
|
}
|
||||||
|
|
||||||
bool TileMap::INVARIANT() {
|
bool TileMap::INVARIANT() {
|
||||||
dbc::check(matrix::height($tile_ids) == $height, "$tile_ids has wrong height");
|
dbc::check(matrix::height($tile_ids) == $height, "$tile_ids has wrong height");
|
||||||
dbc::check(matrix::width($tile_ids) == $width, "$tile_ids has wrong width");
|
dbc::check(matrix::width($tile_ids) == $width, "$tile_ids has wrong width");
|
||||||
|
|
|
@ -13,13 +13,13 @@ typedef std::vector<TileRow> TileDisplay;
|
||||||
|
|
||||||
class TileMap {
|
class TileMap {
|
||||||
public:
|
public:
|
||||||
Config &$config;
|
Config $config;
|
||||||
size_t $width;
|
size_t $width;
|
||||||
size_t $height;
|
size_t $height;
|
||||||
matrix::Matrix $tile_ids;
|
matrix::Matrix $tile_ids;
|
||||||
TileDisplay $display;
|
TileDisplay $display;
|
||||||
|
|
||||||
TileMap(Config &config, size_t width, size_t height);
|
TileMap(size_t width, size_t height);
|
||||||
|
|
||||||
// disable copying
|
// disable copying
|
||||||
TileMap(TileMap &map) = delete;
|
TileMap(TileMap &map) = delete;
|
||||||
|
@ -27,6 +27,7 @@ public:
|
||||||
size_t width() { return $width; }
|
size_t width() { return $width; }
|
||||||
size_t height() { return $height; }
|
size_t height() { return $height; }
|
||||||
void load(matrix::Matrix &walls);
|
void load(matrix::Matrix &walls);
|
||||||
|
const std::string &at(size_t x, size_t y);
|
||||||
|
|
||||||
void dump(int show_x=-1, int show_y=-1);
|
void dump(int show_x=-1, int show_y=-1);
|
||||||
bool INVARIANT();
|
bool INVARIANT();
|
||||||
|
|
|
@ -137,8 +137,10 @@ void WorldBuilder::generate() {
|
||||||
for(matrix::each_cell it{$map.$walls}; it.next();) {
|
for(matrix::each_cell it{$map.$walls}; it.next();) {
|
||||||
int is_wall = !$map.$walls[it.y][it.x];
|
int is_wall = !$map.$walls[it.y][it.x];
|
||||||
$map.$walls[it.y][it.x] = is_wall;
|
$map.$walls[it.y][it.x] = is_wall;
|
||||||
$map.$tiles[it.y][it.x] = is_wall ? L'#' : L'.';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// BUG: this is so weird
|
||||||
|
$map.load_tiles();
|
||||||
}
|
}
|
||||||
|
|
||||||
void WorldBuilder::make_room(size_t origin_x, size_t origin_y, size_t w, size_t h) {
|
void WorldBuilder::make_room(size_t origin_x, size_t origin_y, size_t w, size_t h) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue