From 0d1eacdc5c231b87b8f3c05c94ec899997b7451e Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Sat, 12 Jul 2025 14:46:42 -0400 Subject: [PATCH] Now entities are drawn after the map so that there's no holes. --- map.hpp | 2 ++ systems.cpp | 18 ++++++++---------- systems.hpp | 2 +- tests/map.cpp | 15 ++++++++++++--- 4 files changed, 23 insertions(+), 14 deletions(-) diff --git a/map.hpp b/map.hpp index 305682d..622009c 100644 --- a/map.hpp +++ b/map.hpp @@ -20,6 +20,8 @@ struct Room { size_t height = 0; }; +using EntityGrid = std::unordered_map; + class Map { public: size_t $width; diff --git a/systems.cpp b/systems.cpp index 0679800..a6b699e 100644 --- a/systems.cpp +++ b/systems.cpp @@ -407,13 +407,15 @@ void System::plan_motion(World& world, Position move_to) { motion.dy = move_to.location.y - player_position.location.y; } -void System::draw_map(GameLevel& level, Matrix& grid, int compass_dir) { +void System::draw_map(GameLevel& level, Matrix& grid, EntityGrid& entity_map, int compass_dir) { (void)compass_dir; World &world = *level.world; Map &map = *level.map; size_t view_x = matrix::width(grid) - 1; size_t view_y = matrix::height(grid) - 1; + entity_map.clear(); + auto player_pos = world.get(level.player).location; Point cam_orig = map.center_camera(player_pos, view_x, view_y); auto &tiles = map.tiles(); @@ -436,17 +438,13 @@ void System::draw_map(GameLevel& level, Matrix& grid, int compass_dir) { } // then get the enemy/item/device tiles and fill those in - world.query([&](auto ent, auto &pos, auto &entity_glyph) { + world.query([&](auto, auto &pos, auto &entity_glyph) { + // BUG: don't I have a within bounds macro somewhere? 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) { + && pos.location.y >= cam_orig.y && pos.location.y <= cam_orig.y + view_y) + { Point view_pos = map.map_to_camera(pos.location, cam_orig); - - if(ent == level.player) { - // grid[view_pos.y][view_pos.x] = COMPASS[compass_dir][0]; - grid[view_pos.y][view_pos.x] = 41981; - } else { - grid[view_pos.y][view_pos.x] = entity_glyph.display; - } + entity_map.insert_or_assign(view_pos, entity_glyph.display); } }); } diff --git a/systems.hpp b/systems.hpp index 564efb8..8ae3f2b 100644 --- a/systems.hpp +++ b/systems.hpp @@ -19,7 +19,7 @@ namespace System { void init_positions(World &world, SpatialMap &collider); void device(World &world, Entity actor, Entity item); void plan_motion(World& world, Position move_to); - void draw_map(GameLevel& level, Matrix& grid, int compass_dir); + void draw_map(GameLevel& level, Matrix& grid, EntityGrid& entity_map, int compass_dir); Entity spawn_item(World& world, const string& name); bool drop_item(GameLevel& level, Entity item); diff --git a/tests/map.cpp b/tests/map.cpp index cd3627f..8bd4e3e 100644 --- a/tests/map.cpp +++ b/tests/map.cpp @@ -81,7 +81,8 @@ TEST_CASE("dijkstra algo test", "[map]") { } } -sf::Sprite render_map(Matrix& tiles, sf::RenderTexture& render) { +sf::Sprite render_map(Matrix& tiles, EntityGrid& entity_map, sf::RenderTexture& render) { + (void) entity_map; sf::Vector2i size{MAP_TILE_DIM,MAP_TILE_DIM}; sf::Vector2u dim{ @@ -101,6 +102,12 @@ sf::Sprite render_map(Matrix& tiles, sf::RenderTexture& render) { render.draw(sprite); } + for(auto [point, display] : entity_map) { + auto& sprite = textures::get_map_sprite(display); + sprite.setPosition({float(point.x * size.x), float(point.y * size.y)}); + render.draw(sprite); + } + render.display(); return sf::Sprite{render.getTexture()}; } @@ -112,6 +119,8 @@ TEST_CASE("map image test", "[map-sprite]") { LevelManager levels; GameLevel level = levels.current(); Matrix map_tiles = matrix::make(7,7); + EntityGrid entity_map; + auto render = std::make_shared(); auto player = level.world->get_the(); auto& player_pos = level.world->get(player.entity); @@ -120,9 +129,9 @@ TEST_CASE("map image test", "[map-sprite]") { player_pos.location.x = it.x; player_pos.location.y = it.y; - System::draw_map(level, map_tiles, 2); + System::draw_map(level, map_tiles, entity_map, 2); // on level start make one render texture with the base map - auto map_sprite = render_map(map_tiles, *render); + auto map_sprite = render_map(map_tiles, entity_map, *render); } /*