Fix up the colors and rendering so that tilemap just uses components::Tile all the time. Need to load all of the config data from json one time on system start instead of constantly, although constantly does make debugging live easier.

This commit is contained in:
Zed A. Shaw 2025-02-09 15:54:17 -05:00
parent a4c13f7fc9
commit 0cbe20af35
10 changed files with 36 additions and 44 deletions

View file

@ -7,7 +7,7 @@
"inventory_count": 0,
"randomized": false,
"components": [
{"_type": "Tile", "chr": "\u2ac5",
{"_type": "Tile", "display": "\u2ac5",
"foreground": [24, 205, 189],
"background": [24, 205, 189]
},
@ -23,7 +23,7 @@
"inventory_count": 0,
"placement": "fixed",
"components": [
{"_type": "Tile", "chr": "\u2259",
{"_type": "Tile", "display": "\u2259",
"foreground": [24, 205, 189],
"background": [24, 205, 189]
},
@ -38,7 +38,7 @@
"description": "Spikes stab you from the floor.",
"inventory_count": 0,
"components": [
{"_type": "Tile", "chr": "\u1ac7",
{"_type": "Tile", "display": "\u1ac7",
"foreground": [24, 205, 189],
"background": [24, 205, 189]
},

View file

@ -1,7 +1,7 @@
{
"PLAYER_TILE": {
"components": [
{"_type": "Tile", "chr": "\ua66b",
{"_type": "Tile", "display": "\ua66b",
"foreground": [255, 200, 125],
"background": [30, 20, 75]
},
@ -13,7 +13,7 @@
},
"EVIL_EYE": {
"components": [
{"_type": "Tile", "chr": "\u08ac",
{"_type": "Tile", "display": "\u08ac",
"foreground": [75, 200, 125],
"background": [30, 20, 75]
},

View file

@ -6,7 +6,7 @@
"inventory_count": 1,
"components": [
{"_type": "LightSource", "strength": 70, "radius": 2.0},
{"_type": "Tile", "chr": "\u0f08",
{"_type": "Tile", "display": "\u0f08",
"foreground": [24, 120, 189],
"background": [230,120, 120]
}
@ -19,7 +19,7 @@
"inventory_count": 1,
"components": [
{"_type": "Weapon", "damage": 15},
{"_type": "Tile", "chr": "\u1e37",
{"_type": "Tile", "display": "\u1e37",
"foreground": [24, 120, 189],
"background": [24, 120, 189]
}
@ -32,7 +32,7 @@
"inventory_count": 1,
"components": [
{"_type": "LightSource", "strength": 70, "radius": 1.8},
{"_type": "Tile", "chr": "\u0236",
{"_type": "Tile", "display": "\u0236",
"foreground": [24, 205, 210],
"background": [24, 205, 210]
},
@ -44,7 +44,7 @@
"name": "Small Chest",
"description": "A small chest of gold. You wonder who would leave something like this around.",
"components": [
{"_type": "Tile", "chr": "\uaaea",
{"_type": "Tile", "display": "\uaaea",
"foreground": [150, 100, 189],
"background": [150, 100, 189]
},
@ -58,7 +58,7 @@
"description": "A torch on a wall you can't pick up.",
"inventory_count": 0,
"components": [
{"_type": "Tile", "chr": "\u077e",
{"_type": "Tile", "display": "\u077e",
"foreground": [24, 205, 210],
"background": [24, 205, 210]
},
@ -71,7 +71,7 @@
"description": "A small healing potion.",
"inventory_count": 1,
"components": [
{"_type": "Tile", "chr": "\u03eb",
{"_type": "Tile", "display": "\u03eb",
"foreground": [255, 205, 189],
"background": [255, 205, 189]
},

View file

@ -7,7 +7,6 @@ namespace components {
ENROLL_COMPONENT(Weapon, damage);
ENROLL_COMPONENT(Curative, hp);
ENROLL_COMPONENT(EnemyConfig, hearing_distance);
ENROLL_COMPONENT(Tile, chr, foreground, background);
ENROLL_COMPONENT(Motion, dx, dy, random);
ENROLL_COMPONENT(Combat, hp, damage, dead);
ENROLL_COMPONENT(LightSource, strength, radius);

View file

@ -25,7 +25,7 @@ namespace components {
};
struct Tile {
std::string chr;
std::string display;
std::array<uint8_t, 3> foreground;
std::array<uint8_t, 3> background;
};
@ -78,4 +78,7 @@ namespace components {
};
void configure(ComponentMap& component_map);
// these need to be here if you're using components::convert outside of components.cpp
ENROLL_COMPONENT(Tile, display, foreground, background);
}

View file

@ -19,6 +19,12 @@ namespace components {
using ReflFuncSignature = std::function<void(DinkyECS::World& world, DinkyECS::Entity ent, nlohmann::json &j)>;
using ComponentMap = std::unordered_map<std::string, ReflFuncSignature>;
template<typename COMPONENT> COMPONENT convert(nlohmann::json &data) {
COMPONENT result;
from_json(data, result);
return result;
}
template <typename COMPONENT> void enroll(ComponentMap &m) {
m[NameOf<COMPONENT>::name] = [](DinkyECS::World& world, DinkyECS::Entity ent, nlohmann::json &j) {
COMPONENT c;

View file

@ -37,7 +37,7 @@ namespace gui {
for(size_t y = 0; y < end_y; ++y) {
for(size_t x = 0; x < end_x; ++x)
{
const TileCell& tile = tiles.at(start.x+x, start.y+y);
const Tile& tile = tiles.at(start.x+x, start.y+y);
// light value is an integer that's a percent
float light_value = debug.LIGHT ? 80 * PERCENT : lighting[start.y+y][start.x+x] * PERCENT;
int dnum = debug.PATHS ? paths[start.y+y][start.x+x] : WALL_PATH_LIMIT;
@ -47,12 +47,12 @@ namespace gui {
$canvas.DrawText(x * 2, y * 4, num, [dnum, tile, light_value](auto &pixel) {
pixel.foreground_color = Color::HSV(dnum * 20, 150, 200);
pixel.background_color = Color::HSV(30, 20, tile.bg_v * 50 * PERCENT);
pixel.background_color = Color::HSV(30, 20, tile.foreground[2] * 50 * PERCENT);
});
} else {
$canvas.DrawText(x * 2, y * 4, tile.display, [tile, light_value](auto &pixel) {
pixel.foreground_color = Color::HSV(tile.fg_h, tile.fg_s, tile.fg_v * light_value);
pixel.background_color = Color::HSV(tile.bg_h, tile.bg_s, tile.bg_v * light_value);
pixel.foreground_color = Color::HSV(tile.foreground[0], tile.foreground[1], tile.foreground[2] * light_value);
pixel.background_color = Color::HSV(tile.background[0], tile.background[1], tile.background[2] * light_value);
});
}
}

View file

@ -238,12 +238,12 @@ void System::draw_entities(DinkyECS::World &world, Map &map, const Matrix &light
Point loc = map.map_to_camera(pos.location, cam_orig);
float light_value = lights[pos.location.y][pos.location.x] * PERCENT;
const TileCell& cell = tiles.at(pos.location.x, pos.location.y);
const Tile& cell = tiles.at(pos.location.x, pos.location.y);
// the 2 and 4 are from ftxui::Canvas since it does a kind of "subpixel" drawing
canvas.DrawText(loc.x*2, loc.y*4, tile.chr, [tile, light_value, cell](auto &pixel) {
canvas.DrawText(loc.x*2, loc.y*4, tile.display, [tile, light_value, cell](auto &pixel) {
pixel.foreground_color = Color::HSV(tile.foreground[0], tile.foreground[1], tile.foreground[2] * light_value);
pixel.background_color = Color::HSV(cell.bg_h, cell.bg_s, cell.bg_v * light_value);
pixel.background_color = Color::HSV(cell.background[0], cell.background[1], cell.background[2] * light_value);
});
}
});

View file

@ -3,19 +3,20 @@
#include "constants.hpp"
using nlohmann::json;
using components::Tile;
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, {""}))
$display(height, TileRow(width, {"", {0,0,0}, {0,0,0}}))
{
}
void TileMap::dump(int show_x, int show_y) {
for(matrix::each_row it{$tile_ids}; it.next();) {
const TileCell &cell = $display[it.y][it.x];
const Tile &cell = $display[it.y][it.x];
if(int(it.x) == show_x && int(it.y) == show_y) {
fmt::print("{}<", cell.display);
@ -30,15 +31,7 @@ void TileMap::dump(int show_x, int show_y) {
void TileMap::set_tile(size_t x, size_t y, string tile_name) {
std::wstring tile_id = $config.wstring(tile_name, "display");
json tile_conf = $config[tile_name];
TileCell tile{
tile_conf["display"],
tile_conf["foreground"][0],
tile_conf["foreground"][1],
tile_conf["foreground"][2],
tile_conf["background"][0],
tile_conf["background"][1],
tile_conf["background"][2]};
auto tile = components::convert<Tile>(tile_conf);
$tile_ids[y][x] = tile_id[0];
$display[y][x] = tile;
}
@ -50,7 +43,7 @@ void TileMap::load(matrix::Matrix &walls) {
}
}
const TileCell &TileMap::at(size_t x, size_t y) {
const Tile &TileMap::at(size_t x, size_t y) {
return $display[y][x];
}

View file

@ -7,18 +7,9 @@
#include "point.hpp"
#include "matrix.hpp"
#include "config.hpp"
#include "components.hpp"
struct TileCell {
std::string display;
uint8_t fg_h = 0;
uint8_t fg_s = 0;
uint8_t fg_v = 0;
uint8_t bg_h = 0;
uint8_t bg_s = 0;
uint8_t bg_v = 0;
};
typedef std::vector<TileCell> TileRow;
typedef std::vector<components::Tile> TileRow;
typedef std::vector<TileRow> TileGrid;
class TileMap {
@ -37,7 +28,7 @@ public:
size_t width() { return $width; }
size_t height() { return $height; }
void load(matrix::Matrix &walls);
const TileCell &at(size_t x, size_t y);
const components::Tile &at(size_t x, size_t y);
void set_tile(size_t x, size_t y, std::string tile_name);
std::vector<std::string> tile_names(bool collision);