Now have color and display char coming from assets/tiles.json but lighting still needs work.

main
Zed A. Shaw 1 week ago
parent 7fe6ad174d
commit 89e31279be
  1. 36
      assets/tiles.json
  2. 21
      systems.cpp
  3. 32
      tilemap.cpp
  4. 19
      tilemap.hpp

@ -1,8 +1,32 @@
{ {
"WALL_TILE": "\ua5b8", "WALL_TILE": {
"FLOOR_TILE": "\u2849", "foreground": [230, 20, 0],
"PLAYER_TILE": "\ua66b", "background": [230, 20, 0],
"ENEMY_TILE": "\u1d5c", "display": "\ua5b8"
"BG_TILE": "█", },
"WATER_TILE": "\u224b" "FLOOR_TILE": {
"foreground": [80, 100, 0],
"background": [30, 20, 0],
"display":"\u2849"
},
"PLAYER_TILE": {
"foreground": [255, 200, 0],
"background": [30, 20, 0],
"display":"\ua66b"
},
"ENEMY_TILE": {
"foreground": [255, 200, 0],
"background": [30, 20, 0],
"display":"\u1d5c"
},
"BG_TILE": {
"foreground": [230, 20, 0],
"background": [230, 20, 0],
"display":"█"
},
"WATER_TILE": {
"foreground": [132, 200, 0],
"background": [147, 220, 0],
"display":"\u224b"
}
} }

@ -176,7 +176,6 @@ void System::draw_entities(DinkyECS::World &world, Map &game_map, const Matrix &
void System::draw_map(DinkyECS::World &world, Map &game_map, const Matrix &lighting, ftxui::Canvas &canvas, size_t view_x, size_t view_y) { void System::draw_map(DinkyECS::World &world, Map &game_map, const Matrix &lighting, ftxui::Canvas &canvas, size_t view_x, size_t view_y) {
const auto& debug = world.get_the<Debug>(); const auto& debug = world.get_the<Debug>();
const auto& config = world.get_the<MapConfig>();
const auto& player = world.get_the<Player>(); const auto& player = world.get_the<Player>();
const auto& player_position = world.get<Position>(player.entity); const auto& player_position = world.get<Position>(player.entity);
Point start = game_map.center_camera(player_position.location, view_x, view_y); Point start = game_map.center_camera(player_position.location, view_x, view_y);
@ -188,15 +187,10 @@ 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) {
const string& tile = tiles.at(start.x+x, start.y+y); const TileCell& 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(debug.PATHS) {
canvas.DrawText(x * 2, y * 4, config.WALL_TILE, [light_value](auto &pixel) {
pixel.foreground_color = Color::HSV(230, 20, 10);
pixel.background_color = Color::HSV(230, 20, light_value / 2);
});
} else if(debug.PATHS) {
int dnum = paths[start.y+y][start.x+x]; int dnum = paths[start.y+y][start.x+x];
string num = format("{:x}", dnum); string num = format("{:x}", dnum);
num = num.size() > 2 ? "*" : num; num = num.size() > 2 ? "*" : num;
@ -205,15 +199,10 @@ void System::draw_map(DinkyECS::World &world, Map &game_map, const Matrix &light
pixel.foreground_color = Color::HSV(dnum * 20, 150, 200); pixel.foreground_color = Color::HSV(dnum * 20, 150, 200);
pixel.background_color = Color::HSV(30, 20, light_value / 5); pixel.background_color = Color::HSV(30, 20, light_value / 5);
}); });
} else if(tile == config.WATER_TILE) {
canvas.DrawText(x * 2, y * 4, tile, [light_value](auto &pixel) {
pixel.foreground_color = Color::HSV(132, 200, std::min(int(light_value * 1.5), 200));
pixel.background_color = Color::HSV(147, 220, light_value / 1.5);
});
} else { } else {
canvas.DrawText(x * 2, y * 4, tile, [light_value](auto &pixel) { canvas.DrawText(x * 2, y * 4, tile.display, [tile, light_value](auto &pixel) {
pixel.foreground_color = Color::HSV(80, 100, light_value / 1.5); pixel.foreground_color = Color::HSV(tile.fg_h, tile.fg_s, light_value);
pixel.background_color = Color::HSV(30, 20, light_value / 3); pixel.background_color = Color::HSV(tile.bg_h, tile.bg_s, light_value / 2);
}); });
} }
} }

@ -3,25 +3,27 @@
#include "constants.hpp" #include "constants.hpp"
#include "render.hpp" #include "render.hpp"
using nlohmann::json;
using ftxui::Color;
TileMap::TileMap(size_t width, size_t height) : TileMap::TileMap(size_t width, size_t height) :
$config("./assets/tiles.json"), $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();) {
string cell = $display[it.y][it.x]; const TileCell &cell = $display[it.y][it.x];
if(int(it.x) == show_x && int(it.y) == show_y) { if(int(it.x) == show_x && int(it.y) == show_y) {
fmt::print("{}<", cell); fmt::print("{}<", cell.display);
} else { } else {
fmt::print("{} ", cell); fmt::print("{} ", cell.display);
} }
if(it.row) fmt::print("\n"); if(it.row) fmt::print("\n");
@ -32,16 +34,24 @@ void TileMap::load(matrix::Matrix &walls) {
for(matrix::each_cell it{walls}; it.next();) { for(matrix::each_cell it{walls}; it.next();) {
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_id = $config.wstring(tile_name, "display");
string tile_s = $config[tile_name]; json tile_conf = $config[tile_name];
TileCell tile{
$tile_ids[it.y][it.x] = tile[0]; tile_conf["display"],
$display[it.y][it.x] = tile_s; tile_conf["foreground"][0],
tile_conf["foreground"][1],
tile_conf["foreground"][2],
tile_conf["background"][0],
tile_conf["background"][1],
tile_conf["background"][2]};
$tile_ids[it.y][it.x] = tile_id[0];
$display[it.y][it.x] = tile;
} }
} }
const string &TileMap::at(size_t x, size_t y) { const TileCell &TileMap::at(size_t x, size_t y) {
return $display[y][x]; return $display[y][x];
} }

@ -7,9 +7,20 @@
#include "point.hpp" #include "point.hpp"
#include "matrix.hpp" #include "matrix.hpp"
#include "config.hpp" #include "config.hpp"
#include <ftxui/screen/color.hpp>
typedef std::vector<std::string> TileRow; struct TileCell {
typedef std::vector<TileRow> TileDisplay; std::string display;
uint8_t fg_h;
uint8_t fg_s;
uint8_t fg_v;
uint8_t bg_h;
uint8_t bg_s;
uint8_t bg_v;
};
typedef std::vector<TileCell> TileRow;
typedef std::vector<TileRow> TileGrid;
class TileMap { class TileMap {
public: public:
@ -17,7 +28,7 @@ public:
size_t $width; size_t $width;
size_t $height; size_t $height;
matrix::Matrix $tile_ids; matrix::Matrix $tile_ids;
TileDisplay $display; TileGrid $display;
TileMap(size_t width, size_t height); TileMap(size_t width, size_t height);
@ -27,7 +38,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); const TileCell &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();

Loading…
Cancel
Save