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:
parent
293a564a29
commit
5ccd7d0c13
6 changed files with 33 additions and 21 deletions
|
@ -14,7 +14,7 @@ Level::Level(const pugi::xml_document& xmlDoc, const pro_maat::TextureStore& tex
|
|||
{
|
||||
if(!strncmp(child.name(),"Entity",6))
|
||||
{
|
||||
entities.emplace_back(std::make_unique<Entity>(child,textures.at(child.attribute("textureId").as_int(0)).get()));
|
||||
entities.emplace_back(std::make_shared<Entity>(child,textures.at(child.attribute("textureId").as_int(0)).get()));
|
||||
|
||||
// Initialize the occupied squares vector with the new entity's squares
|
||||
std::vector<pro_maat::GridPos> entitySquares = entities.rbegin()->get()->getOccupiedSquares();
|
||||
|
@ -23,7 +23,8 @@ Level::Level(const pugi::xml_document& xmlDoc, const pro_maat::TextureStore& tex
|
|||
}
|
||||
|
||||
// FIXME : For testing purposes
|
||||
addRule(EntityType::Significant,State::Moving,EntityType::Citizen);
|
||||
addRule(EntityType::Significant,State::Fleeing,EntityType::Citizen);
|
||||
addRule(EntityType::Citizen,State::Moving,EntityType::Significant);
|
||||
}
|
||||
|
||||
void Level::addRule(EntityType affectedEntities, const State targetState, EntityType targetEntities)
|
||||
|
@ -32,7 +33,7 @@ void Level::addRule(EntityType affectedEntities, const State targetState, Entity
|
|||
{
|
||||
if(entity->getType() == affectedEntities)
|
||||
{
|
||||
entity = std::make_unique<Rule>(entity.release(),targetState,targetEntities,entities,occupiedSquares,size);
|
||||
entity = std::make_shared<Rule>(entity,targetState,targetEntities,entities,occupiedSquares,size);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -189,7 +190,7 @@ Orientation Level::findPath(pro_maat::GridPos start, pro_maat::GridPos goal, int
|
|||
}
|
||||
|
||||
pathCosts.insert_or_assign(neighbour,newPathCost);
|
||||
estimatedCosts.insert_or_assign(neighbour,newPathCost + pro_maat::manhattanDistance(neighbour,goal));
|
||||
estimatedCosts.insert_or_assign(neighbour,newPathCost + pro_maat::manhattanDistance(neighbour,goal)*sign);
|
||||
paths.insert_or_assign(neighbour,currentNode);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
#include <cstring>
|
||||
#include <set>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
|
||||
#include "Utils.h"
|
||||
#include "Entity.h"
|
||||
|
@ -34,7 +35,7 @@ public:
|
|||
private:
|
||||
const pro_maat::GridPos size;
|
||||
|
||||
std::vector<std::unique_ptr<Entity>> entities;
|
||||
std::vector<std::shared_ptr<Entity>> entities;
|
||||
|
||||
const pro_maat::TextureStore& textures;
|
||||
|
||||
|
|
18
src/Rule.cpp
18
src/Rule.cpp
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
10
src/Rule.h
10
src/Rule.h
|
@ -8,7 +8,7 @@
|
|||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <math.h>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
|
||||
#include "Entity.h"
|
||||
|
||||
|
@ -18,7 +18,8 @@ class Rule : public Entity
|
|||
{
|
||||
public:
|
||||
// The Rule object takes ownership of the Entity*
|
||||
Rule(Entity* entity, State targetState, EntityType targetType, std::vector<std::unique_ptr<Entity>>& entities,
|
||||
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);
|
||||
|
||||
/// Update according to the targetState and targetType
|
||||
|
@ -40,13 +41,12 @@ private:
|
|||
/// \return Suitable target square or current position if none was found.
|
||||
pro_maat::GridPos findTarget();
|
||||
|
||||
std::unique_ptr<Entity> entity;
|
||||
std::shared_ptr<Entity> entity;
|
||||
|
||||
State targetState;
|
||||
EntityType targetType;
|
||||
|
||||
// TOOD : dropped const-qualifier. Consider using shared_ptr ?
|
||||
std::vector<std::unique_ptr<Entity>>& entities;
|
||||
const std::vector<std::shared_ptr<Entity>>& entities;
|
||||
const std::vector<pro_maat::GridPos>& occupiedSquares;
|
||||
|
||||
const pro_maat::GridPos& mapSize;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue