Added type alias for std::pair<uint8_t,uint8_t> for grid coordinates as sf::Vector2 cannot be used in std::map or std::set

This commit is contained in:
Teo-CD 2019-06-08 16:11:29 +02:00
parent 1a79679c21
commit f7481f2bc9
5 changed files with 24 additions and 12 deletions

View file

@ -71,17 +71,17 @@ const State Entity::getState() const
return currentState; return currentState;
} }
const sf::Vector2i Entity::getPosition() const const pro_maat::GridPos Entity::getPosition() const
{ {
// Safe : size is a multiple of pro_maat::pixelsPerUnit // Safe : size is a multiple of pro_maat::pixelsPerUnit
uint8_t x = (shape.getPosition().x-0.5*shape.getSize().x)/pro_maat::pixelsPerUnit; uint8_t x = (shape.getPosition().x-0.5*shape.getSize().x)/pro_maat::pixelsPerUnit;
uint8_t y = (shape.getPosition().y-0.5*shape.getSize().y)/pro_maat::pixelsPerUnit; uint8_t y = (shape.getPosition().y-0.5*shape.getSize().y)/pro_maat::pixelsPerUnit;
return sf::Vector2i(x,y); return pro_maat::GridPos(x,y);
} }
const std::vector<std::pair<uint8_t, uint8_t>> Entity::getOccupiedSquares() const const std::vector<pro_maat::GridPos> Entity::getOccupiedSquares() const
{ {
std::vector<std::pair<uint8_t, uint8_t>> occupiedSquares; std::vector<pro_maat::GridPos> occupiedSquares;
// Safe : size is a multiple of pro_maat::pixelsPerUnit // Safe : size is a multiple of pro_maat::pixelsPerUnit
uint8_t w = shape.getSize().x/pro_maat::pixelsPerUnit; uint8_t w = shape.getSize().x/pro_maat::pixelsPerUnit;
uint8_t h = shape.getSize().y/pro_maat::pixelsPerUnit; uint8_t h = shape.getSize().y/pro_maat::pixelsPerUnit;

View file

@ -49,13 +49,13 @@ public:
const sf::RectangleShape& getShape() const; const sf::RectangleShape& getShape() const;
const State getState() const; const State getState() const;
const sf::Vector2i getPosition() const; const pro_maat::GridPos getPosition() const;
// Don't like it : iterates over every square every tick // Don't like it : iterates over every square every tick
const std::vector<std::pair<uint8_t, uint8_t>> getOccupiedSquares() const; const std::vector<pro_maat::GridPos> getOccupiedSquares() const;
/// Position of the target of the current action on the map /// Position of the target of the current action on the map
sf::Vector2i target; pro_maat::GridPos target;
private: private:
static const std::map<std::string,EntityType> entityTypeLookup; static const std::map<std::string,EntityType> entityTypeLookup;
@ -69,7 +69,7 @@ private:
State nextState; State nextState;
/// Used with rules : last to update has priority /// Used with rules : last to update has priority
sf::Vector2i nextTarget; pro_maat::GridPos nextTarget;
}; };

View file

@ -33,7 +33,7 @@ void Level::render(sf::RenderWindow& renderWindow) const
void Level::runStep() void Level::runStep()
{ {
std::vector<std::pair<uint8_t,uint8_t>> newOccupiedSquares{}; std::vector<pro_maat::GridPos> newOccupiedSquares{};
newOccupiedSquares.reserve(occupiedSquares.size()); newOccupiedSquares.reserve(occupiedSquares.size());
for(Entity& entity: entities) for(Entity& entity: entities)
@ -72,7 +72,7 @@ void Level::runStep()
std::sort(occupiedSquares.begin(),occupiedSquares.end()); std::sort(occupiedSquares.begin(),occupiedSquares.end());
} }
Orientation Level::findPath(sf::Vector2i start, sf::Vector2i end, int sign) Orientation Level::findPath(pro_maat::GridPos start, pro_maat::GridPos end, int sign)
{ {
// TODO : A* which returns the next move // TODO : A* which returns the next move
return Orientation::East; return Orientation::East;

View file

@ -9,6 +9,8 @@
#include <pugixml.hpp> #include <pugixml.hpp>
#include <vector> #include <vector>
#include <cstring> #include <cstring>
#include <set>
#include <map>
#include "Utils.h" #include "Utils.h"
#include "Entity.h" #include "Entity.h"
@ -33,9 +35,9 @@ private:
// Pathfinding // Pathfinding
// //
std::vector<std::pair<uint8_t,uint8_t>> occupiedSquares; std::vector<pro_maat::GridPos> occupiedSquares;
Orientation findPath(sf::Vector2i start, sf::Vector2i end, int sign); Orientation findPath(pro_maat::GridPos start, pro_maat::GridPos end, int sign);
}; };

View file

@ -12,6 +12,8 @@
namespace pro_maat namespace pro_maat
{ {
// Used with positions on the map grid
using GridPos = std::pair<uint8_t,uint8_t>;
static constexpr uint8_t pixelsPerUnit = 50; static constexpr uint8_t pixelsPerUnit = 50;
static constexpr char levelFolder[] = "resources/"; static constexpr char levelFolder[] = "resources/";
@ -20,6 +22,14 @@ static constexpr char fontFolder[] = "resources/";
void errorWindow(const std::string& error); void errorWindow(const std::string& error);
// Good heuristic on 4-way grids
double manhattanDistance(pro_maat::GridPos leftHandSide, pro_maat::GridPos rightHandSide)
{
// The *0.01 helps with breaking ties and minimizing exploration
// As per http://theory.stanford.edu/~amitp/GameProgramming/Heuristics.html#breaking-ties
return (std::abs(rightHandSide.first-leftHandSide.first)+std::abs(rightHandSide.second-leftHandSide.second))*1.01;
}
} }
#endif //PROJECT_MAAT_UTILS_H #endif //PROJECT_MAAT_UTILS_H