Re-formatted files
This commit is contained in:
@@ -1,45 +1,35 @@
|
|||||||
#include "camera.hpp"
|
#include "camera.hpp"
|
||||||
#include "math.hpp"
|
|
||||||
#include "log.hpp"
|
#include "log.hpp"
|
||||||
|
#include "math.hpp"
|
||||||
|
|
||||||
// for now only pass-through placeholder functions,
|
// for now only pass-through placeholder functions,
|
||||||
// since we draw the whole map
|
// since we draw the whole map
|
||||||
|
|
||||||
|
void Camera::Pan(const WorldPos &delta) { m_Pan += (delta / m_Zoom); }
|
||||||
|
|
||||||
void Camera::Pan(const WorldPos& delta)
|
void Camera::Zoom(float delta) {
|
||||||
{
|
|
||||||
m_Pan += (delta / m_Zoom);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Camera::Zoom(float delta)
|
|
||||||
{
|
|
||||||
constexpr float ZOOM_SCALE = 0.1f;
|
constexpr float ZOOM_SCALE = 0.1f;
|
||||||
m_Zoom += delta * ZOOM_SCALE;
|
m_Zoom += delta * ZOOM_SCALE;
|
||||||
LOG_DEBUG("Zoom: ", m_Zoom);
|
LOG_DEBUG("Zoom: ", m_Zoom);
|
||||||
}
|
}
|
||||||
|
|
||||||
WindowPos Camera::WorldToWindow(WorldPos world) const
|
WindowPos Camera::WorldToWindow(WorldPos world) const {
|
||||||
{
|
const auto &v = world + m_Pan;
|
||||||
const auto& v = world + m_Pan;
|
|
||||||
return WindowPos{v[0], v[1]} * m_Zoom;
|
return WindowPos{v[0], v[1]} * m_Zoom;
|
||||||
}
|
}
|
||||||
|
|
||||||
WorldPos Camera::WindowToWorld(WindowPos window) const
|
WorldPos Camera::WindowToWorld(WindowPos window) const {
|
||||||
{
|
|
||||||
window /= m_Zoom;
|
window /= m_Zoom;
|
||||||
return WorldPos{window[0], window[1]} - m_Pan;
|
return WorldPos{window[0], window[1]} - m_Pan;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
WindowSize Camera::WorldToWindowSize(WorldSize world) const {
|
||||||
WindowSize Camera::WorldToWindowSize(WorldSize world) const
|
const auto &v = world;
|
||||||
{
|
|
||||||
const auto& v = world;
|
|
||||||
// no zoom yet, just pass-through
|
// no zoom yet, just pass-through
|
||||||
return WindowSize{v[0], v[1]} * m_Zoom;
|
return WindowSize{v[0], v[1]} * m_Zoom;
|
||||||
}
|
}
|
||||||
|
|
||||||
WorldSize Camera::WindowToWorldSize(WindowSize window) const
|
WorldSize Camera::WindowToWorldSize(WindowSize window) const {
|
||||||
{
|
window /= m_Zoom;
|
||||||
window /= m_Zoom;
|
return WorldSize{window[0], window[1]};
|
||||||
return WorldSize{window[0], window[1]};
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,28 +2,27 @@
|
|||||||
|
|
||||||
#include "math.hpp"
|
#include "math.hpp"
|
||||||
|
|
||||||
class Camera
|
class Camera {
|
||||||
{
|
|
||||||
public:
|
public:
|
||||||
void Pan(const WorldPos& delta);
|
void Pan(const WorldPos &delta);
|
||||||
void Zoom(float delta);
|
void Zoom(float delta);
|
||||||
|
|
||||||
WorldPos GetPan() const { return m_Pan; }
|
WorldPos GetPan() const { return m_Pan; }
|
||||||
float GetZoom() const { return m_Zoom; }
|
float GetZoom() const { return m_Zoom; }
|
||||||
|
|
||||||
WindowPos WorldToWindow(WorldPos) const;
|
WindowPos WorldToWindow(WorldPos) const;
|
||||||
WorldPos WindowToWorld(WindowPos) const;
|
WorldPos WindowToWorld(WindowPos) const;
|
||||||
WindowSize WorldToWindowSize(WorldSize) const;
|
WindowSize WorldToWindowSize(WorldSize) const;
|
||||||
WorldSize WindowToWorldSize(WindowSize) const;
|
WorldSize WindowToWorldSize(WindowSize) const;
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
requires std::floating_point<T>
|
requires std::floating_point<T>
|
||||||
T WindowToWorldSize(T window_size) const {
|
T WindowToWorldSize(T window_size) const {
|
||||||
return window_size / static_cast<T>(m_Zoom);
|
return window_size / static_cast<T>(m_Zoom);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
requires std::floating_point<T>
|
requires std::floating_point<T>
|
||||||
T WorldToWindowSize(T world_size) const {
|
T WorldToWindowSize(T world_size) const {
|
||||||
return world_size * static_cast<T>(m_Zoom);
|
return world_size * static_cast<T>(m_Zoom);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,7 +30,8 @@ void Entity::ZeroActualVelocityInDirection(WorldPos direction) {
|
|||||||
|
|
||||||
// q1 * e1 + q2 * e2 = v, from this follows:
|
// q1 * e1 + q2 * e2 = v, from this follows:
|
||||||
auto &v = GetActualVelocity();
|
auto &v = GetActualVelocity();
|
||||||
float q2 = (v.y() * e1.x() - v.x() * e1.y()) / (e2.y() * e1.x() - e2.x() * e1.y());
|
float q2 =
|
||||||
|
(v.y() * e1.x() - v.x() * e1.y()) / (e2.y() * e1.x() - e2.x() * e1.y());
|
||||||
float q1 = (v.x() - q2 * e2.x()) / e1.x();
|
float q1 = (v.x() - q2 * e2.x()) / e1.x();
|
||||||
|
|
||||||
// We then zero-out the q1, but only if it's positive - meaning
|
// We then zero-out the q1, but only if it's positive - meaning
|
||||||
@@ -46,9 +47,8 @@ void Entity::Update(float time_delta) {
|
|||||||
m_Position += m_ActualVelocity * time_delta;
|
m_Position += m_ActualVelocity * time_delta;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::optional<WorldPos> Entity::GetMoveTarget()
|
std::optional<WorldPos> Entity::GetMoveTarget() {
|
||||||
{
|
auto &path = GetPath();
|
||||||
auto& path = GetPath();
|
|
||||||
if (path.empty()) {
|
if (path.empty()) {
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
@@ -61,27 +61,24 @@ std::optional<WorldPos> Entity::GetMoveTarget()
|
|||||||
return next_pos;
|
return next_pos;
|
||||||
}
|
}
|
||||||
// target reached, pop it
|
// target reached, pop it
|
||||||
//m_MoveQueue.pop();
|
// m_MoveQueue.pop();
|
||||||
path.erase(path.begin());
|
path.erase(path.begin());
|
||||||
// return nothing - if there's next point in the queue,
|
// return nothing - if there's next point in the queue,
|
||||||
// we'll get it in the next iteration
|
// we'll get it in the next iteration
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Entity::CollidesWith(const Entity& other) const
|
bool Entity::CollidesWith(const Entity &other) const {
|
||||||
{
|
const auto &A = *this;
|
||||||
const auto& A = *this;
|
const auto &B = other;
|
||||||
const auto& B = other;
|
|
||||||
|
|
||||||
auto position_A = A.GetPosition();
|
auto position_A = A.GetPosition();
|
||||||
auto position_B = B.GetPosition();
|
auto position_B = B.GetPosition();
|
||||||
auto distance_sq = position_A.DistanceSquared(position_B);
|
auto distance_sq = position_A.DistanceSquared(position_B);
|
||||||
auto collision_distance_sq =
|
auto collision_distance_sq =
|
||||||
A.GetCollisionRadiusSquared() +
|
A.GetCollisionRadiusSquared() + B.GetCollisionRadiusSquared() +
|
||||||
B.GetCollisionRadiusSquared() +
|
|
||||||
2 * A.GetCollisionRadius() * B.GetCollisionRadius();
|
2 * A.GetCollisionRadius() * B.GetCollisionRadius();
|
||||||
if (distance_sq < collision_distance_sq)
|
if (distance_sq < collision_distance_sq) {
|
||||||
{
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -4,13 +4,13 @@
|
|||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string_view>
|
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
#include "log.hpp"
|
#include "log.hpp"
|
||||||
#include "math.hpp"
|
#include "math.hpp"
|
||||||
#include "sprite.hpp"
|
|
||||||
#include "pathfinder/base.hpp"
|
#include "pathfinder/base.hpp"
|
||||||
|
#include "sprite.hpp"
|
||||||
|
|
||||||
class Entity {
|
class Entity {
|
||||||
public:
|
public:
|
||||||
@@ -58,12 +58,12 @@ public:
|
|||||||
|
|
||||||
void ZeroActualVelocityInDirection(WorldPos direction);
|
void ZeroActualVelocityInDirection(WorldPos direction);
|
||||||
|
|
||||||
const pathfinder::Path& GetPath() const { return m_Path; }
|
const pathfinder::Path &GetPath() const { return m_Path; }
|
||||||
pathfinder::Path& GetPath() { return m_Path; }
|
pathfinder::Path &GetPath() { return m_Path; }
|
||||||
void SetPath(pathfinder::Path& path) { m_Path = path; }
|
void SetPath(pathfinder::Path &path) { m_Path = path; }
|
||||||
std::optional<WorldPos> GetMoveTarget();
|
std::optional<WorldPos> GetMoveTarget();
|
||||||
|
|
||||||
bool CollidesWith(const Entity& other) const;
|
bool CollidesWith(const Entity &other) const;
|
||||||
|
|
||||||
bool IsCollisionBoxVisible() const { return m_CollisionBoxVisible; }
|
bool IsCollisionBoxVisible() const { return m_CollisionBoxVisible; }
|
||||||
|
|
||||||
|
|||||||
@@ -21,18 +21,17 @@ void GameLoop::Draw() {
|
|||||||
TilePos{static_cast<int32_t>(row), static_cast<int32_t>(col)}));
|
TilePos{static_cast<int32_t>(row), static_cast<int32_t>(col)}));
|
||||||
const auto &size = camera.WorldToWindowSize(map.GetTileSize());
|
const auto &size = camera.WorldToWindowSize(map.GetTileSize());
|
||||||
// LOG_DEBUG("Drawing rect (", row, ", ", col, ")");
|
// LOG_DEBUG("Drawing rect (", row, ", ", col, ")");
|
||||||
m_Window->DrawFilledRect(position, size, tiles[row][col]->R, tiles[row][col]->G,
|
m_Window->DrawFilledRect(position, size, tiles[row][col]->R,
|
||||||
tiles[row][col]->B, tiles[row][col]->A);
|
tiles[row][col]->G, tiles[row][col]->B,
|
||||||
|
tiles[row][col]->A);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// draw the path, if it exists
|
// draw the path, if it exists
|
||||||
|
|
||||||
for (const auto& entity : m_Game->GetEntities())
|
for (const auto &entity : m_Game->GetEntities()) {
|
||||||
{
|
|
||||||
WorldPos start_pos = entity->GetPosition();
|
WorldPos start_pos = entity->GetPosition();
|
||||||
for (const auto &next_pos : entity->GetPath())
|
for (const auto &next_pos : entity->GetPath()) {
|
||||||
{
|
|
||||||
const auto &camera = m_Game->GetCamera();
|
const auto &camera = m_Game->GetCamera();
|
||||||
m_Window->DrawLine(camera.WorldToWindow(start_pos),
|
m_Window->DrawLine(camera.WorldToWindow(start_pos),
|
||||||
camera.WorldToWindow(next_pos));
|
camera.WorldToWindow(next_pos));
|
||||||
@@ -44,29 +43,24 @@ void GameLoop::Draw() {
|
|||||||
for (auto &entity : m_Game->GetEntities()) {
|
for (auto &entity : m_Game->GetEntities()) {
|
||||||
const auto &camera = m_Game->GetCamera();
|
const auto &camera = m_Game->GetCamera();
|
||||||
auto entity_pos = camera.WorldToWindow(entity->GetPosition());
|
auto entity_pos = camera.WorldToWindow(entity->GetPosition());
|
||||||
m_Window->DrawSprite(entity_pos,
|
m_Window->DrawSprite(entity_pos, entity->GetSprite(), camera.GetZoom());
|
||||||
entity->GetSprite(),
|
if (entity->IsCollisionBoxVisible()) {
|
||||||
camera.GetZoom());
|
float collision_radius =
|
||||||
if (entity->IsCollisionBoxVisible())
|
camera.WorldToWindowSize(entity->GetCollisionRadius());
|
||||||
{
|
|
||||||
float collision_radius = camera.WorldToWindowSize(entity->GetCollisionRadius());
|
|
||||||
m_Window->DrawCircle(entity_pos, collision_radius, 255, 0, 0);
|
m_Window->DrawCircle(entity_pos, collision_radius, 255, 0, 0);
|
||||||
}
|
}
|
||||||
if (entity->IsSelected())
|
if (entity->IsSelected()) {
|
||||||
{
|
float collision_radius =
|
||||||
float collision_radius = camera.WorldToWindowSize(entity->GetCollisionRadius());
|
camera.WorldToWindowSize(entity->GetCollisionRadius());
|
||||||
m_Window->DrawCircle(entity_pos, collision_radius, 0, 255, 0);
|
m_Window->DrawCircle(entity_pos, collision_radius, 0, 255, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// draw the selection box, if present
|
// draw the selection box, if present
|
||||||
if (m_Game->IsSelectionBoxActive())
|
if (m_Game->IsSelectionBoxActive()) {
|
||||||
{
|
const auto &[corner_pos, size] = m_Game->GetSelectionBoxPosSize();
|
||||||
const auto& [corner_pos, size] = m_Game->GetSelectionBoxPosSize();
|
|
||||||
m_Window->DrawRect(corner_pos, size, 200, 20, 20);
|
m_Window->DrawRect(corner_pos, size, 200, 20, 20);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO rethink coupling and dependencies in the game loop class
|
// TODO rethink coupling and dependencies in the game loop class
|
||||||
|
|||||||
@@ -3,24 +3,22 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
#include "pathfindingdemo.hpp"
|
#include "pathfindingdemo.hpp"
|
||||||
#include "window.hpp"
|
|
||||||
#include "user_input.hpp"
|
#include "user_input.hpp"
|
||||||
|
#include "window.hpp"
|
||||||
|
|
||||||
class GameLoop {
|
class GameLoop {
|
||||||
public:
|
public:
|
||||||
GameLoop() = default;
|
GameLoop() = default;
|
||||||
~GameLoop() = default;
|
~GameLoop() = default;
|
||||||
|
|
||||||
GameLoop(const GameLoop&) = delete;
|
GameLoop(const GameLoop &) = delete;
|
||||||
GameLoop(GameLoop&&) = delete;
|
GameLoop(GameLoop &&) = delete;
|
||||||
GameLoop& operator=(const GameLoop&) = delete;
|
GameLoop &operator=(const GameLoop &) = delete;
|
||||||
GameLoop& operator=(GameLoop&&) = delete;
|
GameLoop &operator=(GameLoop &&) = delete;
|
||||||
|
|
||||||
void Run();
|
void Run();
|
||||||
|
|
||||||
void SetGame(std::unique_ptr<PathFindingDemo> x) {
|
void SetGame(std::unique_ptr<PathFindingDemo> x) { m_Game = std::move(x); }
|
||||||
m_Game = std::move(x);
|
|
||||||
}
|
|
||||||
void SetWindow(std::unique_ptr<Window> x) { m_Window = std::move(x); }
|
void SetWindow(std::unique_ptr<Window> x) { m_Window = std::move(x); }
|
||||||
void SetUserInput(std::unique_ptr<UserInput> x) {
|
void SetUserInput(std::unique_ptr<UserInput> x) {
|
||||||
m_UserInput = std::move(x);
|
m_UserInput = std::move(x);
|
||||||
|
|||||||
@@ -3,11 +3,11 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#if defined(__GNUC__) || defined(__clang__)
|
#if defined(__GNUC__) || defined(__clang__)
|
||||||
# define PRETTY_FUNC __PRETTY_FUNCTION__
|
#define PRETTY_FUNC __PRETTY_FUNCTION__
|
||||||
#elif defined(_MSC_VER)
|
#elif defined(_MSC_VER)
|
||||||
# define PRETTY_FUNC __FUNCTION__
|
#define PRETTY_FUNC __FUNCTION__
|
||||||
#else
|
#else
|
||||||
# define PRETTY_FUNC __func__
|
#define PRETTY_FUNC __func__
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define LOG_CRITICAL(...) Log::critical(PRETTY_FUNC, ": ", __VA_ARGS__)
|
#define LOG_CRITICAL(...) Log::critical(PRETTY_FUNC, ": ", __VA_ARGS__)
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ Map::Map(int rows, int cols) : m_Cols(cols), m_Rows(rows) {
|
|||||||
for (size_t row = 0; row < m_Rows; row++) {
|
for (size_t row = 0; row < m_Rows; row++) {
|
||||||
m_Tiles.push_back(std::vector<const Tile *>{});
|
m_Tiles.push_back(std::vector<const Tile *>{});
|
||||||
for (size_t col = 0; col < m_Cols; col++) {
|
for (size_t col = 0; col < m_Cols; col++) {
|
||||||
m_Tiles[row].push_back(&tile_types.at(TileType::GRASS));
|
m_Tiles[row].push_back(&tile_types.at(TileType::GRASS));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -25,7 +25,8 @@ WorldPos Map::TileEdgeToWorld(TilePos p) const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TilePos Map::WorldToTile(WorldPos p) const {
|
TilePos Map::WorldToTile(WorldPos p) const {
|
||||||
return TilePos{static_cast<int32_t>(p.x() / TILE_SIZE), static_cast<int32_t>(p.y() / TILE_SIZE)};
|
return TilePos{static_cast<int32_t>(p.x() / TILE_SIZE),
|
||||||
|
static_cast<int32_t>(p.y() / TILE_SIZE)};
|
||||||
}
|
}
|
||||||
|
|
||||||
WorldSize Map::GetTileSize() const { return WorldSize{TILE_SIZE, TILE_SIZE}; }
|
WorldSize Map::GetTileSize() const { return WorldSize{TILE_SIZE, TILE_SIZE}; }
|
||||||
@@ -51,40 +52,38 @@ bool Map::IsTilePosValid(TilePos p) const {
|
|||||||
return row < m_Tiles.size() && col < m_Tiles[0].size();
|
return row < m_Tiles.size() && col < m_Tiles[0].size();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<TilePos> Map::GetNeighbors(TilePos center) const {
|
||||||
std::vector<TilePos> Map::GetNeighbors(TilePos center) const
|
|
||||||
{
|
|
||||||
std::vector<TilePos> neighbours;
|
std::vector<TilePos> neighbours;
|
||||||
neighbours.reserve(4);
|
neighbours.reserve(4);
|
||||||
|
|
||||||
std::array<TilePos, 4> candidates = {
|
std::array<TilePos, 4> candidates = {
|
||||||
center + TilePos{ 1, 0},
|
center + TilePos{1, 0},
|
||||||
center + TilePos{-1, 0},
|
center + TilePos{-1, 0},
|
||||||
center + TilePos{ 0, 1},
|
center + TilePos{0, 1},
|
||||||
center + TilePos{ 0, -1},
|
center + TilePos{0, -1},
|
||||||
};
|
};
|
||||||
|
|
||||||
for (const auto& c : candidates) {
|
for (const auto &c : candidates) {
|
||||||
if (IsTilePosValid(c))
|
if (IsTilePosValid(c))
|
||||||
neighbours.push_back(c);
|
neighbours.push_back(c);
|
||||||
}
|
}
|
||||||
return neighbours;
|
return neighbours;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Map::PaintCircle(TilePos center, unsigned radius, TileType tile_type) {
|
||||||
void Map::PaintCircle(TilePos center, unsigned radius, TileType tile_type)
|
|
||||||
{
|
|
||||||
// get rectangle that wraps the circle
|
// get rectangle that wraps the circle
|
||||||
TilePos corner1 = TilePos{center.x() - static_cast<int32_t>(radius), center.y() - static_cast<int32_t>(radius)};
|
TilePos corner1 = TilePos{center.x() - static_cast<int32_t>(radius),
|
||||||
TilePos corner2 = TilePos{center.x() + static_cast<int32_t>(radius), center.y() + static_cast<int32_t>(radius)};
|
center.y() - static_cast<int32_t>(radius)};
|
||||||
|
TilePos corner2 = TilePos{center.x() + static_cast<int32_t>(radius),
|
||||||
|
center.y() + static_cast<int32_t>(radius)};
|
||||||
// iterate through all valid points, setting the type
|
// iterate through all valid points, setting the type
|
||||||
const unsigned radius_squared = radius * radius;
|
const unsigned radius_squared = radius * radius;
|
||||||
for (int x = corner1.x(); x < corner2.x(); x++) {
|
for (int x = corner1.x(); x < corner2.x(); x++) {
|
||||||
for (int y = corner1.y(); y < corner2.y(); y++) {
|
for (int y = corner1.y(); y < corner2.y(); y++) {
|
||||||
TilePos current_tile = {x, y};
|
TilePos current_tile = {x, y};
|
||||||
unsigned distance_squared = static_cast<unsigned>(center.DistanceTo(current_tile) * center.DistanceTo(current_tile));
|
unsigned distance_squared = static_cast<unsigned>(
|
||||||
if (IsTilePosValid(current_tile) && distance_squared < radius_squared)
|
center.DistanceTo(current_tile) * center.DistanceTo(current_tile));
|
||||||
{
|
if (IsTilePosValid(current_tile) && distance_squared < radius_squared) {
|
||||||
// y is row, x is col
|
// y is row, x is col
|
||||||
m_Tiles[y][x] = &tile_types.at(tile_type);
|
m_Tiles[y][x] = &tile_types.at(tile_type);
|
||||||
}
|
}
|
||||||
@@ -92,10 +91,12 @@ void Map::PaintCircle(TilePos center, unsigned radius, TileType tile_type)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Map::PaintLine(TilePos start_tile, TilePos stop_tile, double width, TileType tile_type)
|
void Map::PaintLine(TilePos start_tile, TilePos stop_tile, double width,
|
||||||
{
|
TileType tile_type) {
|
||||||
const vec<double, 2> start{static_cast<double>(start_tile.x()), static_cast<double>(start_tile.y())};
|
const vec<double, 2> start{static_cast<double>(start_tile.x()),
|
||||||
const vec<double, 2> stop{static_cast<double>(stop_tile.x()), static_cast<double>(stop_tile.y())};
|
static_cast<double>(start_tile.y())};
|
||||||
|
const vec<double, 2> stop{static_cast<double>(stop_tile.x()),
|
||||||
|
static_cast<double>(stop_tile.y())};
|
||||||
const double line_length = start.DistanceTo(stop);
|
const double line_length = start.DistanceTo(stop);
|
||||||
const vec<double, 2> step = (stop - start) / line_length;
|
const vec<double, 2> step = (stop - start) / line_length;
|
||||||
const vec<double, 2> ortho = step.GetOrthogonal();
|
const vec<double, 2> ortho = step.GetOrthogonal();
|
||||||
@@ -104,7 +105,8 @@ void Map::PaintLine(TilePos start_tile, TilePos stop_tile, double width, TileTyp
|
|||||||
for (double t = 0; t < line_length; t += 1.0) {
|
for (double t = 0; t < line_length; t += 1.0) {
|
||||||
for (double ortho_t = 0; ortho_t < width; ortho_t += 0.1) {
|
for (double ortho_t = 0; ortho_t < width; ortho_t += 0.1) {
|
||||||
auto tile_pos = start + step * t + ortho * ortho_t;
|
auto tile_pos = start + step * t + ortho * ortho_t;
|
||||||
TilePos tile_pos_int{static_cast<int32_t>(tile_pos.x()), static_cast<int32_t>(tile_pos.y())};
|
TilePos tile_pos_int{static_cast<int32_t>(tile_pos.x()),
|
||||||
|
static_cast<int32_t>(tile_pos.y())};
|
||||||
if (IsTilePosValid(tile_pos_int)) {
|
if (IsTilePosValid(tile_pos_int)) {
|
||||||
size_t row = static_cast<size_t>(tile_pos.x());
|
size_t row = static_cast<size_t>(tile_pos.x());
|
||||||
size_t col = static_cast<size_t>(tile_pos.y());
|
size_t col = static_cast<size_t>(tile_pos.y());
|
||||||
@@ -114,14 +116,13 @@ void Map::PaintLine(TilePos start_tile, TilePos stop_tile, double width, TileTyp
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Map::PaintRectangle(TilePos first_corner, TilePos second_corner,
|
||||||
void Map::PaintRectangle(TilePos first_corner, TilePos second_corner, TileType tile_type)
|
TileType tile_type) {
|
||||||
{
|
|
||||||
std::initializer_list<int> xvals = {first_corner.x(), second_corner.x()};
|
std::initializer_list<int> xvals = {first_corner.x(), second_corner.x()};
|
||||||
std::initializer_list<int> yvals = {first_corner.y(), second_corner.y()};
|
std::initializer_list<int> yvals = {first_corner.y(), second_corner.y()};
|
||||||
for (int x = std::min(xvals); x < std::max(xvals); x++) {
|
for (int x = std::min(xvals); x < std::max(xvals); x++) {
|
||||||
for (int y = std::min(yvals); y < std::max(yvals); y++) {
|
for (int y = std::min(yvals); y < std::max(yvals); y++) {
|
||||||
TilePos tile_pos{x,y};
|
TilePos tile_pos{x, y};
|
||||||
LOG_DEBUG("tile_pos = ", tile_pos);
|
LOG_DEBUG("tile_pos = ", tile_pos);
|
||||||
if (IsTilePosValid(tile_pos)) {
|
if (IsTilePosValid(tile_pos)) {
|
||||||
size_t row = static_cast<size_t>(tile_pos.x());
|
size_t row = static_cast<size_t>(tile_pos.x());
|
||||||
|
|||||||
@@ -14,10 +14,10 @@ public:
|
|||||||
Map(int rows, int cols);
|
Map(int rows, int cols);
|
||||||
Map() : Map(0, 0) {}
|
Map() : Map(0, 0) {}
|
||||||
|
|
||||||
Map(const Map&) = delete;
|
Map(const Map &) = delete;
|
||||||
Map(Map&&) = delete;
|
Map(Map &&) = delete;
|
||||||
Map& operator=(const Map&) = delete;
|
Map &operator=(const Map &) = delete;
|
||||||
Map& operator=(Map&&) = delete;
|
Map &operator=(Map &&) = delete;
|
||||||
|
|
||||||
const TileGrid &GetMapTiles() const { return m_Tiles; }
|
const TileGrid &GetMapTiles() const { return m_Tiles; }
|
||||||
|
|
||||||
@@ -35,7 +35,8 @@ public:
|
|||||||
// methods for drawing on the map
|
// methods for drawing on the map
|
||||||
void PaintCircle(TilePos center, unsigned radius, TileType tile_type);
|
void PaintCircle(TilePos center, unsigned radius, TileType tile_type);
|
||||||
void PaintLine(TilePos start, TilePos stop, double width, TileType tile_type);
|
void PaintLine(TilePos start, TilePos stop, double width, TileType tile_type);
|
||||||
void PaintRectangle(TilePos first_corner, TilePos second_corner, TileType tile_type);
|
void PaintRectangle(TilePos first_corner, TilePos second_corner,
|
||||||
|
TileType tile_type);
|
||||||
|
|
||||||
std::vector<TilePos> GetNeighbors(TilePos center) const;
|
std::vector<TilePos> GetNeighbors(TilePos center) const;
|
||||||
float GetCost(TilePos pos) const { return GetTileAt(pos)->cost; }
|
float GetCost(TilePos pos) const { return GetTileAt(pos)->cost; }
|
||||||
|
|||||||
221
cpp/src/math.hpp
221
cpp/src/math.hpp
@@ -4,12 +4,12 @@
|
|||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <concepts>
|
#include <concepts>
|
||||||
|
#include <functional>
|
||||||
#include <initializer_list>
|
#include <initializer_list>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <numeric>
|
#include <numeric>
|
||||||
#include <ranges>
|
#include <ranges>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <functional>
|
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#include <numbers>
|
#include <numbers>
|
||||||
@@ -39,8 +39,7 @@ struct Any {};
|
|||||||
template <typename T, size_t N, typename Tag = Any> class vec {
|
template <typename T, size_t N, typename Tag = Any> class vec {
|
||||||
|
|
||||||
// Friend declaration for move constructor from different tag types
|
// Friend declaration for move constructor from different tag types
|
||||||
template <typename U, size_t M, typename OtherTag>
|
template <typename U, size_t M, typename OtherTag> friend class vec;
|
||||||
friend class vec;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
vec() : m_Array{} {}
|
vec() : m_Array{} {}
|
||||||
@@ -51,8 +50,8 @@ public:
|
|||||||
|
|
||||||
vec(std::array<T, N> array) : m_Array{array} {}
|
vec(std::array<T, N> array) : m_Array{array} {}
|
||||||
|
|
||||||
template<typename OtherTag>
|
template <typename OtherTag>
|
||||||
vec(vec<T, N, OtherTag>&& other) : m_Array{std::move(other.m_Array)} {}
|
vec(vec<T, N, OtherTag> &&other) : m_Array{std::move(other.m_Array)} {}
|
||||||
|
|
||||||
//
|
//
|
||||||
// Access to elements & data
|
// Access to elements & data
|
||||||
@@ -77,20 +76,20 @@ public:
|
|||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::array<T,N>& Data() { return m_Array; }
|
std::array<T, N> &Data() { return m_Array; }
|
||||||
|
|
||||||
//
|
//
|
||||||
// binary operators
|
// binary operators
|
||||||
//
|
//
|
||||||
|
|
||||||
friend bool operator==(const vec &a, const vec &b)
|
friend bool operator==(const vec &a, const vec &b)
|
||||||
requires (std::is_integral_v<T>)
|
requires(std::is_integral_v<T>)
|
||||||
{
|
{
|
||||||
return std::ranges::equal(a.m_Array, b.m_Array);
|
return std::ranges::equal(a.m_Array, b.m_Array);
|
||||||
}
|
}
|
||||||
|
|
||||||
friend bool operator==(const vec &a, const vec &b)
|
friend bool operator==(const vec &a, const vec &b)
|
||||||
requires (std::is_floating_point_v<T>)
|
requires(std::is_floating_point_v<T>)
|
||||||
{
|
{
|
||||||
for (const auto &[u, v] : std::views::zip(a.m_Array, b.m_Array)) {
|
for (const auto &[u, v] : std::views::zip(a.m_Array, b.m_Array)) {
|
||||||
if (!equalEpsilon(u, v)) {
|
if (!equalEpsilon(u, v)) {
|
||||||
@@ -109,8 +108,7 @@ public:
|
|||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
friend vec operator+(const vec& a, T b)
|
friend vec operator+(const vec &a, T b) {
|
||||||
{
|
|
||||||
vec<T, N, Tag> c;
|
vec<T, N, Tag> c;
|
||||||
std::ranges::transform(a.m_Array, std::views::repeat(b), c.m_Array.begin(),
|
std::ranges::transform(a.m_Array, std::views::repeat(b), c.m_Array.begin(),
|
||||||
std::plus{});
|
std::plus{});
|
||||||
@@ -124,8 +122,7 @@ public:
|
|||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
friend vec operator-(const vec& a, T b)
|
friend vec operator-(const vec &a, T b) {
|
||||||
{
|
|
||||||
vec<T, N, Tag> c;
|
vec<T, N, Tag> c;
|
||||||
std::ranges::transform(a.m_Array, std::views::repeat(b), c.m_Array.begin(),
|
std::ranges::transform(a.m_Array, std::views::repeat(b), c.m_Array.begin(),
|
||||||
std::minus{});
|
std::minus{});
|
||||||
@@ -148,10 +145,10 @@ public:
|
|||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
friend vec operator/(const vec &a, const vec& b) {
|
friend vec operator/(const vec &a, const vec &b) {
|
||||||
vec<T, N, Tag> c;
|
vec<T, N, Tag> c;
|
||||||
std::ranges::transform(a.m_Array, b.m_Array,
|
std::ranges::transform(a.m_Array, b.m_Array, c.m_Array.begin(),
|
||||||
c.m_Array.begin(), std::divides{});
|
std::divides{});
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -173,12 +170,12 @@ public:
|
|||||||
return a;
|
return a;
|
||||||
}
|
}
|
||||||
|
|
||||||
vec& operator/=(float scalar)
|
vec &operator/=(float scalar) {
|
||||||
{
|
vec &a = *this;
|
||||||
vec& a = *this;
|
|
||||||
auto b = std::views::repeat(scalar);
|
auto b = std::views::repeat(scalar);
|
||||||
std::ranges::transform(a.m_Array, b, a.m_Array.begin(), std::divides{});
|
std::ranges::transform(a.m_Array, b, a.m_Array.begin(), std::divides{});
|
||||||
// TODO check all of this, could be done better with views instead of ranges?
|
// TODO check all of this, could be done better with views instead of
|
||||||
|
// ranges?
|
||||||
return a;
|
return a;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -203,7 +200,6 @@ public:
|
|||||||
return (a - b).LengthSquared();
|
return (a - b).LengthSquared();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// In-place vector operations
|
// In-place vector operations
|
||||||
//
|
//
|
||||||
@@ -237,16 +233,14 @@ public:
|
|||||||
return tmp;
|
return tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
static T DotProduct(const vec& a, const vec& b)
|
static T DotProduct(const vec &a, const vec &b) {
|
||||||
{
|
return std::inner_product(a.m_Array.begin(), a.m_Array.end(),
|
||||||
return std::inner_product(
|
b.m_Array.begin(), T{}, std::plus{},
|
||||||
a.m_Array.begin(), a.m_Array.end(), b.m_Array.begin(),
|
std::multiplies{});
|
||||||
T{}, std::plus{}, std::multiplies{});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
T DotProduct(const vec& b) const
|
T DotProduct(const vec &b) const {
|
||||||
{
|
const auto &a = *this;
|
||||||
const auto& a = *this;
|
|
||||||
return DotProduct(a, b);
|
return DotProduct(a, b);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -290,28 +284,24 @@ public:
|
|||||||
return m_Array[2];
|
return m_Array[2];
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename TargetTag>
|
template <typename TargetTag> vec<T, N, TargetTag> ChangeTag() const & {
|
||||||
vec<T,N,TargetTag> ChangeTag() const &
|
return vec<T, N, TargetTag>(m_Array);
|
||||||
{
|
|
||||||
return vec<T,N,TargetTag>(m_Array);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename TargetTag>
|
template <typename TargetTag> vec<T, N, TargetTag> ChangeTag() && {
|
||||||
vec<T,N,TargetTag> ChangeTag() &&
|
return vec<T, N, TargetTag>(std::move(*this));
|
||||||
{
|
|
||||||
return vec<T,N,TargetTag>(std::move(*this));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Structured binding support for N == 2
|
// Structured binding support for N == 2
|
||||||
template<size_t I>
|
template <size_t I>
|
||||||
const T& get() const
|
const T &get() const
|
||||||
requires(N == 2 && I < 2)
|
requires(N == 2 && I < 2)
|
||||||
{
|
{
|
||||||
return m_Array[I];
|
return m_Array[I];
|
||||||
}
|
}
|
||||||
|
|
||||||
template<size_t I>
|
template <size_t I>
|
||||||
T& get()
|
T &get()
|
||||||
requires(N == 2 && I < 2)
|
requires(N == 2 && I < 2)
|
||||||
{
|
{
|
||||||
return m_Array[I];
|
return m_Array[I];
|
||||||
@@ -372,114 +362,101 @@ struct TilePosHash {
|
|||||||
//
|
//
|
||||||
|
|
||||||
// Collumn major square matrix
|
// Collumn major square matrix
|
||||||
template <typename T, size_t N, typename Tag = Any>
|
template <typename T, size_t N, typename Tag = Any> class Matrix {
|
||||||
class Matrix {
|
|
||||||
|
|
||||||
using vec_type = vec<T, N, Tag>;
|
using vec_type = vec<T, N, Tag>;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Matrix() = default;
|
Matrix() = default;
|
||||||
|
|
||||||
// Initialization using flat array of N*N elements
|
// Initialization using flat array of N*N elements
|
||||||
template <typename Tarr, size_t M>
|
template <typename Tarr, size_t M>
|
||||||
requires (M == N*N && std::same_as<Tarr, T>)
|
requires(M == N * N && std::same_as<Tarr, T>)
|
||||||
Matrix(std::array<Tarr,M> array) : m_Array{}
|
Matrix(std::array<Tarr, M> array) : m_Array{} {
|
||||||
{
|
std::size_t idx = 0;
|
||||||
std::size_t idx = 0;
|
for (auto col : array | std::views::chunk(N)) {
|
||||||
for (auto col : array | std::views::chunk(N))
|
std::ranges::copy(col, m_Array[idx++].Data().begin());
|
||||||
{
|
|
||||||
std::ranges::copy(col, m_Array[idx++].Data().begin());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const vec_type& operator[](size_t index) const { return m_Array[index]; }
|
const vec_type &operator[](size_t index) const { return m_Array[index]; }
|
||||||
vec_type& operator[](size_t index) { return m_Array[index]; }
|
vec_type &operator[](size_t index) { return m_Array[index]; }
|
||||||
|
|
||||||
friend std::ostream &operator<<(std::ostream &os, const Matrix &obj)
|
friend std::ostream &operator<<(std::ostream &os, const Matrix &obj) {
|
||||||
{
|
os << "( ";
|
||||||
os << "( ";
|
for (const auto &element : obj.m_Array) {
|
||||||
for (const auto &element : obj.m_Array) {
|
os << element << " ";
|
||||||
os << element << " ";
|
|
||||||
}
|
|
||||||
os << ")";
|
|
||||||
return os;
|
|
||||||
}
|
}
|
||||||
|
os << ")";
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
friend Matrix operator+(const Matrix &A, const Matrix &B) {
|
||||||
|
Matrix C;
|
||||||
|
std::ranges::transform(A.m_Array, B.m_Array, C.m_Array.begin(),
|
||||||
|
std::plus{});
|
||||||
|
return C;
|
||||||
|
}
|
||||||
|
|
||||||
friend Matrix operator+(const Matrix& A, const Matrix& B)
|
friend Matrix operator-(const Matrix &A, const Matrix &B) {
|
||||||
{
|
Matrix C;
|
||||||
Matrix C;
|
std::ranges::transform(A.m_Array, B.m_Array, C.m_Array.begin(),
|
||||||
std::ranges::transform(A.m_Array, B.m_Array, C.m_Array.begin(), std::plus{});
|
std::minus{});
|
||||||
return C;
|
return C;
|
||||||
|
}
|
||||||
|
|
||||||
|
friend Matrix operator*(const Matrix &A, const Matrix &B) {
|
||||||
|
Matrix C;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < N; i++) {
|
||||||
|
for (size_t j = 0; j < N; j++) {
|
||||||
|
T sum = 0;
|
||||||
|
for (size_t k = 0; k < N; ++k)
|
||||||
|
sum += A[i][k] * B[k][j];
|
||||||
|
C[i][j] = sum;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return C;
|
||||||
|
}
|
||||||
|
|
||||||
friend Matrix operator-(const Matrix& A, const Matrix& B)
|
friend vec_type operator*(const Matrix &A, const vec_type &b) {
|
||||||
{
|
// we assume that b is row vector
|
||||||
Matrix C;
|
vec_type c;
|
||||||
std::ranges::transform(A.m_Array, B.m_Array, C.m_Array.begin(), std::minus{});
|
for (size_t i = 0; i < N; i++) {
|
||||||
return C;
|
c[i] = b.DotProduct(A[i]);
|
||||||
}
|
}
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
friend Matrix operator*(const Matrix& A, const Matrix& B)
|
static constexpr Matrix Eye() {
|
||||||
{
|
Matrix E;
|
||||||
Matrix C;
|
for (size_t i = 0; i < N; i++) {
|
||||||
|
E[i][i] = T{1};
|
||||||
for (size_t i = 0; i < N; i++)
|
|
||||||
{
|
|
||||||
for (size_t j = 0; j < N; j++)
|
|
||||||
{
|
|
||||||
T sum = 0;
|
|
||||||
for (size_t k = 0; k < N; ++k) sum += A[i][k] * B[k][j];
|
|
||||||
C[i][j] = sum;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return C;
|
|
||||||
}
|
}
|
||||||
|
return E;
|
||||||
friend vec_type operator*(const Matrix& A, const vec_type& b)
|
}
|
||||||
{
|
|
||||||
// we assume that b is row vector
|
|
||||||
vec_type c;
|
|
||||||
for (size_t i = 0; i < N; i++)
|
|
||||||
{
|
|
||||||
c[i] = b.DotProduct(A[i]);
|
|
||||||
}
|
|
||||||
return c;
|
|
||||||
}
|
|
||||||
|
|
||||||
static constexpr Matrix Eye()
|
|
||||||
{
|
|
||||||
Matrix E;
|
|
||||||
for (size_t i = 0; i < N; i++)
|
|
||||||
{
|
|
||||||
E[i][i] = T{1};
|
|
||||||
}
|
|
||||||
return E;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::array<vec_type, N> m_Array;
|
std::array<vec_type, N> m_Array;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Structured binding support for vec<T, 2, Tag>
|
// Structured binding support for vec<T, 2, Tag>
|
||||||
namespace std {
|
namespace std {
|
||||||
template<typename T, typename Tag>
|
template <typename T, typename Tag>
|
||||||
struct tuple_size<vec<T, 2, Tag>> : integral_constant<size_t, 2> {};
|
struct tuple_size<vec<T, 2, Tag>> : integral_constant<size_t, 2> {};
|
||||||
|
|
||||||
template<size_t I, typename T, typename Tag>
|
template <size_t I, typename T, typename Tag>
|
||||||
struct tuple_element<I, vec<T, 2, Tag>> {
|
struct tuple_element<I, vec<T, 2, Tag>> {
|
||||||
using type = T;
|
using type = T;
|
||||||
};
|
};
|
||||||
}
|
} // namespace std
|
||||||
|
|
||||||
// ADL-based get for structured bindings
|
// ADL-based get for structured bindings
|
||||||
template<size_t I, typename T, typename Tag>
|
template <size_t I, typename T, typename Tag>
|
||||||
const T& get(const vec<T, 2, Tag>& v) {
|
const T &get(const vec<T, 2, Tag> &v) {
|
||||||
return v.template get<I>();
|
return v.template get<I>();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<size_t I, typename T, typename Tag>
|
template <size_t I, typename T, typename Tag> T &get(vec<T, 2, Tag> &v) {
|
||||||
T& get(vec<T, 2, Tag>& v) {
|
|
||||||
return v.template get<I>();
|
return v.template get<I>();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
#include <memory>
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
#include <memory>
|
||||||
#include <queue>
|
#include <queue>
|
||||||
|
|
||||||
#include "pathfinder/base.hpp"
|
#include "pathfinder/base.hpp"
|
||||||
@@ -9,14 +9,15 @@
|
|||||||
|
|
||||||
namespace pathfinder {
|
namespace pathfinder {
|
||||||
|
|
||||||
PathFinderBase::PathFinderBase(const Map* map) : m_Map(map) {}
|
PathFinderBase::PathFinderBase(const Map *map) : m_Map(map) {}
|
||||||
|
|
||||||
// LinearPathFinder also lives here, since it is too small to get it's
|
// LinearPathFinder also lives here, since it is too small to get it's
|
||||||
// own implementation file
|
// own implementation file
|
||||||
Path LinearPathFinder::CalculatePath(WorldPos, WorldPos end) // first argument (start pos) not used
|
Path LinearPathFinder::CalculatePath(
|
||||||
|
WorldPos, WorldPos end) // first argument (start pos) not used
|
||||||
{
|
{
|
||||||
auto path = Path{end};
|
auto path = Path{end};
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // pathfinder namespace
|
} // namespace pathfinder
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <vector>
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include "math.hpp"
|
|
||||||
#include "map.hpp"
|
#include "map.hpp"
|
||||||
|
#include "math.hpp"
|
||||||
|
|
||||||
namespace pathfinder {
|
namespace pathfinder {
|
||||||
|
|
||||||
@@ -21,32 +21,30 @@ enum class PathFinderType {
|
|||||||
|
|
||||||
class PathFinderBase {
|
class PathFinderBase {
|
||||||
public:
|
public:
|
||||||
PathFinderBase(const Map* m);
|
PathFinderBase(const Map *m);
|
||||||
~PathFinderBase() = default;
|
~PathFinderBase() = default;
|
||||||
|
|
||||||
PathFinderBase(const PathFinderBase&) = delete;
|
PathFinderBase(const PathFinderBase &) = delete;
|
||||||
PathFinderBase(PathFinderBase&&) = delete;
|
PathFinderBase(PathFinderBase &&) = delete;
|
||||||
PathFinderBase& operator=(const PathFinderBase&) = delete;
|
PathFinderBase &operator=(const PathFinderBase &) = delete;
|
||||||
PathFinderBase& operator=(PathFinderBase&&) = delete;
|
PathFinderBase &operator=(PathFinderBase &&) = delete;
|
||||||
|
|
||||||
virtual const std::string_view& GetName() const = 0;
|
virtual const std::string_view &GetName() const = 0;
|
||||||
virtual Path CalculatePath(WorldPos start, WorldPos end) = 0;
|
virtual Path CalculatePath(WorldPos start, WorldPos end) = 0;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
const Map* m_Map;
|
const Map *m_Map;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
class LinearPathFinder : public PathFinderBase {
|
class LinearPathFinder : public PathFinderBase {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
LinearPathFinder(const Map* m): PathFinderBase(m) {}
|
LinearPathFinder(const Map *m) : PathFinderBase(m) {}
|
||||||
Path CalculatePath(WorldPos start, WorldPos end) override;
|
Path CalculatePath(WorldPos start, WorldPos end) override;
|
||||||
const std::string_view& GetName() const override { return m_Name; }
|
const std::string_view &GetName() const override { return m_Name; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const std::string_view m_Name = "Linear Path";
|
const std::string_view m_Name = "Linear Path";
|
||||||
};
|
};
|
||||||
|
|
||||||
} // pathfinder namespace
|
} // namespace pathfinder
|
||||||
|
|
||||||
|
|||||||
@@ -9,58 +9,59 @@
|
|||||||
namespace pathfinder {
|
namespace pathfinder {
|
||||||
|
|
||||||
Path BFS::CalculatePath(WorldPos start_world, WorldPos end_world) {
|
Path BFS::CalculatePath(WorldPos start_world, WorldPos end_world) {
|
||||||
if (m_Map == nullptr) return {};
|
if (m_Map == nullptr)
|
||||||
|
return {};
|
||||||
|
|
||||||
const TilePos start = m_Map->WorldToTile(start_world);
|
const TilePos start = m_Map->WorldToTile(start_world);
|
||||||
const TilePos end = m_Map->WorldToTile(end_world);
|
const TilePos end = m_Map->WorldToTile(end_world);
|
||||||
|
|
||||||
if (!m_Map->IsTilePosValid(start) || !m_Map->IsTilePosValid(end))
|
if (!m_Map->IsTilePosValid(start) || !m_Map->IsTilePosValid(end))
|
||||||
return {};
|
return {};
|
||||||
if (start == end) {
|
if (start == end) {
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
// clear previous run
|
// clear previous run
|
||||||
m_CameFrom.clear();
|
m_CameFrom.clear();
|
||||||
m_Distance.clear();
|
m_Distance.clear();
|
||||||
|
|
||||||
std::queue<TilePos> frontier;
|
std::queue<TilePos> frontier;
|
||||||
frontier.push(start);
|
frontier.push(start);
|
||||||
m_CameFrom[start] = start;
|
m_CameFrom[start] = start;
|
||||||
m_Distance[start] = 0.0f;
|
m_Distance[start] = 0.0f;
|
||||||
|
|
||||||
// ---------------- build flow-field ----------------
|
// ---------------- build flow-field ----------------
|
||||||
bool early_exit = false;
|
bool early_exit = false;
|
||||||
while (!frontier.empty() && !early_exit) {
|
while (!frontier.empty() && !early_exit) {
|
||||||
TilePos current = frontier.front();
|
TilePos current = frontier.front();
|
||||||
frontier.pop();
|
frontier.pop();
|
||||||
|
|
||||||
for (TilePos next : m_Map->GetNeighbors(current)) {
|
for (TilePos next : m_Map->GetNeighbors(current)) {
|
||||||
if (m_CameFrom.find(next) == m_CameFrom.end()) { // not visited
|
if (m_CameFrom.find(next) == m_CameFrom.end()) { // not visited
|
||||||
frontier.push(next);
|
frontier.push(next);
|
||||||
m_Distance[next] = m_Distance[current] + 1.0f;
|
m_Distance[next] = m_Distance[current] + 1.0f;
|
||||||
m_CameFrom[next] = current;
|
m_CameFrom[next] = current;
|
||||||
|
|
||||||
if (next == end) { // early exit
|
if (next == end) { // early exit
|
||||||
early_exit = true;
|
early_exit = true;
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// --------------- reconstruct path -----------------
|
// --------------- reconstruct path -----------------
|
||||||
if (m_CameFrom.find(end) == m_CameFrom.end())
|
if (m_CameFrom.find(end) == m_CameFrom.end())
|
||||||
return {}; // end not reached
|
return {}; // end not reached
|
||||||
|
|
||||||
Path path;
|
Path path;
|
||||||
TilePos cur = end;
|
TilePos cur = end;
|
||||||
|
path.push_back(m_Map->TileToWorld(cur));
|
||||||
|
while (cur != start) {
|
||||||
|
cur = m_CameFrom[cur];
|
||||||
path.push_back(m_Map->TileToWorld(cur));
|
path.push_back(m_Map->TileToWorld(cur));
|
||||||
while (cur != start) {
|
}
|
||||||
cur = m_CameFrom[cur];
|
std::reverse(path.begin(), path.end());
|
||||||
path.push_back(m_Map->TileToWorld(cur));
|
return path;
|
||||||
}
|
|
||||||
std::reverse(path.begin(), path.end());
|
|
||||||
return path;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // pathfinder namespace
|
} // namespace pathfinder
|
||||||
|
|||||||
@@ -9,12 +9,12 @@
|
|||||||
|
|
||||||
namespace pathfinder {
|
namespace pathfinder {
|
||||||
|
|
||||||
class BFS: public PathFinderBase {
|
class BFS : public PathFinderBase {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
BFS(const Map* m): PathFinderBase(m) {}
|
BFS(const Map *m) : PathFinderBase(m) {}
|
||||||
Path CalculatePath(WorldPos start, WorldPos end) override;
|
Path CalculatePath(WorldPos start, WorldPos end) override;
|
||||||
const std::string_view& GetName() const override { return m_Name; }
|
const std::string_view &GetName() const override { return m_Name; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const std::string_view m_Name = "Breadth First Search";
|
const std::string_view m_Name = "Breadth First Search";
|
||||||
@@ -22,4 +22,4 @@ private:
|
|||||||
std::unordered_map<TilePos, TilePos, TilePosHash> m_CameFrom;
|
std::unordered_map<TilePos, TilePos, TilePosHash> m_CameFrom;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
} // namespace pathfinder
|
||||||
|
|||||||
@@ -3,71 +3,69 @@
|
|||||||
#include "dijkstra.hpp"
|
#include "dijkstra.hpp"
|
||||||
|
|
||||||
#include "base.hpp"
|
#include "base.hpp"
|
||||||
#include "utils.hpp"
|
|
||||||
#include "math.hpp"
|
|
||||||
#include "map.hpp"
|
#include "map.hpp"
|
||||||
|
#include "math.hpp"
|
||||||
|
#include "utils.hpp"
|
||||||
|
|
||||||
namespace pathfinder {
|
namespace pathfinder {
|
||||||
|
|
||||||
Path Dijkstra::CalculatePath(WorldPos start_world, WorldPos end_world)
|
Path Dijkstra::CalculatePath(WorldPos start_world, WorldPos end_world) {
|
||||||
{
|
|
||||||
using QueueEntry = utils::QueueEntry;
|
using QueueEntry = utils::QueueEntry;
|
||||||
|
|
||||||
if (!m_Map) return {};
|
if (!m_Map)
|
||||||
|
return {};
|
||||||
|
|
||||||
const TilePos start = m_Map->WorldToTile(start_world);
|
const TilePos start = m_Map->WorldToTile(start_world);
|
||||||
const TilePos end = m_Map->WorldToTile(end_world);
|
const TilePos end = m_Map->WorldToTile(end_world);
|
||||||
|
|
||||||
if (!m_Map->IsTilePosValid(start) || !m_Map->IsTilePosValid(end))
|
if (!m_Map->IsTilePosValid(start) || !m_Map->IsTilePosValid(end))
|
||||||
return {};
|
return {};
|
||||||
if (start == end) return {};
|
if (start == end)
|
||||||
|
return {};
|
||||||
|
|
||||||
// clear previous run
|
// clear previous run
|
||||||
m_CameFrom.clear();
|
m_CameFrom.clear();
|
||||||
m_Cost.clear();
|
m_Cost.clear();
|
||||||
|
|
||||||
std::priority_queue<QueueEntry, std::vector<QueueEntry>, std::greater<>> frontier;
|
std::priority_queue<QueueEntry, std::vector<QueueEntry>, std::greater<>>
|
||||||
|
frontier;
|
||||||
frontier.push({0.0f, start});
|
frontier.push({0.0f, start});
|
||||||
m_CameFrom[start] = start; // sentinel
|
m_CameFrom[start] = start; // sentinel
|
||||||
m_Cost[start] = 0.0f;
|
m_Cost[start] = 0.0f;
|
||||||
|
|
||||||
while (!frontier.empty())
|
while (!frontier.empty()) {
|
||||||
{
|
const QueueEntry current = frontier.top();
|
||||||
const QueueEntry current = frontier.top();
|
frontier.pop();
|
||||||
frontier.pop();
|
|
||||||
|
|
||||||
if (current.tile == end) // early exit
|
if (current.tile == end) // early exit
|
||||||
break;
|
break;
|
||||||
|
|
||||||
for (TilePos next : m_Map->GetNeighbors(current.tile))
|
for (TilePos next : m_Map->GetNeighbors(current.tile)) {
|
||||||
{
|
// cost of moving to neighbour (uniform 1.0 matches original BFS)
|
||||||
// cost of moving to neighbour (uniform 1.0 matches original BFS)
|
const float newCost = m_Cost[current.tile] + m_Map->GetCost(next);
|
||||||
const float newCost = m_Cost[current.tile] + m_Map->GetCost(next);
|
|
||||||
|
|
||||||
if (!m_Cost.count(next) || newCost < m_Cost[next])
|
if (!m_Cost.count(next) || newCost < m_Cost[next]) {
|
||||||
{
|
m_Cost[next] = newCost;
|
||||||
m_Cost[next] = newCost;
|
m_CameFrom[next] = current.tile;
|
||||||
m_CameFrom[next] = current.tile;
|
frontier.push({newCost, next});
|
||||||
frontier.push({newCost, next});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// reconstruct path
|
// reconstruct path
|
||||||
if (!m_CameFrom.count(end))
|
if (!m_CameFrom.count(end))
|
||||||
return {}; // goal never reached
|
return {}; // goal never reached
|
||||||
|
|
||||||
Path path;
|
Path path;
|
||||||
TilePos cur = end;
|
TilePos cur = end;
|
||||||
path.push_back(m_Map->TileToWorld(cur));
|
path.push_back(m_Map->TileToWorld(cur));
|
||||||
|
|
||||||
while (cur != start)
|
while (cur != start) {
|
||||||
{
|
cur = m_CameFrom[cur];
|
||||||
cur = m_CameFrom[cur];
|
path.push_back(m_Map->TileToWorld(cur));
|
||||||
path.push_back(m_Map->TileToWorld(cur));
|
|
||||||
}
|
}
|
||||||
std::reverse(path.begin(), path.end());
|
std::reverse(path.begin(), path.end());
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // pathfinder namespace
|
} // namespace pathfinder
|
||||||
|
|||||||
@@ -10,12 +10,12 @@
|
|||||||
|
|
||||||
namespace pathfinder {
|
namespace pathfinder {
|
||||||
|
|
||||||
class Dijkstra: public PathFinderBase {
|
class Dijkstra : public PathFinderBase {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Dijkstra(const Map* m): PathFinderBase(m) {}
|
Dijkstra(const Map *m) : PathFinderBase(m) {}
|
||||||
Path CalculatePath(WorldPos start, WorldPos end) override;
|
Path CalculatePath(WorldPos start, WorldPos end) override;
|
||||||
const std::string_view& GetName() const override { return m_Name; }
|
const std::string_view &GetName() const override { return m_Name; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const std::string_view m_Name = "Dijkstra's Algorithm";
|
const std::string_view m_Name = "Dijkstra's Algorithm";
|
||||||
@@ -23,4 +23,4 @@ private:
|
|||||||
std::unordered_map<TilePos, TilePos, TilePosHash> m_CameFrom;
|
std::unordered_map<TilePos, TilePos, TilePosHash> m_CameFrom;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // pathfinder namespace
|
} // namespace pathfinder
|
||||||
|
|||||||
@@ -3,69 +3,67 @@
|
|||||||
#include "gbfs.hpp"
|
#include "gbfs.hpp"
|
||||||
|
|
||||||
#include "base.hpp"
|
#include "base.hpp"
|
||||||
#include "math.hpp"
|
|
||||||
#include "map.hpp"
|
#include "map.hpp"
|
||||||
|
#include "math.hpp"
|
||||||
#include "pathfinder/utils.hpp"
|
#include "pathfinder/utils.hpp"
|
||||||
|
|
||||||
namespace pathfinder {
|
namespace pathfinder {
|
||||||
|
|
||||||
float GBFS::Heuristic(const TilePos& a, const TilePos& b)
|
float GBFS::Heuristic(const TilePos &a, const TilePos &b) {
|
||||||
{
|
return static_cast<float>(std::abs(a.x() - b.x()) + std::abs(a.y() - b.y()));
|
||||||
return static_cast<float>(std::abs(a.x() - b.x()) + std::abs(a.y() - b.y()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Path GBFS::CalculatePath(WorldPos start_world, WorldPos end_world)
|
Path GBFS::CalculatePath(WorldPos start_world, WorldPos end_world) {
|
||||||
{
|
|
||||||
using QueueEntry = pathfinder::utils::QueueEntry;
|
using QueueEntry = pathfinder::utils::QueueEntry;
|
||||||
|
|
||||||
if (!m_Map) return {};
|
if (!m_Map)
|
||||||
|
return {};
|
||||||
|
|
||||||
const TilePos start = m_Map->WorldToTile(start_world);
|
const TilePos start = m_Map->WorldToTile(start_world);
|
||||||
const TilePos end = m_Map->WorldToTile(end_world);
|
const TilePos end = m_Map->WorldToTile(end_world);
|
||||||
|
|
||||||
if (!m_Map->IsTilePosValid(start) || !m_Map->IsTilePosValid(end))
|
if (!m_Map->IsTilePosValid(start) || !m_Map->IsTilePosValid(end))
|
||||||
return {};
|
return {};
|
||||||
if (start == end) return {};
|
if (start == end)
|
||||||
|
return {};
|
||||||
|
|
||||||
m_CameFrom.clear();
|
m_CameFrom.clear();
|
||||||
|
|
||||||
std::priority_queue<QueueEntry, std::vector<QueueEntry>, std::greater<>> frontier;
|
std::priority_queue<QueueEntry, std::vector<QueueEntry>, std::greater<>>
|
||||||
|
frontier;
|
||||||
frontier.push({Heuristic(start, end), start});
|
frontier.push({Heuristic(start, end), start});
|
||||||
m_CameFrom[start] = start; // sentinel
|
m_CameFrom[start] = start; // sentinel
|
||||||
|
|
||||||
while (!frontier.empty())
|
while (!frontier.empty()) {
|
||||||
{
|
const QueueEntry current = frontier.top();
|
||||||
const QueueEntry current = frontier.top();
|
frontier.pop();
|
||||||
frontier.pop();
|
|
||||||
|
|
||||||
if (current.tile == end) // early exit
|
if (current.tile == end) // early exit
|
||||||
break;
|
break;
|
||||||
|
|
||||||
for (TilePos next : m_Map->GetNeighbors(current.tile))
|
for (TilePos next : m_Map->GetNeighbors(current.tile)) {
|
||||||
|
if (!m_CameFrom.count(next)) // not visited
|
||||||
{
|
{
|
||||||
if (!m_CameFrom.count(next)) // not visited
|
m_CameFrom[next] = current.tile;
|
||||||
{
|
frontier.push({Heuristic(end, next), next});
|
||||||
m_CameFrom[next] = current.tile;
|
|
||||||
frontier.push({Heuristic(end, next), next});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// reconstruct path
|
// reconstruct path
|
||||||
if (!m_CameFrom.count(end))
|
if (!m_CameFrom.count(end))
|
||||||
return {}; // goal never reached
|
return {}; // goal never reached
|
||||||
|
|
||||||
Path path;
|
Path path;
|
||||||
TilePos cur = end;
|
TilePos cur = end;
|
||||||
path.push_back(m_Map->TileToWorld(cur));
|
path.push_back(m_Map->TileToWorld(cur));
|
||||||
|
|
||||||
while (cur != start)
|
while (cur != start) {
|
||||||
{
|
cur = m_CameFrom[cur];
|
||||||
cur = m_CameFrom[cur];
|
path.push_back(m_Map->TileToWorld(cur));
|
||||||
path.push_back(m_Map->TileToWorld(cur));
|
|
||||||
}
|
}
|
||||||
std::reverse(path.begin(), path.end());
|
std::reverse(path.begin(), path.end());
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // pathfinder namespace
|
} // namespace pathfinder
|
||||||
|
|||||||
@@ -10,17 +10,17 @@
|
|||||||
|
|
||||||
namespace pathfinder {
|
namespace pathfinder {
|
||||||
|
|
||||||
class GBFS: public PathFinderBase {
|
class GBFS : public PathFinderBase {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
GBFS(const Map* m): PathFinderBase(m) {}
|
GBFS(const Map *m) : PathFinderBase(m) {}
|
||||||
Path CalculatePath(WorldPos start, WorldPos end) override;
|
Path CalculatePath(WorldPos start, WorldPos end) override;
|
||||||
const std::string_view& GetName() const override { return m_Name; }
|
const std::string_view &GetName() const override { return m_Name; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static float Heuristic(const TilePos& a, const TilePos& b);
|
static float Heuristic(const TilePos &a, const TilePos &b);
|
||||||
const std::string_view m_Name = "Greedy Best First Search";
|
const std::string_view m_Name = "Greedy Best First Search";
|
||||||
std::unordered_map<TilePos, TilePos, TilePosHash> m_CameFrom;
|
std::unordered_map<TilePos, TilePos, TilePosHash> m_CameFrom;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // pathfinder namespace
|
} // namespace pathfinder
|
||||||
|
|||||||
@@ -3,9 +3,9 @@
|
|||||||
#include "utils.hpp"
|
#include "utils.hpp"
|
||||||
|
|
||||||
#include "base.hpp"
|
#include "base.hpp"
|
||||||
|
#include "log.hpp"
|
||||||
#include "map.hpp"
|
#include "map.hpp"
|
||||||
#include "math.hpp"
|
#include "math.hpp"
|
||||||
#include "log.hpp"
|
|
||||||
#include "pathfinder/bfs.hpp"
|
#include "pathfinder/bfs.hpp"
|
||||||
#include "pathfinder/dijkstra.hpp"
|
#include "pathfinder/dijkstra.hpp"
|
||||||
#include "pathfinder/gbfs.hpp"
|
#include "pathfinder/gbfs.hpp"
|
||||||
@@ -13,23 +13,23 @@
|
|||||||
namespace pathfinder {
|
namespace pathfinder {
|
||||||
namespace utils {
|
namespace utils {
|
||||||
|
|
||||||
std::unique_ptr<PathFinderBase> create(PathFinderType type, const Map* map) {
|
std::unique_ptr<PathFinderBase> create(PathFinderType type, const Map *map) {
|
||||||
using namespace pathfinder;
|
using namespace pathfinder;
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case PathFinderType::LINEAR:
|
case PathFinderType::LINEAR:
|
||||||
return std::move(std::make_unique<LinearPathFinder>(map));
|
return std::move(std::make_unique<LinearPathFinder>(map));
|
||||||
case PathFinderType::BFS:
|
case PathFinderType::BFS:
|
||||||
return std::move(std::make_unique<BFS>(map));
|
return std::move(std::make_unique<BFS>(map));
|
||||||
case PathFinderType::DIJKSTRA:
|
case PathFinderType::DIJKSTRA:
|
||||||
return std::move(std::make_unique<Dijkstra>(map));
|
return std::move(std::make_unique<Dijkstra>(map));
|
||||||
case PathFinderType::GBFS:
|
case PathFinderType::GBFS:
|
||||||
return std::move(std::make_unique<GBFS>(map));
|
return std::move(std::make_unique<GBFS>(map));
|
||||||
case PathFinderType::COUNT:
|
case PathFinderType::COUNT:
|
||||||
LOG_WARNING("Incorrect pathfinder type");
|
LOG_WARNING("Incorrect pathfinder type");
|
||||||
return nullptr;
|
return nullptr;
|
||||||
};
|
};
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // utils namespace
|
} // namespace utils
|
||||||
} // pathfinding namespace
|
} // namespace pathfinder
|
||||||
|
|||||||
@@ -10,16 +10,16 @@
|
|||||||
namespace pathfinder {
|
namespace pathfinder {
|
||||||
namespace utils {
|
namespace utils {
|
||||||
|
|
||||||
struct QueueEntry
|
struct QueueEntry {
|
||||||
{
|
float cost;
|
||||||
float cost;
|
TilePos tile;
|
||||||
TilePos tile;
|
|
||||||
|
|
||||||
// min-heap -> smallest cost on top
|
// min-heap -> smallest cost on top
|
||||||
bool operator>(const QueueEntry& o) const noexcept { return cost > o.cost; }
|
bool operator>(const QueueEntry &o) const noexcept { return cost > o.cost; }
|
||||||
};
|
};
|
||||||
|
|
||||||
std::unique_ptr<pathfinder::PathFinderBase> create(pathfinder::PathFinderType type, const Map* map);
|
std::unique_ptr<pathfinder::PathFinderBase>
|
||||||
|
create(pathfinder::PathFinderType type, const Map *map);
|
||||||
|
|
||||||
} // utils namespace
|
} // namespace utils
|
||||||
} // pathfinding namespace
|
} // namespace pathfinder
|
||||||
|
|||||||
@@ -8,17 +8,16 @@
|
|||||||
#include "entities.hpp"
|
#include "entities.hpp"
|
||||||
#include "log.hpp"
|
#include "log.hpp"
|
||||||
#include "map.hpp"
|
#include "map.hpp"
|
||||||
#include "user_input.hpp"
|
|
||||||
#include "pathfinder/base.hpp"
|
#include "pathfinder/base.hpp"
|
||||||
#include "pathfinder/utils.hpp"
|
#include "pathfinder/utils.hpp"
|
||||||
#include "tile.hpp"
|
#include "tile.hpp"
|
||||||
|
#include "user_input.hpp"
|
||||||
|
|
||||||
PathFindingDemo::PathFindingDemo(int width, int height) :
|
PathFindingDemo::PathFindingDemo(int width, int height) : m_Map(width, height) {
|
||||||
m_Map(width, height)
|
|
||||||
{
|
|
||||||
LOG_DEBUG(".");
|
LOG_DEBUG(".");
|
||||||
// set default pathfinder method
|
// set default pathfinder method
|
||||||
m_PathFinder = pathfinder::utils::create(pathfinder::PathFinderType::DIJKSTRA, (const Map*)&m_Map);
|
m_PathFinder = pathfinder::utils::create(pathfinder::PathFinderType::DIJKSTRA,
|
||||||
|
(const Map *)&m_Map);
|
||||||
}
|
}
|
||||||
|
|
||||||
PathFindingDemo::~PathFindingDemo() { LOG_DEBUG("."); }
|
PathFindingDemo::~PathFindingDemo() { LOG_DEBUG("."); }
|
||||||
@@ -33,27 +32,27 @@ void PathFindingDemo::CreateMap() {
|
|||||||
m_Map.PaintCircle(TilePos{50, 50}, 10, TileType::WATER);
|
m_Map.PaintCircle(TilePos{50, 50}, 10, TileType::WATER);
|
||||||
m_Map.PaintCircle(TilePos{75, 100}, 50, TileType::WATER);
|
m_Map.PaintCircle(TilePos{75, 100}, 50, TileType::WATER);
|
||||||
// river
|
// river
|
||||||
m_Map.PaintLine(TilePos{0,0}, TilePos{100,100}, 3.0, TileType::WATER);
|
m_Map.PaintLine(TilePos{0, 0}, TilePos{100, 100}, 3.0, TileType::WATER);
|
||||||
// road
|
// road
|
||||||
m_Map.PaintLine(TilePos{17,6}, TilePos{100,6}, 5.0, TileType::ROAD);
|
m_Map.PaintLine(TilePos{17, 6}, TilePos{100, 6}, 5.0, TileType::ROAD);
|
||||||
m_Map.PaintLine(TilePos{10,17}, TilePos{10,100}, 5.0, TileType::ROAD);
|
m_Map.PaintLine(TilePos{10, 17}, TilePos{10, 100}, 5.0, TileType::ROAD);
|
||||||
m_Map.PaintLine(TilePos{20,10}, TilePos{10,20}, 5.0, TileType::ROAD);
|
m_Map.PaintLine(TilePos{20, 10}, TilePos{10, 20}, 5.0, TileType::ROAD);
|
||||||
// bridges
|
// bridges
|
||||||
m_Map.PaintLine(TilePos{50,75}, TilePos{70,75}, 5.0, TileType::WOOD);
|
m_Map.PaintLine(TilePos{50, 75}, TilePos{70, 75}, 5.0, TileType::WOOD);
|
||||||
m_Map.PaintLine(TilePos{95,26}, TilePos{95,60}, 5.0, TileType::WOOD);
|
m_Map.PaintLine(TilePos{95, 26}, TilePos{95, 60}, 5.0, TileType::WOOD);
|
||||||
// island
|
// island
|
||||||
m_Map.PaintRectangle(TilePos{70, 60}, TilePos{100,100}, TileType::GRASS);
|
m_Map.PaintRectangle(TilePos{70, 60}, TilePos{100, 100}, TileType::GRASS);
|
||||||
// walls
|
// walls
|
||||||
m_Map.PaintLine(TilePos{71,60}, TilePos{90,60}, 1.0, TileType::WALL);
|
m_Map.PaintLine(TilePos{71, 60}, TilePos{90, 60}, 1.0, TileType::WALL);
|
||||||
m_Map.PaintLine(TilePos{77,67}, TilePos{100,67}, 1.0, TileType::WALL);
|
m_Map.PaintLine(TilePos{77, 67}, TilePos{100, 67}, 1.0, TileType::WALL);
|
||||||
m_Map.PaintLine(TilePos{71,60}, TilePos{71,75}, 1.0, TileType::WALL);
|
m_Map.PaintLine(TilePos{71, 60}, TilePos{71, 75}, 1.0, TileType::WALL);
|
||||||
m_Map.PaintLine(TilePos{72,73}, TilePos{95,73}, 1.0, TileType::WALL);
|
m_Map.PaintLine(TilePos{72, 73}, TilePos{95, 73}, 1.0, TileType::WALL);
|
||||||
m_Map.PaintLine(TilePos{95,73}, TilePos{95,90}, 1.0, TileType::WALL);
|
m_Map.PaintLine(TilePos{95, 73}, TilePos{95, 90}, 1.0, TileType::WALL);
|
||||||
m_Map.PaintLine(TilePos{71,81}, TilePos{71,100}, 1.0, TileType::WALL);
|
m_Map.PaintLine(TilePos{71, 81}, TilePos{71, 100}, 1.0, TileType::WALL);
|
||||||
m_Map.PaintLine(TilePos{72,81}, TilePos{90,81}, 1.0, TileType::WALL);
|
m_Map.PaintLine(TilePos{72, 81}, TilePos{90, 81}, 1.0, TileType::WALL);
|
||||||
m_Map.PaintLine(TilePos{89,87}, TilePos{89,100}, 1.0, TileType::WALL);
|
m_Map.PaintLine(TilePos{89, 87}, TilePos{89, 100}, 1.0, TileType::WALL);
|
||||||
m_Map.PaintLine(TilePos{84,81}, TilePos{84,96}, 1.0, TileType::WALL);
|
m_Map.PaintLine(TilePos{84, 81}, TilePos{84, 96}, 1.0, TileType::WALL);
|
||||||
m_Map.PaintLine(TilePos{78,87}, TilePos{78,100}, 1.0, TileType::WALL);
|
m_Map.PaintLine(TilePos{78, 87}, TilePos{78, 100}, 1.0, TileType::WALL);
|
||||||
|
|
||||||
// add some controllable entities
|
// add some controllable entities
|
||||||
m_Entities.clear();
|
m_Entities.clear();
|
||||||
@@ -65,19 +64,16 @@ void PathFindingDemo::CreateMap() {
|
|||||||
player2->SetPosition(m_Map.TileToWorld(TilePos{50, 20}));
|
player2->SetPosition(m_Map.TileToWorld(TilePos{50, 20}));
|
||||||
AddEntity(player2);
|
AddEntity(player2);
|
||||||
|
|
||||||
for (int i = 0; i < 1; i++)
|
for (int i = 0; i < 1; i++) {
|
||||||
{
|
for (int j = 0; j < 10; j++) {
|
||||||
for (int j = 0; j < 10; j++)
|
|
||||||
{
|
|
||||||
auto p = std::make_shared<Player>();
|
auto p = std::make_shared<Player>();
|
||||||
p->SetPosition(m_Map.TileToWorld(TilePos{10+5*i, 40+5*j}));
|
p->SetPosition(m_Map.TileToWorld(TilePos{10 + 5 * i, 40 + 5 * j}));
|
||||||
AddEntity(p);
|
AddEntity(p);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// select everything - TODO this is just temporary for testing
|
// select everything - TODO this is just temporary for testing
|
||||||
for (const auto& entity : m_Entities)
|
for (const auto &entity : m_Entities) {
|
||||||
{
|
|
||||||
m_SelectedEntities.push_back(entity);
|
m_SelectedEntities.push_back(entity);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -86,23 +82,17 @@ WorldPos PathFindingDemo::GetRandomPosition() const {
|
|||||||
return WorldPos{0.0f, 0.0f}; // totally random!
|
return WorldPos{0.0f, 0.0f}; // totally random!
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const std::vector<Collision> &PathFindingDemo::GetEntityCollisions() {
|
||||||
|
|
||||||
const std::vector<Collision>& PathFindingDemo::GetEntityCollisions()
|
|
||||||
{
|
|
||||||
static std::vector<Collision> m_Collisions;
|
static std::vector<Collision> m_Collisions;
|
||||||
m_Collisions.clear();
|
m_Collisions.clear();
|
||||||
|
|
||||||
for (const auto &entity_A : m_Entities)
|
for (const auto &entity_A : m_Entities) {
|
||||||
{
|
for (const auto &entity_B : m_Entities) {
|
||||||
for (const auto &entity_B : m_Entities)
|
|
||||||
{
|
|
||||||
if (entity_A == entity_B)
|
if (entity_A == entity_B)
|
||||||
continue;
|
continue;
|
||||||
if (!entity_A->IsCollidable() || !entity_B->IsCollidable())
|
if (!entity_A->IsCollidable() || !entity_B->IsCollidable())
|
||||||
continue;
|
continue;
|
||||||
if (entity_A->CollidesWith(*entity_B))
|
if (entity_A->CollidesWith(*entity_B)) {
|
||||||
{
|
|
||||||
// handle collision logic
|
// handle collision logic
|
||||||
m_Collisions.emplace_back(Collision(entity_A, entity_B));
|
m_Collisions.emplace_back(Collision(entity_A, entity_B));
|
||||||
}
|
}
|
||||||
@@ -111,37 +101,33 @@ const std::vector<Collision>& PathFindingDemo::GetEntityCollisions()
|
|||||||
return m_Collisions;
|
return m_Collisions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Update entity positions, handle collisions
|
// Update entity positions, handle collisions
|
||||||
void PathFindingDemo::UpdateWorld() {
|
void PathFindingDemo::UpdateWorld() {
|
||||||
|
|
||||||
float time_delta = 1.0f;
|
float time_delta = 1.0f;
|
||||||
|
|
||||||
for (auto& entity : m_Entities)
|
for (auto &entity : m_Entities) {
|
||||||
{
|
|
||||||
// calculate the velocity
|
// calculate the velocity
|
||||||
auto current_pos = entity->GetPosition();
|
auto current_pos = entity->GetPosition();
|
||||||
double tile_velocity_coeff = m_Map.GetTileVelocityCoeff(current_pos);
|
double tile_velocity_coeff = m_Map.GetTileVelocityCoeff(current_pos);
|
||||||
auto next_pos = entity->GetMoveTarget();
|
auto next_pos = entity->GetMoveTarget();
|
||||||
WorldPos velocity = WorldPos{};
|
WorldPos velocity = WorldPos{};
|
||||||
if (next_pos)
|
if (next_pos) {
|
||||||
{
|
|
||||||
velocity = next_pos.value() - current_pos;
|
velocity = next_pos.value() - current_pos;
|
||||||
velocity.Normalize();
|
velocity.Normalize();
|
||||||
//LOG_DEBUG("I want to move to: ", next_pos.value(),
|
// LOG_DEBUG("I want to move to: ", next_pos.value(),
|
||||||
// ", velocity: ", velocity);
|
// ", velocity: ", velocity);
|
||||||
}
|
}
|
||||||
entity->SetActualVelocity(velocity * tile_velocity_coeff);
|
entity->SetActualVelocity(velocity * tile_velocity_coeff);
|
||||||
|
|
||||||
for (const auto& collision : GetEntityCollisions())
|
for (const auto &collision : GetEntityCollisions()) {
|
||||||
{
|
// TODO this loop is quite "hot", is it good idea to use weak_ptr and
|
||||||
// TODO this loop is quite "hot", is it good idea to use weak_ptr and promote it?
|
// promote it?
|
||||||
auto weak_A = std::get<0>(collision);
|
auto weak_A = std::get<0>(collision);
|
||||||
auto weak_B = std::get<1>(collision);
|
auto weak_B = std::get<1>(collision);
|
||||||
auto A = weak_A.lock();
|
auto A = weak_A.lock();
|
||||||
auto B = weak_B.lock();
|
auto B = weak_B.lock();
|
||||||
if (A == nullptr || B == nullptr)
|
if (A == nullptr || B == nullptr) {
|
||||||
{
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (!A->IsMovable())
|
if (!A->IsMovable())
|
||||||
@@ -159,58 +145,43 @@ void PathFindingDemo::UpdateWorld() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void PathFindingDemo::HandleActions(const std::vector<UserAction> &actions)
|
void PathFindingDemo::HandleActions(const std::vector<UserAction> &actions) {
|
||||||
{
|
for (const auto &action : actions) {
|
||||||
for (const auto &action : actions)
|
if (action.type == UserAction::Type::EXIT) {
|
||||||
{
|
|
||||||
if (action.type == UserAction::Type::EXIT)
|
|
||||||
{
|
|
||||||
LOG_INFO("Exit requested");
|
LOG_INFO("Exit requested");
|
||||||
m_ExitRequested = true;
|
m_ExitRequested = true;
|
||||||
}
|
} else if (action.type == UserAction::Type::SET_MOVE_TARGET) {
|
||||||
else if (action.type == UserAction::Type::SET_MOVE_TARGET)
|
|
||||||
{
|
|
||||||
WorldPos target_pos = m_Camera.WindowToWorld(action.Argument.position);
|
WorldPos target_pos = m_Camera.WindowToWorld(action.Argument.position);
|
||||||
for (auto& selected_entity : m_SelectedEntities)
|
for (auto &selected_entity : m_SelectedEntities) {
|
||||||
{
|
|
||||||
LOG_INFO("Calculating path to target: ", target_pos);
|
LOG_INFO("Calculating path to target: ", target_pos);
|
||||||
if (auto sp = selected_entity.lock())
|
if (auto sp = selected_entity.lock()) {
|
||||||
{
|
auto path =
|
||||||
auto path = m_PathFinder->CalculatePath(sp->GetPosition(), target_pos);
|
m_PathFinder->CalculatePath(sp->GetPosition(), target_pos);
|
||||||
sp->SetPath(path);
|
sp->SetPath(path);
|
||||||
LOG_INFO("Done, path node count: ", path.size());
|
LOG_INFO("Done, path node count: ", path.size());
|
||||||
} else {
|
} else {
|
||||||
LOG_INFO("Cannot calculate path for destroyed entity (weak_ptr.lock() failed)");
|
LOG_INFO("Cannot calculate path for destroyed entity "
|
||||||
|
"(weak_ptr.lock() failed)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} else if (action.type == UserAction::Type::SELECT_PATHFINDER) {
|
||||||
else if (action.type == UserAction::Type::SELECT_PATHFINDER)
|
|
||||||
{
|
|
||||||
using namespace pathfinder;
|
using namespace pathfinder;
|
||||||
PathFinderType type = static_cast<PathFinderType>(action.Argument.number);
|
PathFinderType type = static_cast<PathFinderType>(action.Argument.number);
|
||||||
m_PathFinder = pathfinder::utils::create(type, (const Map*)&m_Map);
|
m_PathFinder = pathfinder::utils::create(type, (const Map *)&m_Map);
|
||||||
LOG_INFO("Switched to path finding method: ", m_PathFinder->GetName());
|
LOG_INFO("Switched to path finding method: ", m_PathFinder->GetName());
|
||||||
}
|
} else if (action.type == UserAction::Type::CAMERA_PAN) {
|
||||||
else if (action.type == UserAction::Type::CAMERA_PAN)
|
const auto &window_pan = action.Argument.position;
|
||||||
{
|
|
||||||
const auto& window_pan = action.Argument.position;
|
|
||||||
WorldPos world_pan{window_pan.x(), window_pan.y()};
|
WorldPos world_pan{window_pan.x(), window_pan.y()};
|
||||||
m_Camera.Pan(world_pan);
|
m_Camera.Pan(world_pan);
|
||||||
LOG_INFO("Camera pan delta: ", world_pan);
|
LOG_INFO("Camera pan delta: ", world_pan);
|
||||||
}
|
} else if (action.type == UserAction::Type::CAMERA_ZOOM) {
|
||||||
else if (action.type == UserAction::Type::CAMERA_ZOOM)
|
|
||||||
{
|
|
||||||
m_Camera.Zoom(action.Argument.float_number);
|
m_Camera.Zoom(action.Argument.float_number);
|
||||||
LOG_INFO("Camera zoom: ", action.Argument.float_number);
|
LOG_INFO("Camera zoom: ", action.Argument.float_number);
|
||||||
}
|
} else if (action.type == UserAction::Type::SELECTION_START) {
|
||||||
else if (action.type == UserAction::Type::SELECTION_START)
|
|
||||||
{
|
|
||||||
m_SelectionBox.active = true;
|
m_SelectionBox.active = true;
|
||||||
m_SelectionBox.start = action.Argument.position;
|
m_SelectionBox.start = action.Argument.position;
|
||||||
m_SelectionBox.end = action.Argument.position;
|
m_SelectionBox.end = action.Argument.position;
|
||||||
}
|
} else if (action.type == UserAction::Type::SELECTION_END) {
|
||||||
else if (action.type == UserAction::Type::SELECTION_END)
|
|
||||||
{
|
|
||||||
m_SelectionBox.end = action.Argument.position;
|
m_SelectionBox.end = action.Argument.position;
|
||||||
m_SelectionBox.active = false;
|
m_SelectionBox.active = false;
|
||||||
auto diff = m_SelectionBox.end - m_SelectionBox.start;
|
auto diff = m_SelectionBox.end - m_SelectionBox.start;
|
||||||
@@ -219,9 +190,7 @@ void PathFindingDemo::HandleActions(const std::vector<UserAction> &actions)
|
|||||||
WorldPos start = m_Camera.WindowToWorld(m_SelectionBox.start);
|
WorldPos start = m_Camera.WindowToWorld(m_SelectionBox.start);
|
||||||
WorldPos end = m_Camera.WindowToWorld(m_SelectionBox.end);
|
WorldPos end = m_Camera.WindowToWorld(m_SelectionBox.end);
|
||||||
SelectEntitiesInRectangle(start, end);
|
SelectEntitiesInRectangle(start, end);
|
||||||
}
|
} else if (action.type == UserAction::Type::SELECTION_CHANGE) {
|
||||||
else if (action.type == UserAction::Type::SELECTION_CHANGE)
|
|
||||||
{
|
|
||||||
m_SelectionBox.end = action.Argument.position;
|
m_SelectionBox.end = action.Argument.position;
|
||||||
auto diff = m_SelectionBox.end - m_SelectionBox.start;
|
auto diff = m_SelectionBox.end - m_SelectionBox.start;
|
||||||
m_SelectionBox.size = diff.ChangeTag<WindowSizeTag>();
|
m_SelectionBox.size = diff.ChangeTag<WindowSizeTag>();
|
||||||
@@ -229,30 +198,25 @@ void PathFindingDemo::HandleActions(const std::vector<UserAction> &actions)
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
void PathFindingDemo::DeselectEntities()
|
void PathFindingDemo::DeselectEntities() {
|
||||||
{
|
std::for_each(m_SelectedEntities.begin(), m_SelectedEntities.end(),
|
||||||
std::for_each(m_SelectedEntities.begin(), m_SelectedEntities.end(), [](auto& x)
|
[](auto &x) {
|
||||||
{
|
if (auto entity = x.lock())
|
||||||
if (auto entity = x.lock())
|
entity->Deselect();
|
||||||
entity->Deselect();
|
});
|
||||||
}
|
|
||||||
);
|
|
||||||
m_SelectedEntities.clear();
|
m_SelectedEntities.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
void PathFindingDemo::SelectEntitiesInRectangle(WorldPos A, WorldPos B)
|
void PathFindingDemo::SelectEntitiesInRectangle(WorldPos A, WorldPos B) {
|
||||||
{
|
|
||||||
DeselectEntities();
|
DeselectEntities();
|
||||||
// TODO use colliders for this
|
// TODO use colliders for this
|
||||||
auto [x_min, x_max] = std::minmax(A.x(), B.x());
|
auto [x_min, x_max] = std::minmax(A.x(), B.x());
|
||||||
auto [y_min, y_max] = std::minmax(A.y(), B.y());
|
auto [y_min, y_max] = std::minmax(A.y(), B.y());
|
||||||
for (const auto& entity : m_Entities)
|
for (const auto &entity : m_Entities) {
|
||||||
{
|
const auto &pos = entity->GetPosition();
|
||||||
const auto& pos = entity->GetPosition();
|
|
||||||
bool x_in_range = x_min < pos.x() && pos.x() < x_max;
|
bool x_in_range = x_min < pos.x() && pos.x() < x_max;
|
||||||
bool y_in_range = y_min < pos.y() && pos.y() < y_max;
|
bool y_in_range = y_min < pos.y() && pos.y() < y_max;
|
||||||
if (x_in_range && y_in_range)
|
if (x_in_range && y_in_range) {
|
||||||
{
|
|
||||||
m_SelectedEntities.push_back(std::weak_ptr(entity));
|
m_SelectedEntities.push_back(std::weak_ptr(entity));
|
||||||
entity->Select();
|
entity->Select();
|
||||||
}
|
}
|
||||||
@@ -260,12 +224,9 @@ void PathFindingDemo::SelectEntitiesInRectangle(WorldPos A, WorldPos B)
|
|||||||
LOG_INFO("Selected ", m_SelectedEntities.size(), " entities");
|
LOG_INFO("Selected ", m_SelectedEntities.size(), " entities");
|
||||||
}
|
}
|
||||||
|
|
||||||
std::pair<WindowPos, WindowSize> PathFindingDemo::GetSelectionBoxPosSize()
|
std::pair<WindowPos, WindowSize> PathFindingDemo::GetSelectionBoxPosSize() {
|
||||||
{
|
const auto &pos = m_SelectionBox.start;
|
||||||
const auto& pos = m_SelectionBox.start;
|
|
||||||
WindowPos size_pos = m_SelectionBox.end - m_SelectionBox.start;
|
WindowPos size_pos = m_SelectionBox.end - m_SelectionBox.start;
|
||||||
WindowSize size = size_pos.ChangeTag<WindowSizeTag>();
|
WindowSize size = size_pos.ChangeTag<WindowSizeTag>();
|
||||||
return std::pair(pos, size);
|
return std::pair(pos, size);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,17 +5,16 @@
|
|||||||
#include <queue>
|
#include <queue>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include "camera.hpp"
|
||||||
#include "entities.hpp"
|
#include "entities.hpp"
|
||||||
#include "log.hpp"
|
#include "log.hpp"
|
||||||
#include "map.hpp"
|
#include "map.hpp"
|
||||||
#include "user_input.hpp"
|
|
||||||
#include "pathfinder/base.hpp"
|
#include "pathfinder/base.hpp"
|
||||||
#include "camera.hpp"
|
#include "user_input.hpp"
|
||||||
|
|
||||||
using Collision = std::pair<std::weak_ptr<Entity>, std::weak_ptr<Entity>>;
|
using Collision = std::pair<std::weak_ptr<Entity>, std::weak_ptr<Entity>>;
|
||||||
|
|
||||||
struct SelectionBox
|
struct SelectionBox {
|
||||||
{
|
|
||||||
WindowPos start, end;
|
WindowPos start, end;
|
||||||
WindowSize size;
|
WindowSize size;
|
||||||
bool active;
|
bool active;
|
||||||
@@ -31,9 +30,9 @@ public:
|
|||||||
PathFindingDemo &operator=(const PathFindingDemo &) = delete;
|
PathFindingDemo &operator=(const PathFindingDemo &) = delete;
|
||||||
PathFindingDemo &operator=(PathFindingDemo &&) = delete;
|
PathFindingDemo &operator=(PathFindingDemo &&) = delete;
|
||||||
|
|
||||||
std::vector<std::shared_ptr<Entity>>& GetEntities() { return m_Entities; }
|
std::vector<std::shared_ptr<Entity>> &GetEntities() { return m_Entities; }
|
||||||
const Map& GetMap() const { return m_Map; }
|
const Map &GetMap() const { return m_Map; }
|
||||||
const Camera& GetCamera() const { return m_Camera; }
|
const Camera &GetCamera() const { return m_Camera; }
|
||||||
bool IsExitRequested() const { return m_ExitRequested; }
|
bool IsExitRequested() const { return m_ExitRequested; }
|
||||||
|
|
||||||
void AddEntity(std::shared_ptr<Entity> e);
|
void AddEntity(std::shared_ptr<Entity> e);
|
||||||
@@ -46,10 +45,12 @@ public:
|
|||||||
void DeselectEntities();
|
void DeselectEntities();
|
||||||
bool IsSelectionBoxActive() const { return m_SelectionBox.active; }
|
bool IsSelectionBoxActive() const { return m_SelectionBox.active; }
|
||||||
std::pair<WindowPos, WindowSize> GetSelectionBoxPosSize();
|
std::pair<WindowPos, WindowSize> GetSelectionBoxPosSize();
|
||||||
std::vector<std::weak_ptr<Entity>> GetSelectedEntities() { return m_SelectedEntities; }
|
std::vector<std::weak_ptr<Entity>> GetSelectedEntities() {
|
||||||
|
return m_SelectedEntities;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const std::vector<Collision>& GetEntityCollisions();
|
const std::vector<Collision> &GetEntityCollisions();
|
||||||
|
|
||||||
bool m_ExitRequested = false;
|
bool m_ExitRequested = false;
|
||||||
Map m_Map;
|
Map m_Map;
|
||||||
|
|||||||
@@ -6,9 +6,9 @@
|
|||||||
// we could use array here, but this is more explicit,
|
// we could use array here, but this is more explicit,
|
||||||
// and we don't access tile_types that often, so it should be ok
|
// and we don't access tile_types that often, so it should be ok
|
||||||
const std::unordered_map<TileType, Tile> tile_types = {
|
const std::unordered_map<TileType, Tile> tile_types = {
|
||||||
{ TileType::GRASS, Tile{1.0, 0, 200, 0, 255}},
|
{TileType::GRASS, Tile{1.0, 0, 200, 0, 255}},
|
||||||
{ TileType::WOOD, Tile{1.0, 132, 68, 0, 255}},
|
{TileType::WOOD, Tile{1.0, 132, 68, 0, 255}},
|
||||||
{ TileType::ROAD, Tile{0.5, 20, 20, 20, 255}},
|
{TileType::ROAD, Tile{0.5, 20, 20, 20, 255}},
|
||||||
{ TileType::WATER, Tile{10.0, 0, 50, 200, 255}},
|
{TileType::WATER, Tile{10.0, 0, 50, 200, 255}},
|
||||||
{ TileType::WALL, Tile{1000.0, 144, 33, 0, 255}},
|
{TileType::WALL, Tile{1000.0, 144, 33, 0, 255}},
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <array>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
#include <array>
|
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
struct Tile {
|
struct Tile {
|
||||||
|
|||||||
@@ -2,8 +2,8 @@
|
|||||||
#include <expected>
|
#include <expected>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
|
||||||
#include <unordered_set>
|
#include <unordered_set>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include "user_input.hpp"
|
#include "user_input.hpp"
|
||||||
|
|
||||||
@@ -20,130 +20,103 @@ UserInput::~UserInput() { LOG_DEBUG("."); };
|
|||||||
|
|
||||||
std::expected<void, std::string> UserInput::Init() { return {}; }
|
std::expected<void, std::string> UserInput::Init() { return {}; }
|
||||||
|
|
||||||
void UserInput::GetActions_mouse(const SDL_Event& event)
|
void UserInput::GetActions_mouse(const SDL_Event &event) {
|
||||||
{
|
|
||||||
static bool mouse_pan = false;
|
static bool mouse_pan = false;
|
||||||
|
|
||||||
SDL_MouseButtonEvent mouse_event = event.button;
|
SDL_MouseButtonEvent mouse_event = event.button;
|
||||||
MouseButton button = static_cast<MouseButton>(mouse_event.button);
|
MouseButton button = static_cast<MouseButton>(mouse_event.button);
|
||||||
|
|
||||||
if (event.type == SDL_EVENT_MOUSE_BUTTON_DOWN)
|
if (event.type == SDL_EVENT_MOUSE_BUTTON_DOWN) {
|
||||||
{
|
if (button == MouseButton::LEFT) {
|
||||||
if (button == MouseButton::LEFT)
|
|
||||||
{
|
|
||||||
LOG_DEBUG("Selection start at ", mouse_event.x, ", ", mouse_event.y);
|
LOG_DEBUG("Selection start at ", mouse_event.x, ", ", mouse_event.y);
|
||||||
m_SelectionActive = true;
|
m_SelectionActive = true;
|
||||||
m_Actions.emplace_back(UserAction::Type::SELECTION_START,
|
m_Actions.emplace_back(UserAction::Type::SELECTION_START,
|
||||||
WindowPos{mouse_event.x, mouse_event.y});
|
WindowPos{mouse_event.x, mouse_event.y});
|
||||||
}
|
} else if (button == MouseButton::RIGHT) {
|
||||||
else if (button == MouseButton::RIGHT)
|
|
||||||
{
|
|
||||||
LOG_DEBUG("Set move target to: ", mouse_event.x, ", ", mouse_event.y);
|
LOG_DEBUG("Set move target to: ", mouse_event.x, ", ", mouse_event.y);
|
||||||
m_Actions.emplace_back(UserAction::Type::SET_MOVE_TARGET,
|
m_Actions.emplace_back(UserAction::Type::SET_MOVE_TARGET,
|
||||||
WindowPos{mouse_event.x, mouse_event.y});
|
WindowPos{mouse_event.x, mouse_event.y});
|
||||||
}
|
} else if (button == MouseButton::MIDDLE) {
|
||||||
else if (button == MouseButton::MIDDLE)
|
|
||||||
{
|
|
||||||
mouse_pan = true;
|
mouse_pan = true;
|
||||||
}
|
}
|
||||||
}
|
} else if (event.type == SDL_EVENT_MOUSE_BUTTON_UP) {
|
||||||
else if (event.type == SDL_EVENT_MOUSE_BUTTON_UP)
|
if (button == MouseButton::LEFT) {
|
||||||
{
|
|
||||||
if (button == MouseButton::LEFT)
|
|
||||||
{
|
|
||||||
LOG_DEBUG("Selection end at ", mouse_event.x, ", ", mouse_event.y);
|
LOG_DEBUG("Selection end at ", mouse_event.x, ", ", mouse_event.y);
|
||||||
m_SelectionActive = false;
|
m_SelectionActive = false;
|
||||||
m_Actions.emplace_back(UserAction::Type::SELECTION_END,
|
m_Actions.emplace_back(UserAction::Type::SELECTION_END,
|
||||||
WindowPos{mouse_event.x, mouse_event.y});
|
WindowPos{mouse_event.x, mouse_event.y});
|
||||||
}
|
}
|
||||||
if (button == MouseButton::MIDDLE)
|
if (button == MouseButton::MIDDLE) {
|
||||||
{
|
|
||||||
mouse_pan = false;
|
mouse_pan = false;
|
||||||
}
|
}
|
||||||
}
|
} else if (event.type == SDL_EVENT_MOUSE_MOTION) {
|
||||||
else if (event.type == SDL_EVENT_MOUSE_MOTION)
|
|
||||||
{
|
|
||||||
SDL_MouseMotionEvent motion_event = event.motion;
|
SDL_MouseMotionEvent motion_event = event.motion;
|
||||||
if (mouse_pan)
|
if (mouse_pan) {
|
||||||
{
|
|
||||||
m_Actions.emplace_back(UserAction::Type::CAMERA_PAN,
|
m_Actions.emplace_back(UserAction::Type::CAMERA_PAN,
|
||||||
WindowPos{motion_event.xrel, motion_event.yrel});
|
WindowPos{motion_event.xrel, motion_event.yrel});
|
||||||
}
|
}
|
||||||
if (m_SelectionActive)
|
if (m_SelectionActive) {
|
||||||
{
|
|
||||||
m_Actions.emplace_back(UserAction::Type::SELECTION_CHANGE,
|
m_Actions.emplace_back(UserAction::Type::SELECTION_CHANGE,
|
||||||
WindowPos{mouse_event.x, mouse_event.y});
|
WindowPos{mouse_event.x, mouse_event.y});
|
||||||
}
|
}
|
||||||
}
|
} else if (event.type == SDL_EVENT_MOUSE_WHEEL) {
|
||||||
else if(event.type == SDL_EVENT_MOUSE_WHEEL)
|
|
||||||
{
|
|
||||||
SDL_MouseWheelEvent mouse_wheel = event.wheel;
|
SDL_MouseWheelEvent mouse_wheel = event.wheel;
|
||||||
m_Actions.emplace_back(UserAction::Type::CAMERA_ZOOM, mouse_wheel.y);
|
m_Actions.emplace_back(UserAction::Type::CAMERA_ZOOM, mouse_wheel.y);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void UserInput::GetActions_keyboard(const SDL_Event& event)
|
void UserInput::GetActions_keyboard(const SDL_Event &event) {
|
||||||
{
|
bool key_down = event.type == SDL_EVENT_KEY_DOWN ? true : false;
|
||||||
bool key_down = event.type == SDL_EVENT_KEY_DOWN ? true : false;
|
SDL_KeyboardEvent kbd_event = event.key;
|
||||||
SDL_KeyboardEvent kbd_event = event.key;
|
if (kbd_event.repeat) {
|
||||||
if (kbd_event.repeat) {
|
// SDL repeats KEY_DOWN if key is held down, we ignore that
|
||||||
// SDL repeats KEY_DOWN if key is held down, we ignore that
|
return;
|
||||||
return;
|
}
|
||||||
}
|
LOG_DEBUG("Key '", static_cast<char>(kbd_event.key),
|
||||||
LOG_DEBUG("Key '", static_cast<char>(kbd_event.key),
|
key_down ? "' down" : "' up");
|
||||||
key_down ? "' down" : "' up");
|
|
||||||
|
|
||||||
switch (kbd_event.key) {
|
switch (kbd_event.key) {
|
||||||
case 'q':
|
case 'q':
|
||||||
m_Actions.emplace_back(UserAction::Type::EXIT);
|
m_Actions.emplace_back(UserAction::Type::EXIT);
|
||||||
return;
|
return;
|
||||||
case '1':
|
case '1':
|
||||||
case '2':
|
case '2':
|
||||||
case '3':
|
case '3':
|
||||||
case '4':
|
case '4':
|
||||||
if (key_down) {
|
if (key_down) {
|
||||||
int selection = kbd_event.key - '0';
|
int selection = kbd_event.key - '0';
|
||||||
m_Actions.emplace_back(UserAction::Type::SELECT_PATHFINDER, selection);
|
m_Actions.emplace_back(UserAction::Type::SELECT_PATHFINDER, selection);
|
||||||
LOG_INFO("Pathfinder selected: ", selection);
|
LOG_INFO("Pathfinder selected: ", selection);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
LOG_INFO("Key '", static_cast<char>(kbd_event.key), "' not mapped");
|
LOG_INFO("Key '", static_cast<char>(kbd_event.key), "' not mapped");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::vector<UserAction>& UserInput::GetActions() {
|
const std::vector<UserAction> &UserInput::GetActions() {
|
||||||
|
|
||||||
static std::unordered_set<uint32_t> mouse_events = {
|
static std::unordered_set<uint32_t> mouse_events = {
|
||||||
SDL_EVENT_MOUSE_MOTION,
|
SDL_EVENT_MOUSE_MOTION, SDL_EVENT_MOUSE_BUTTON_DOWN,
|
||||||
SDL_EVENT_MOUSE_BUTTON_DOWN,
|
SDL_EVENT_MOUSE_BUTTON_UP, SDL_EVENT_MOUSE_WHEEL,
|
||||||
SDL_EVENT_MOUSE_BUTTON_UP,
|
SDL_EVENT_MOUSE_ADDED, SDL_EVENT_MOUSE_REMOVED,
|
||||||
SDL_EVENT_MOUSE_WHEEL,
|
|
||||||
SDL_EVENT_MOUSE_ADDED,
|
|
||||||
SDL_EVENT_MOUSE_REMOVED,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static std::unordered_set<uint32_t> keyboard_events = {
|
static std::unordered_set<uint32_t> keyboard_events = {
|
||||||
SDL_EVENT_KEY_DOWN,
|
SDL_EVENT_KEY_DOWN,
|
||||||
SDL_EVENT_KEY_UP,
|
SDL_EVENT_KEY_UP,
|
||||||
};
|
};
|
||||||
|
|
||||||
SDL_Event event;
|
SDL_Event event;
|
||||||
m_Actions.clear();
|
m_Actions.clear();
|
||||||
|
|
||||||
while (SDL_PollEvent(&event))
|
while (SDL_PollEvent(&event)) {
|
||||||
{
|
if (keyboard_events.contains(event.type)) {
|
||||||
if (keyboard_events.contains(event.type))
|
|
||||||
{
|
|
||||||
GetActions_keyboard(event);
|
GetActions_keyboard(event);
|
||||||
}
|
} else if (mouse_events.contains(event.type)) {
|
||||||
else if (mouse_events.contains(event.type))
|
|
||||||
{
|
|
||||||
GetActions_mouse(event);
|
GetActions_mouse(event);
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
// TODO uncomment, for now too much noise
|
// TODO uncomment, for now too much noise
|
||||||
// LOG_WARNING("Action not processed");
|
// LOG_WARNING("Action not processed");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,9 +12,7 @@ enum class MouseButton { LEFT = 1, MIDDLE, RIGHT };
|
|||||||
|
|
||||||
class UserAction {
|
class UserAction {
|
||||||
public:
|
public:
|
||||||
|
enum class Type {
|
||||||
enum class Type
|
|
||||||
{
|
|
||||||
NONE,
|
NONE,
|
||||||
EXIT,
|
EXIT,
|
||||||
SET_MOVE_TARGET,
|
SET_MOVE_TARGET,
|
||||||
@@ -44,7 +42,7 @@ public:
|
|||||||
} Argument;
|
} Argument;
|
||||||
|
|
||||||
// TODO use std::variant
|
// TODO use std::variant
|
||||||
//std::variant<WindowPos, char, int> Argument;
|
// std::variant<WindowPos, char, int> Argument;
|
||||||
};
|
};
|
||||||
|
|
||||||
class UserInput {
|
class UserInput {
|
||||||
@@ -65,6 +63,6 @@ private:
|
|||||||
std::vector<UserAction> m_Actions;
|
std::vector<UserAction> m_Actions;
|
||||||
bool m_SelectionActive = false;
|
bool m_SelectionActive = false;
|
||||||
|
|
||||||
void GetActions_keyboard(const SDL_Event&);
|
void GetActions_keyboard(const SDL_Event &);
|
||||||
void GetActions_mouse(const SDL_Event&);
|
void GetActions_mouse(const SDL_Event &);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -79,19 +79,20 @@ Window::~Window() {
|
|||||||
void Window::DrawSprite(const WindowPos &position, Sprite &s, float scale) {
|
void Window::DrawSprite(const WindowPos &position, Sprite &s, float scale) {
|
||||||
WorldSize size = s.GetSize() * scale;
|
WorldSize size = s.GetSize() * scale;
|
||||||
WorldPos img_center = s.GetCenter() * scale;
|
WorldPos img_center = s.GetCenter() * scale;
|
||||||
SDL_FRect rect = {position.x() - img_center.x(), position.y() - img_center.y(),
|
SDL_FRect rect = {position.x() - img_center.x(),
|
||||||
size.x(), size.y()};
|
position.y() - img_center.y(), size.x(), size.y()};
|
||||||
SDL_RenderTexture(m_Renderer.get(), s.GetTexture(), nullptr, &rect);
|
SDL_RenderTexture(m_Renderer.get(), s.GetTexture(), nullptr, &rect);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Window::DrawFilledRect(const WindowPos &position, const WindowSize size, uint8_t R,
|
void Window::DrawFilledRect(const WindowPos &position, const WindowSize size,
|
||||||
uint8_t G, uint8_t B, uint8_t A) {
|
uint8_t R, uint8_t G, uint8_t B, uint8_t A) {
|
||||||
SDL_FRect rect = {position.x(), position.y(), size.x(), size.y()};
|
SDL_FRect rect = {position.x(), position.y(), size.x(), size.y()};
|
||||||
SDL_SetRenderDrawColor(m_Renderer.get(), R, G, B, A);
|
SDL_SetRenderDrawColor(m_Renderer.get(), R, G, B, A);
|
||||||
SDL_RenderFillRect(m_Renderer.get(), &rect);
|
SDL_RenderFillRect(m_Renderer.get(), &rect);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Window::DrawRect(const WindowPos &position, const WindowSize size, uint8_t R, uint8_t G, uint8_t B) {
|
void Window::DrawRect(const WindowPos &position, const WindowSize size,
|
||||||
|
uint8_t R, uint8_t G, uint8_t B) {
|
||||||
SDL_FRect rect = {position.x(), position.y(), size.x(), size.y()};
|
SDL_FRect rect = {position.x(), position.y(), size.x(), size.y()};
|
||||||
SDL_SetRenderDrawColor(m_Renderer.get(), R, G, B, 255);
|
SDL_SetRenderDrawColor(m_Renderer.get(), R, G, B, 255);
|
||||||
SDL_RenderRect(m_Renderer.get(), &rect);
|
SDL_RenderRect(m_Renderer.get(), &rect);
|
||||||
@@ -105,7 +106,8 @@ void Window::ClearWindow() {
|
|||||||
void Window::Flush() { SDL_RenderPresent(m_Renderer.get()); }
|
void Window::Flush() { SDL_RenderPresent(m_Renderer.get()); }
|
||||||
|
|
||||||
// TODO use some struct for color
|
// TODO use some struct for color
|
||||||
void Window::DrawCircle(const WindowPos &position, float radius, uint8_t R, uint8_t G, uint8_t B) {
|
void Window::DrawCircle(const WindowPos &position, float radius, uint8_t R,
|
||||||
|
uint8_t G, uint8_t B) {
|
||||||
int cx = static_cast<int>(position.x());
|
int cx = static_cast<int>(position.x());
|
||||||
int cy = static_cast<int>(position.y());
|
int cy = static_cast<int>(position.y());
|
||||||
SDL_SetRenderDrawColor(m_Renderer.get(), R, G, B, 255);
|
SDL_SetRenderDrawColor(m_Renderer.get(), R, G, B, 255);
|
||||||
@@ -117,9 +119,7 @@ void Window::DrawCircle(const WindowPos &position, float radius, uint8_t R, uint
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Window::DrawLine(const WindowPos &A, const WindowPos &B)
|
void Window::DrawLine(const WindowPos &A, const WindowPos &B) {
|
||||||
{
|
|
||||||
SDL_SetRenderDrawColor(m_Renderer.get(), 255, 0, 0, 255);
|
SDL_SetRenderDrawColor(m_Renderer.get(), 255, 0, 0, 255);
|
||||||
SDL_RenderLine(m_Renderer.get(), A.x(), A.y(), B.x(), B.y());
|
SDL_RenderLine(m_Renderer.get(), A.x(), A.y(), B.x(), B.y());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -23,12 +23,14 @@ public:
|
|||||||
|
|
||||||
std::expected<void, std::string> Init();
|
std::expected<void, std::string> Init();
|
||||||
void DrawSprite(const WindowPos &position, Sprite &s, float scale = 1.0f);
|
void DrawSprite(const WindowPos &position, Sprite &s, float scale = 1.0f);
|
||||||
void DrawFilledRect(const WindowPos &position, const WindowSize size, uint8_t R,
|
void DrawFilledRect(const WindowPos &position, const WindowSize size,
|
||||||
uint8_t G, uint8_t B, uint8_t A);
|
uint8_t R, uint8_t G, uint8_t B, uint8_t A);
|
||||||
void DrawRect(const WindowPos &position, const WindowSize size, uint8_t R, uint8_t G, uint8_t B);
|
void DrawRect(const WindowPos &position, const WindowSize size, uint8_t R,
|
||||||
|
uint8_t G, uint8_t B);
|
||||||
void ClearWindow();
|
void ClearWindow();
|
||||||
void Flush();
|
void Flush();
|
||||||
void DrawCircle(const WindowPos &position, float radius, uint8_t R, uint8_t G, uint8_t B);
|
void DrawCircle(const WindowPos &position, float radius, uint8_t R, uint8_t G,
|
||||||
|
uint8_t B);
|
||||||
void DrawLine(const WindowPos &A, const WindowPos &B);
|
void DrawLine(const WindowPos &A, const WindowPos &B);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@@ -37,5 +39,4 @@ private:
|
|||||||
std::shared_ptr<SDL_Renderer> m_Renderer = nullptr;
|
std::shared_ptr<SDL_Renderer> m_Renderer = nullptr;
|
||||||
SDL_Window *m_Window;
|
SDL_Window *m_Window;
|
||||||
SDL_GLContext m_Context;
|
SDL_GLContext m_Context;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user