Now have a configurable displayable tilemap to do better tiles.

main
Zed A. Shaw 5 days ago
parent b66a3154c7
commit 7fe6ad174d
  1. 2
      .vimrc_proj
  2. 8
      assets/tiles.json
  3. 1
      constants.hpp
  4. 11
      map.cpp
  5. 7
      map.hpp
  6. 2
      systems.cpp
  7. 3
      tests/tilemap.cpp
  8. 13
      tilemap.cpp
  9. 5
      tilemap.hpp
  10. 4
      worldbuilder.cpp

@ -1 +1 @@
set makeprg=meson\ compile\ -j\ 4\ -C\ .
set makeprg=meson\ compile\ -C\ .

@ -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_HEIGHT = 30;
#define FONT_FILE_NAME "./assets/text.otf"
#define TILE_MAP_CONFIG "./assets/tiles.json"

@ -13,17 +13,18 @@ using namespace fmt;
Map::Map(size_t width, size_t height) :
$width(width),
$height(height),
$tiles(height, matrix::Row(width, SPACE_VALUE)),
$tiles(width, height),
$walls(height, matrix::Row(width, SPACE_VALUE)),
$paths(width, height)
{}
Map::Map(Matrix &walls, Pathing &paths) :
$tiles(matrix::width(walls), matrix::height(walls)),
$walls(walls),
$paths(paths)
{
$width = walls[0].size();
$height = walls.size();
$width = matrix::width(walls);
$height = matrix::height(walls);
}
void Map::make_paths() {
@ -175,3 +176,7 @@ bool Map::INVARIANT() {
return true;
}
void Map::load_tiles() {
$tiles.load($walls);
}

@ -11,6 +11,7 @@
#include "pathing.hpp"
#include "matrix.hpp"
#include "constants.hpp"
#include "tilemap.hpp"
using lighting::LightSource;
@ -30,7 +31,7 @@ class Map {
public:
size_t $width;
size_t $height;
Matrix $tiles;
TileMap $tiles;
Matrix $walls;
Pathing $paths;
std::vector<Room> $rooms;
@ -43,7 +44,7 @@ public:
Map(Map &map) = delete;
Matrix& paths() { return $paths.paths(); }
Matrix& tiles() { return $tiles; }
TileMap& tiles() { return $tiles; }
Matrix& input_map() { return $paths.input(); }
Matrix& walls() { return $walls; }
size_t width() { return $width; }
@ -68,4 +69,6 @@ public:
void dump(int show_x=-1, int show_y=-1);
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 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];
if(tile == config.WALL_TILE) {

@ -17,8 +17,7 @@ TEST_CASE("tilemap can load tiles and make a map", "[tilemap]") {
WorldBuilder builder(map);
builder.generate();
Config config("./assets/tiles.json");
TileMap tiles(config, width, height);
TileMap tiles(width, height);
auto& walls = map.walls();
tiles.load(walls);
tiles.dump();

@ -3,16 +3,16 @@
#include "constants.hpp"
#include "render.hpp"
TileMap::TileMap(Config& config, size_t width, size_t height)
: $config(config),
TileMap::TileMap(size_t width, size_t height) :
$config("./assets/tiles.json"),
$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();) {
@ -33,13 +33,18 @@ void TileMap::load(matrix::Matrix &walls) {
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];
string tile_s = $config[tile_name];
$tile_ids[it.y][it.x] = tile[0];
$display[it.y][it.x] = tile_s;
}
}
const string &TileMap::at(size_t x, size_t y) {
return $display[y][x];
}
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");

@ -13,13 +13,13 @@ typedef std::vector<TileRow> TileDisplay;
class TileMap {
public:
Config &$config;
Config $config;
size_t $width;
size_t $height;
matrix::Matrix $tile_ids;
TileDisplay $display;
TileMap(Config &config, size_t width, size_t height);
TileMap(size_t width, size_t height);
// disable copying
TileMap(TileMap &map) = delete;
@ -27,6 +27,7 @@ public:
size_t width() { return $width; }
size_t height() { return $height; }
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);
bool INVARIANT();

@ -137,8 +137,10 @@ void WorldBuilder::generate() {
for(matrix::each_cell it{$map.$walls}; it.next();) {
int is_wall = !$map.$walls[it.y][it.x];
$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) {

Loading…
Cancel
Save