Fixed movement of multiple entities.

Changed std::unique_ptr<Entity> to std::shared_ptr<Entity>.
Fixed pathfinding away from targets

TODO :
 - Fix oscillation around destination
 - Fix fleeing from adjacent target
 - Win conditions
 - GUI
This commit is contained in:
trotFunky 2019-06-10 18:44:45 +02:00
parent 293a564a29
commit 5ccd7d0c13
6 changed files with 33 additions and 21 deletions

View file

@ -5,10 +5,10 @@
#include "Rule.h"
Rule::Rule(Entity* entity, State targetState, EntityType targetType,
std::vector<std::unique_ptr<Entity>>& entities,
Rule::Rule(std::shared_ptr<Entity> entity, State targetState, EntityType targetType,
const std::vector<std::shared_ptr<Entity>>& entities,
const std::vector<pro_maat::GridPos>& occupiedSquares, const pro_maat::GridPos& mapSize)
: entity(entity),
: entity(std::move(entity)),
targetState(targetState),
targetType(targetType),
entities(entities),
@ -47,12 +47,11 @@ void Rule::update()
pro_maat::GridPos Rule::findTarget()
{
// TODO : Sorting in place, consider using shared_ptr ?
// std::vector<Entity> sortedEntities{};
// sortedEntities.insert(sortedEntities.end(),entities.begin(),entities.end());
std::vector<std::shared_ptr<Entity>> sortedEntities{};
sortedEntities.insert(sortedEntities.end(),entities.begin(),entities.end());
// Compares entities via their distance to the current entity
auto distanceSortEntities = [this](const std::unique_ptr<Entity>& leftHandSide, const std::unique_ptr<Entity>& rightHandSide){
auto distanceSortEntities = [this](const std::shared_ptr<Entity>& leftHandSide, const std::shared_ptr<Entity>& rightHandSide){
return (pro_maat::manhattanDistance(entity->getPosition(),leftHandSide->getPosition()) <
pro_maat::manhattanDistance(entity->getPosition(),rightHandSide->getPosition()));
};
@ -90,9 +89,9 @@ pro_maat::GridPos Rule::findTarget()
// Sort in order to minimize entities to process
std::sort(entities.begin(),entities.end(),distanceSortEntities);
std::sort(sortedEntities.begin(),sortedEntities.end(),distanceSortEntities);
for(const auto& processingEntity : entities)
for(const auto& processingEntity : sortedEntities)
{
if(processingEntity->getType() != targetType) continue;
@ -128,7 +127,6 @@ pro_maat::GridPos Rule::findTarget()
if(target != potentialTargets.end())
{
std::cout << "Target : (" << (*target).first << ","<< (*target).second << ")" << std::endl;
return (*target);
}
}