From 33813c135f0d3a68b2bd666225810d36b4488d5f Mon Sep 17 00:00:00 2001 From: Jan Mrna Date: Sat, 27 Sep 2025 14:31:02 +0200 Subject: [PATCH] Move pathfindingdemo implementation to pathfindingdemo.cpp --- cpp/Makefile | 2 +- cpp/src/map.hpp | 5 ++ cpp/src/pathfindingdemo.cpp | 91 ++++++++++++++++++++++++++++++++++ cpp/src/pathfindingdemo.hpp | 98 +++++-------------------------------- 4 files changed, 109 insertions(+), 87 deletions(-) diff --git a/cpp/Makefile b/cpp/Makefile index 82782da..88d2071 100644 --- a/cpp/Makefile +++ b/cpp/Makefile @@ -3,7 +3,7 @@ all: test pathfinding # TODO linter? pathfinding: - g++ -Wall -ggdb3 -lSDL3 -lSDL3_image -std=c++23 -lGLEW -lGL -o pathfinding src/main.cpp src/sprite.cpp src/entities.cpp src/gameloop.cpp src/map.cpp + g++ -Wall -ggdb3 -lSDL3 -lSDL3_image -std=c++23 -lGLEW -lGL -o pathfinding src/main.cpp src/sprite.cpp src/entities.cpp src/gameloop.cpp src/map.cpp src/pathfindingdemo.cpp test: src/test.cpp src/array.hpp g++ -Wall -Wextra -Wpedantic -ggdb3 -std=c++23 -lgtest -o test src/test.cpp diff --git a/cpp/src/map.hpp b/cpp/src/map.hpp index b63b17e..c284994 100644 --- a/cpp/src/map.hpp +++ b/cpp/src/map.hpp @@ -14,6 +14,11 @@ public: Map(int rows, int cols); Map() : Map(0, 0) {} + Map(const Map&) = delete; + Map(Map&&) = delete; + Map& operator=(const Map&) = delete; + Map& operator=(Map&&) = delete; + const TileGrid &GetMapTiles() const { return m_Tiles; } WorldPos TileToWorld(TilePos p) const; diff --git a/cpp/src/pathfindingdemo.cpp b/cpp/src/pathfindingdemo.cpp index e69de29..e704730 100644 --- a/cpp/src/pathfindingdemo.cpp +++ b/cpp/src/pathfindingdemo.cpp @@ -0,0 +1,91 @@ +#include +#include +#include +#include + +#include "pathfindingdemo.hpp" + +#include "entities.hpp" +#include "log.hpp" +#include "map.hpp" +#include "user_input.hpp" + +PathFindingDemo::PathFindingDemo(int width, int height) : m_Map(width, height) { + LOG_DEBUG("."); +} + +PathFindingDemo::~PathFindingDemo() { LOG_DEBUG("."); } + +void PathFindingDemo::AddEntity(std::shared_ptr e) { + // TODO emplace_back + m_Entities.push_back(e); +} + +void PathFindingDemo::CreateMap() { + m_Entities.clear(); + m_Player = std::make_shared(); + m_Player->SetPosition(WorldPos{200.0f, 200.0f}); + m_Entities.push_back(m_Player); +} + +WorldPos PathFindingDemo::GetRandomPosition() const { + return WorldPos{0.0, 0.0}; +} + +std::optional PathFindingDemo::GetMoveTarget() { + WorldPos current_player_pos = GetPlayer()->GetPosition(); + + if (m_MoveQueue.empty()) { + return {}; + } + + WorldPos next_player_pos = m_MoveQueue.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 + return {}; +} + +void PathFindingDemo::UpdatePlayerVelocity() { + auto player = GetPlayer(); + auto current_pos = player->GetPosition(); + double tile_velocity_coeff = m_Map.GetTileVelocityCoeff(current_pos); + auto next_pos = GetMoveTarget(); + auto velocity = WorldPos{}; + if (next_pos) { + velocity = next_pos.value() - current_pos; + velocity.normalize(); + LOG_DEBUG("I want to move to: ", next_pos.value(), + ", velocity: ", velocity); + } + player->SetActualVelocity(velocity * tile_velocity_coeff); + float time_delta = 1.0f; + player->Update(time_delta); +} + +void PathFindingDemo::HandleActions(const std::vector &actions) { + for (const auto &action : 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) { + WorldPos wp = action.Argument.position; + TilePos p = m_Map.WorldToTile(wp); + LOG_INFO("Clearing current move queue and inserting new target: ", wp); + std::queue empty; + std::swap(empty, m_MoveQueue); + m_MoveQueue.push(wp); + } + }; +} diff --git a/cpp/src/pathfindingdemo.hpp b/cpp/src/pathfindingdemo.hpp index 4b52332..afa0e77 100644 --- a/cpp/src/pathfindingdemo.hpp +++ b/cpp/src/pathfindingdemo.hpp @@ -6,107 +6,33 @@ #include #include "entities.hpp" +#include "log.hpp" #include "map.hpp" #include "user_input.hpp" -#include "log.hpp" class PathFindingDemo { public: - PathFindingDemo(int width, int height) - : m_Width(width), m_Height(height), // TODO delete width, height - m_Map(width, height) { - LOG_DEBUG("."); - } - - ~PathFindingDemo() { LOG_DEBUG("."); } + PathFindingDemo(int width, int height); + ~PathFindingDemo(); PathFindingDemo(const PathFindingDemo &m) = delete; PathFindingDemo(PathFindingDemo &&m) = delete; - - void AddEntity(std::shared_ptr e) { - // TODO emplace_back - m_Entities.push_back(e); - } - - void CreateMap() { - m_Entities.clear(); - m_Player = std::make_shared(); - m_Player->SetPosition(Vec2D{200.0f, 200.0f}); - m_Entities.push_back(m_Player); - } + PathFindingDemo &operator=(const PathFindingDemo &) = delete; + PathFindingDemo &operator=(PathFindingDemo &&) = delete; std::shared_ptr GetPlayer() { return m_Player; } - - Vec2D GetRandomPosition() const { return Vec2D{0.0, 0.0}; } - std::vector> &GetEntities() { return m_Entities; } - - std::optional GetMoveTarget() { - WorldPos current_player_pos = GetPlayer()->GetPosition(); - - if (m_MoveQueue.empty()) { - return {}; - } - - WorldPos next_player_pos = m_MoveQueue.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 - return {}; - } - - - void UpdatePlayerVelocity() - { - auto player = GetPlayer(); - auto current_pos = player->GetPosition(); - double tile_velocity_coeff = m_Map.GetTileVelocityCoeff(current_pos); - auto next_pos = GetMoveTarget(); - auto velocity = WorldPos{}; - if (next_pos) { - velocity = next_pos.value() - current_pos; - velocity.normalize(); - LOG_DEBUG("I want to move to: ", next_pos.value(), ", velocity: ", velocity); - } - player->SetActualVelocity(velocity * tile_velocity_coeff); - float time_delta = 1.0f; - player->Update(time_delta); - } - - void HandleActions(const std::vector &actions) { - for (const auto &action : 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) { - WorldPos wp = action.Argument.position; - TilePos p = m_Map.WorldToTile(wp); - LOG_INFO("Clearing current move queue and inserting new target: ", wp); - std::queue empty; - std::swap(empty, m_MoveQueue); - m_MoveQueue.push(wp); - } - }; - } - const Map &GetMap() const { return m_Map; } - bool IsExitRequested() const { return m_ExitRequested; } + void AddEntity(std::shared_ptr e); + void CreateMap(); + std::optional GetMoveTarget(); + void UpdatePlayerVelocity(); + void HandleActions(const std::vector &actions); + WorldPos GetRandomPosition() const; + private: - int m_Width; - int m_Height; bool m_ExitRequested = false; std::vector> m_Entities; std::shared_ptr m_Player;