Pathfinder WIP
This commit is contained in:
@@ -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);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user