From 2997dc363b1cf68aa7b4831bfdc3c624439f63c0 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Mon, 21 Jul 2025 13:10:03 -0400 Subject: [PATCH] FoW is now moved into lighting so light determines what's seen not player's last position. Not sure if I like that though. --- gui/map_view.cpp | 19 +++---------------- gui/map_view.hpp | 1 - lights.cpp | 6 ++++-- lights.hpp | 1 + systems.cpp | 10 +++++++--- systems.hpp | 4 ++-- tests/map.cpp | 8 ++------ 7 files changed, 19 insertions(+), 30 deletions(-) diff --git a/gui/map_view.cpp b/gui/map_view.cpp index 0816a39..64c78ab 100644 --- a/gui/map_view.cpp +++ b/gui/map_view.cpp @@ -24,8 +24,7 @@ namespace gui { $level(level), $map_render(std::make_shared()), $map_sprite($map_render->getTexture()), - $map_tiles(matrix::make(MAP_WIDTH, MAP_HEIGHT)), - $fow(matrix::make($level.map->width(), $level.map->height())) + $map_tiles(matrix::make(MAP_WIDTH, MAP_HEIGHT)) { auto player = $level.world->get_the(); $player_display = $level.world->get(player.entity).display; @@ -33,7 +32,6 @@ namespace gui { void MapViewUI::update_level(GameLevel &level) { $level = level; - $fow = matrix::make($level.map->width(), $level.map->height()); } void MapViewUI::init() { @@ -62,19 +60,8 @@ namespace gui { void MapViewUI::render(sf::RenderWindow &window, int compass_dir) { $gui.render(window); - - auto player = $level.world->get_the(); - auto player_pos = $level.world->get(player.entity).location; - - // NOTE: FoW is probably better done in the lighting system, because it illuminates where you've been. The light calcs could then simply set the light the player touches to 1 when it's run. - for(matrix::circle it{$fow, player_pos, 2.5}; it.next();) { - for(int x = it.left; x < it.right; x++) { - $fow[it.y][x] = 1; - } - } - - System::draw_map($level, $map_tiles, $fow, $entity_map); - System::render_map($map_tiles, $entity_map, *$map_render, compass_dir, $player_display); + System::draw_map($level, $map_tiles, $entity_map); + System::render_map($level, $map_tiles, $entity_map, *$map_render, compass_dir, $player_display); $map_sprite.setTexture($map_render->getTexture(), true); window.draw($map_sprite); // $gui.debug_layout(window); diff --git a/gui/map_view.hpp b/gui/map_view.hpp index 8688c0a..b5d9836 100644 --- a/gui/map_view.hpp +++ b/gui/map_view.hpp @@ -17,7 +17,6 @@ namespace gui { std::shared_ptr $map_render; sf::Sprite $map_sprite; matrix::Matrix $map_tiles; - matrix::Matrix $fow; MapViewUI(GameLevel &level); void init(); diff --git a/lights.cpp b/lights.cpp index 4867473..9fcf6df 100644 --- a/lights.cpp +++ b/lights.cpp @@ -12,7 +12,8 @@ namespace lighting { $height(matrix::height(tiles)), $lightmap(matrix::make($width, $height)), $ambient(matrix::make($width, $height)), - $paths($width, $height) + $paths($width, $height), + $fow(matrix::make($width, $height)) { auto& tile_ambient = textures::get_ambient_light(); @@ -47,8 +48,9 @@ namespace lighting { for(auto point : has_light) { for(matrix::compass it{$lightmap, point.x, point.y}; it.next();) { if($paths.$paths[it.y][it.x] == WALL_PATH_LIMIT) { - $lightmap[it.y][it.x] = light_level(source.strength, 1.5f, point.x, point.y); + $lightmap[it.y][it.x] = light_level(source.strength, 1.5f, point.x, point.y); } + $fow[it.y][it.x] = $lightmap[it.y][it.x]; } } } diff --git a/lights.hpp b/lights.hpp index a4e4298..d5bbfa6 100644 --- a/lights.hpp +++ b/lights.hpp @@ -21,6 +21,7 @@ namespace lighting { Matrix $lightmap; Matrix $ambient; Pathing $paths; + matrix::Matrix $fow; LightRender(Matrix& walls); diff --git a/systems.cpp b/systems.cpp index f9f11ec..9449851 100644 --- a/systems.cpp +++ b/systems.cpp @@ -526,9 +526,10 @@ bool System::inventory_occupied(GameLevel& level, Entity container_id, const std } -void System::draw_map(GameLevel& level, Matrix& grid, Matrix& fow, EntityGrid& entity_map) { +void System::draw_map(GameLevel& level, Matrix& grid, EntityGrid& entity_map) { World &world = *level.world; Map &map = *level.map; + Matrix &fow = level.lights->$fow; size_t view_x = matrix::width(grid) - 1; size_t view_y = matrix::height(grid) - 1; @@ -571,13 +572,15 @@ void System::draw_map(GameLevel& level, Matrix& grid, Matrix& fow, EntityGrid& e }); } - -void System::render_map(Matrix& tiles, EntityGrid& entity_map, sf::RenderTexture& render, int compass_dir, wchar_t player_display) { +void System::render_map(GameLevel& level, Matrix& tiles, EntityGrid& entity_map, sf::RenderTexture& render, int compass_dir, wchar_t player_display) { sf::Vector2i tile_sprite_dim{MAP_TILE_DIM,MAP_TILE_DIM}; unsigned int width = matrix::width(tiles); unsigned int height = matrix::height(tiles); sf::Vector2u dim{width * tile_sprite_dim.x, height * tile_sprite_dim.y}; auto render_size = render.getSize(); + Matrix &fow = level.lights->$fow; + (void)level; + (void)fow; if(render_size.x != width || render_size.y != height) { bool worked = render.resize(dim); @@ -608,6 +611,7 @@ void System::render_map(Matrix& tiles, EntityGrid& entity_map, sf::RenderTexture sprite.setPosition({float(point.x * tile_sprite_dim.x), float(point.y * tile_sprite_dim.y)}); } + render.draw(sprite); } diff --git a/systems.hpp b/systems.hpp index 6fe0fc0..3a95793 100644 --- a/systems.hpp +++ b/systems.hpp @@ -39,6 +39,6 @@ namespace System { void inventory_swap(GameLevel &level, Entity container_id, const std::string& a_name, const std::string &b_name); bool inventory_occupied(GameLevel& level, Entity container_id, const std::string& name); - void draw_map(GameLevel& level, Matrix& grid, Matrix& fow, EntityGrid& entity_map); - void render_map(Matrix& tiles, EntityGrid& entity_map, sf::RenderTexture& render, int compass_dir, wchar_t player_display); + void draw_map(GameLevel& level, Matrix& grid, EntityGrid& entity_map); + void render_map(GameLevel& level, Matrix& tiles, EntityGrid& entity_map, sf::RenderTexture& render, int compass_dir, wchar_t player_display); } diff --git a/tests/map.cpp b/tests/map.cpp index 5befe84..04d9dcc 100644 --- a/tests/map.cpp +++ b/tests/map.cpp @@ -100,12 +100,8 @@ TEST_CASE("map image test", "[map-sprite]") { for(matrix::each_row it{level.map->walls()}; it.next();) { player_pos.location.x = it.x; player_pos.location.y = it.y; - size_t width = level.map->width(); - size_t height = level.map->height(); - - Matrix fow = matrix::make(width, height); - System::draw_map(level, map_tiles, fow, entity_map); - System::render_map(map_tiles, entity_map, *render, 2, player_display); + System::draw_map(level, map_tiles, entity_map); + System::render_map(level, map_tiles, entity_map, *render, 2, player_display); #ifdef TEST_RENDER // confirm we get two different maps