From 4365aa4bfc9c21cc58747efaa5120d8884d869e5 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Sun, 31 Aug 2025 00:39:35 -0400 Subject: [PATCH] Now added a System::multi_path which can target to multiple entities. --- systems.hpp | 22 ++++++++++++++++++++++ tests/pathing.cpp | 24 +++--------------------- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/systems.hpp b/systems.hpp index 9995a5f..81c5238 100644 --- a/systems.hpp +++ b/systems.hpp @@ -3,6 +3,7 @@ #include #include "map.hpp" #include "spatialmap.hpp" +#include "game_level.hpp" namespace System { using namespace components; @@ -43,4 +44,25 @@ namespace System { void set_position(DinkyECS::World& world, SpatialMap& collision, Entity entity, Position pos); bool use_item(const std::string& slot_name); + + template + void multi_path(GameDB::Level& level, Pathing& paths, Matrix& walls) { + // first, put everything of this type as a target + level.world->query( + [&](const auto ent, auto& position, auto&) { + if(ent != level.player) { + paths.set_target(position.location); + } + }); + + level.world->query( + [&](const auto ent, auto& collision) { + if(collision.has && ent != level.player) { + auto& pos = level.world->get(ent); + walls[pos.location.y][pos.location.x] = WALL_VALUE; + } + }); + + paths.compute_paths(walls); + } } diff --git a/tests/pathing.cpp b/tests/pathing.cpp index aa9c9cd..4bd32a2 100644 --- a/tests/pathing.cpp +++ b/tests/pathing.cpp @@ -9,6 +9,7 @@ #include #include #include "rand.hpp" +#include "systems.hpp" using namespace fmt; using namespace nlohmann; @@ -23,32 +24,13 @@ json load_test_pathing(const string &fname) { TEST_CASE("multiple targets can path", "[pathing]") { GameDB::init(); - auto level = GameDB::create_level(); - auto& walls_original = level.map->$walls; - auto walls_copy = walls_original; - + auto walls_copy = level.map->$walls; Pathing paths{matrix::width(walls_copy), matrix::height(walls_copy)}; - // first, put everything of this type as a target - level.world->query( - [&](const auto ent, auto& position, auto&) { - if(ent != level.player) { - paths.set_target(position.location); - } - }); + System::multi_path(level, paths, walls_copy); - level.world->query( - [&](const auto ent, auto& collision) { - if(collision.has && ent != level.player) { - auto& pos = level.world->get(ent); - walls_copy[pos.location.y][pos.location.x] = WALL_VALUE; - } - }); - - paths.compute_paths(walls_copy); bool diag = Random::uniform(0, 1); - auto pos = GameDB::player_position().location; auto found = paths.find_path(pos, PATHING_TOWARD, diag);