Map is way better and components::Tile is _vastly_ improved by switching to a wchar_t on display and letting nlohmann::json auto convert it for me.

This commit is contained in:
Zed A. Shaw 2025-03-22 02:10:56 -04:00
parent 2b57552152
commit 2e79cf8781
11 changed files with 70 additions and 71 deletions

View file

@ -11,6 +11,8 @@
#include "sound.hpp"
#include "ai.hpp"
#include "ai_debug.hpp"
#include "shiterator.hpp"
#include <iostream>
using std::string;
using namespace fmt;
@ -332,21 +334,45 @@ void System::plan_motion(DinkyECS::World& world, Point move_to) {
* This one is called inside the MapViewUI very often so
* just avoid GameMap unlike the others.
*/
void System::draw_entities(DinkyECS::World &world, Map &map, const Matrix &lights, const Point &cam_orig, size_t view_x, size_t view_y) {
std::wstring System::draw_map(GameLevel level, size_t view_x, size_t view_y) {
DinkyECS::World &world = *level.world;
Map &map = *level.map;
auto player_pos = world.get<Position>(level.player).location;
Point cam_orig = map.center_camera(player_pos, view_x, view_y);
auto &tiles = map.tiles();
world.query<Position, Tile>([&](auto, auto &pos, auto &tile) {
// make a grid of chars to work with
auto grid = shiterator::make<wchar_t>(view_x+1, view_y+1);
// first fill it with the map cells
for(shiterator::each_cell_t it{grid}; it.next();) {
size_t tile_y = size_t(it.y) + cam_orig.y;
size_t tile_x = size_t(it.x) + cam_orig.x;
if(tile_x < tiles.$width && tile_y < tiles.$height) {
grid[it.y][it.x] = tiles.at(tile_x, tile_y).display;
} else {
grid[it.y][it.x] = ' ';
}
}
// then get the enemy/item/device tiles and fill those in
world.query<Position, Tile>([&](auto, auto &pos, auto &entity_glyph) {
if(pos.location.x >= cam_orig.x && pos.location.x <= cam_orig.x + view_x
&& pos.location.y >= cam_orig.y && pos.location.y <= cam_orig.y + view_y) {
Point loc = map.map_to_camera(pos.location, cam_orig);
float light_value = lights[pos.location.y][pos.location.x] * PERCENT;
const Tile& cell = tiles.at(pos.location.x, pos.location.y);
(void)loc; // not used yet, this after ripping out map so needs rewrite
(void)light_value;
(void)cell;
(void)tile;
Point view_pos = map.map_to_camera(pos.location, cam_orig);
grid[view_pos.y][view_pos.x] = entity_glyph.display;
}
});
// then generate the string to display, but this goes away soon
std::wstring result;
for(shiterator::each_row_t it{grid}; it.next();) {
result += grid[it.y][it.x];
if(it.row) result += '\n';
}
return result;
}