Pathfinder WIP

This commit is contained in:
Jan Mrna
2025-09-27 17:58:26 +02:00
parent 9b08c057e0
commit ea316ab997
5 changed files with 74 additions and 59 deletions

View File

@@ -9,9 +9,14 @@
#include "log.hpp"
#include "map.hpp"
#include "user_input.hpp"
#include "pathfinder.hpp"
PathFindingDemo::PathFindingDemo(int width, int height) : m_Map(width, height) {
PathFindingDemo::PathFindingDemo(int width, int height) :
m_Map(width, height)
{
LOG_DEBUG(".");
// set default pathfinder method
m_PathFinder = pathfinder::create(pathfinder::PathFinderType::LINEAR);
}
PathFindingDemo::~PathFindingDemo() { LOG_DEBUG("."); }
@@ -35,19 +40,21 @@ WorldPos PathFindingDemo::GetRandomPosition() const {
std::optional<WorldPos> PathFindingDemo::GetMoveTarget() {
WorldPos current_player_pos = GetPlayer()->GetPosition();
if (m_MoveQueue.empty()) {
if (m_Path.empty()) {
return {};
}
WorldPos next_player_pos = m_MoveQueue.front();
WorldPos next_player_pos = m_Path.front();
if (current_player_pos.distance(next_player_pos) > 10.0) {
// target not reached yet
return next_player_pos;
}
// target reached, pop it
m_MoveQueue.pop();
// return nothing - we'll get the next value in the next iteration
//m_MoveQueue.pop();
m_Path.erase(m_Path.begin());
// return nothing - if there's next point in the queue,
// we'll get it in the next iteration
return {};
}
@@ -56,7 +63,7 @@ void PathFindingDemo::UpdatePlayerVelocity() {
auto current_pos = player->GetPosition();
double tile_velocity_coeff = m_Map.GetTileVelocityCoeff(current_pos);
auto next_pos = GetMoveTarget();
auto velocity = WorldPos{};
WorldPos velocity = WorldPos{};
if (next_pos) {
velocity = next_pos.value() - current_pos;
velocity.normalize();
@@ -73,19 +80,15 @@ void PathFindingDemo::HandleActions(const std::vector<UserAction> &actions) {
if (action.type == UserAction::Type::EXIT) {
LOG_INFO("Exit requested");
m_ExitRequested = true;
} else if (action.type == UserAction::Type::FIRE) {
LOG_INFO("Fire");
// AddEntity(m_Player->CreateBomb());
} else if (action.type == UserAction::Type::MOVE) {
LOG_INFO("Move direction ", action.Argument.position);
m_Player->SetRequestedVelocity(action.Argument.position * 4.0f);
} else if (action.type == UserAction::Type::MOVE_TARGET) {
} else if (action.type == UserAction::Type::SET_MOVE_TARGET) {
WorldPos wp = action.Argument.position;
TilePos p = m_Map.WorldToTile(wp);
LOG_INFO("Clearing current move queue and inserting new target: ", wp);
std::queue<WorldPos> empty;
std::swap(empty, m_MoveQueue);
m_MoveQueue.push(wp);
LOG_INFO("Calculating path to target: ", wp);
m_Path = m_PathFinder->CalculatePath(wp);
LOG_INFO("Done, path node count: ", m_Path.size());
} else if (action.type == UserAction::Type::SELECT_PATHFINDER) {
using namespace pathfinder;
PathFinderType type = static_cast<PathFinderType>(action.Argument.number);
m_PathFinder = create(type);
}
};
}