Now added a System::multi_path which can target to multiple entities.
This commit is contained in:
parent
fc678c6b42
commit
4365aa4bfc
2 changed files with 25 additions and 21 deletions
22
systems.hpp
22
systems.hpp
|
@ -3,6 +3,7 @@
|
||||||
#include <SFML/Graphics/RenderTexture.hpp>
|
#include <SFML/Graphics/RenderTexture.hpp>
|
||||||
#include "map.hpp"
|
#include "map.hpp"
|
||||||
#include "spatialmap.hpp"
|
#include "spatialmap.hpp"
|
||||||
|
#include "game_level.hpp"
|
||||||
|
|
||||||
namespace System {
|
namespace System {
|
||||||
using namespace components;
|
using namespace components;
|
||||||
|
@ -43,4 +44,25 @@ namespace System {
|
||||||
|
|
||||||
void set_position(DinkyECS::World& world, SpatialMap& collision, Entity entity, Position pos);
|
void set_position(DinkyECS::World& world, SpatialMap& collision, Entity entity, Position pos);
|
||||||
bool use_item(const std::string& slot_name);
|
bool use_item(const std::string& slot_name);
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void multi_path(GameDB::Level& level, Pathing& paths, Matrix& walls) {
|
||||||
|
// first, put everything of this type as a target
|
||||||
|
level.world->query<Position, T>(
|
||||||
|
[&](const auto ent, auto& position, auto&) {
|
||||||
|
if(ent != level.player) {
|
||||||
|
paths.set_target(position.location);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
level.world->query<Collision>(
|
||||||
|
[&](const auto ent, auto& collision) {
|
||||||
|
if(collision.has && ent != level.player) {
|
||||||
|
auto& pos = level.world->get<Position>(ent);
|
||||||
|
walls[pos.location.y][pos.location.x] = WALL_VALUE;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
paths.compute_paths(walls);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include "rand.hpp"
|
#include "rand.hpp"
|
||||||
|
#include "systems.hpp"
|
||||||
|
|
||||||
using namespace fmt;
|
using namespace fmt;
|
||||||
using namespace nlohmann;
|
using namespace nlohmann;
|
||||||
|
@ -23,32 +24,13 @@ json load_test_pathing(const string &fname) {
|
||||||
|
|
||||||
TEST_CASE("multiple targets can path", "[pathing]") {
|
TEST_CASE("multiple targets can path", "[pathing]") {
|
||||||
GameDB::init();
|
GameDB::init();
|
||||||
|
|
||||||
auto level = GameDB::create_level();
|
auto level = GameDB::create_level();
|
||||||
auto& walls_original = level.map->$walls;
|
auto walls_copy = level.map->$walls;
|
||||||
auto walls_copy = walls_original;
|
|
||||||
|
|
||||||
Pathing paths{matrix::width(walls_copy), matrix::height(walls_copy)};
|
Pathing paths{matrix::width(walls_copy), matrix::height(walls_copy)};
|
||||||
|
|
||||||
// first, put everything of this type as a target
|
System::multi_path<Combat>(level, paths, walls_copy);
|
||||||
level.world->query<Position, Combat>(
|
|
||||||
[&](const auto ent, auto& position, auto&) {
|
|
||||||
if(ent != level.player) {
|
|
||||||
paths.set_target(position.location);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
level.world->query<Collision>(
|
|
||||||
[&](const auto ent, auto& collision) {
|
|
||||||
if(collision.has && ent != level.player) {
|
|
||||||
auto& pos = level.world->get<Position>(ent);
|
|
||||||
walls_copy[pos.location.y][pos.location.x] = WALL_VALUE;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
paths.compute_paths(walls_copy);
|
|
||||||
bool diag = Random::uniform<int>(0, 1);
|
bool diag = Random::uniform<int>(0, 1);
|
||||||
|
|
||||||
auto pos = GameDB::player_position().location;
|
auto pos = GameDB::player_position().location;
|
||||||
auto found = paths.find_path(pos, PATHING_TOWARD, diag);
|
auto found = paths.find_path(pos, PATHING_TOWARD, diag);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue