diff --git a/aStar/App.h b/aStar/App.h index 9c43953..a07be95 100644 --- a/aStar/App.h +++ b/aStar/App.h @@ -22,7 +22,7 @@ private: sf::Uint8* _pixelArray; sf::Sprite pixel_array_sprite; sf::Texture pixel_array_texture; - + Explorer* explorer; // Art assets diff --git a/aStar/Explorer.cpp b/aStar/Explorer.cpp index d26cd51..0e3bb6b 100644 --- a/aStar/Explorer.cpp +++ b/aStar/Explorer.cpp @@ -1,10 +1,11 @@ #include "Explorer.h" #include -Explorer::Explorer(Map* map_) { +Explorer::Explorer(Map* map_){ color = sf::Color::Blue; position = sf::Vector2i(10, 10); map = map_; + pather = new Pather(map_); } @@ -24,7 +25,7 @@ bool Explorer::move() { // While there are moves for us to take if (!movement_stack.empty()) { bool valid = false; // If the next move is valid, collision - int x = movement_stack.top(); + int x = movement_stack.front(); switch (x) { @@ -59,13 +60,13 @@ bool Explorer::move() { std::cout << "Path blocked" << std::endl; // Flush the moves list while(!movement_stack.empty()) { - movement_stack.pop(); + movement_stack.pop_front(); } return false; } // If everything went well, pop and return true - movement_stack.pop(); + movement_stack.pop_front(); return true; } else @@ -74,6 +75,6 @@ bool Explorer::move() { // A* bool Explorer::plan(sf::Vector2i destination_) { - + movement_stack = pather->pathTo(position, destination_); return true; } diff --git a/aStar/Explorer.h b/aStar/Explorer.h index 3af0a1b..f865e5d 100644 --- a/aStar/Explorer.h +++ b/aStar/Explorer.h @@ -2,6 +2,7 @@ #include #include #include "Map.h" +#include "Pather.h" class Explorer { public: @@ -13,8 +14,9 @@ private: sf::Color color; sf::Vector2i position; Map* map; + Pather* pather; - std::stack movement_stack; + std::deque movement_stack; bool move(); bool plan(sf::Vector2i destination_); }; diff --git a/aStar/Pather.cpp b/aStar/Pather.cpp index dab7527..e0854e3 100644 --- a/aStar/Pather.cpp +++ b/aStar/Pather.cpp @@ -100,6 +100,10 @@ Pather::Pather(Map* map_) { map = map_; } +Pather::Pather() { + +} + Pather::~Pather() { } @@ -107,7 +111,7 @@ sf::Vector2i Pather::getEndNodePosition() { return end_node->xy; } -std::vector Pather::pathTo(sf::Vector2i start, sf::Vector2i end) { +std::deque Pather::pathTo(sf::Vector2i start, sf::Vector2i end) { // Clear the visited map for erroneous data for (int i = 0; i < Map::CELLS_WIDTH; i++) { @@ -129,13 +133,13 @@ std::vector Pather::pathTo(sf::Vector2i start, sf::Vector2i end) { openList.emplace(start_node, 0); early_exit = false; - //path_list = Loop(); + path_list = loop(); return path_list; } -std::vector Pather::loop() { +std::deque Pather::loop() { while (!openList.empty() && !early_exit) { // Early exit jankyness, need to change this if (closedList.size() > 3000) { @@ -172,17 +176,17 @@ std::vector Pather::loop() { } } - std::vector return_path = returnPath(); + std::deque return_path = returnPath(); if (no_path || return_path.empty()) { - return std::vector(0, 0); std::cout << " no return path " << std::endl; + return return_path; } return return_path; } -std::vector Pather::returnPath() { - std::vector path; +std::deque Pather::returnPath() { + std::deque path; while (active_node != nullptr) { path.push_back(active_node->cameFrom); diff --git a/aStar/Pather.h b/aStar/Pather.h index 37913dc..af49b39 100644 --- a/aStar/Pather.h +++ b/aStar/Pather.h @@ -29,6 +29,7 @@ private: class Pather { public: Pather(Map* map_); + Pather(); ~Pather(); Map* map; @@ -37,9 +38,9 @@ public: std::unordered_map closedList; int visitedMap[App::WINDOW_HEIGHT][App::WINDOW_WIDTH]; - std::vector pathTo(sf::Vector2i start, sf::Vector2i end); - std::vector loop(); - std::vector returnPath(); + std::deque pathTo(sf::Vector2i start, sf::Vector2i end); + std::deque loop(); + std::deque returnPath(); sf::Vector2i getEndNodePosition(); @@ -51,7 +52,7 @@ public: private: - std::vector path_list; + std::deque path_list; node* end_node; }; diff --git a/aStar/main.cpp b/aStar/main.cpp index c30d317..eaa9c5f 100644 --- a/aStar/main.cpp +++ b/aStar/main.cpp @@ -1,6 +1,7 @@ #include #include #include "App.h" +#include "Pather.h" int main() {