Rules are not template anymore (it was dumb)

Entities are now managed with unique_ptr to allow polymorphism (and good memory management)
Rules can be added via the Level object
Fixed Rule::findTarget not caring about targetType

TODO : Find why pathfinding (unlikely) or findTarget (most likely) is broken
This commit is contained in:
trotFunky 2019-06-10 04:04:03 +02:00
parent 4ab83f1124
commit c94fc5a0be
7 changed files with 221 additions and 193 deletions

View file

@ -14,24 +14,34 @@ Level::Level(const pugi::xml_document& xmlDoc, const pro_maat::TextureStore& tex
{
if(!strncmp(child.name(),"Entity",6))
{
entities.emplace_back(child,textures.at(child.attribute("textureId").as_int(0)).get());
entities.emplace_back(std::make_unique<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()->getOccupiedSquares();
// FIXME : For testing purposes
Rule<State::Moving,EntityType::House> newRule(*entities.rbegin(),entities,occupiedSquares,size);
std::vector<pro_maat::GridPos> entitySquares = entities.rbegin()->get()->getOccupiedSquares();
std::move(entitySquares.begin(),entitySquares.end(),std::back_inserter(occupiedSquares));
}
}
// FIXME : For testing purposes
addRule(EntityType::Significant,State::Moving,EntityType::Citizen);
}
void Level::addRule(EntityType affectedEntities, const State targetState, EntityType targetEntities)
{
for(auto& entity : entities)
{
if(entity->getType() == affectedEntities)
{
entity = std::make_unique<Rule>(entity.release(),targetState,targetEntities,entities,occupiedSquares,size);
}
}
}
void Level::render(sf::RenderWindow& renderWindow) const
{
for(const Entity& entity : entities)
for(const auto& entity : entities)
{
renderWindow.draw(entity.getShape());
renderWindow.draw(entity->getShape());
}
}
@ -40,11 +50,11 @@ void Level::runStep()
std::vector<pro_maat::GridPos> newOccupiedSquares{};
newOccupiedSquares.reserve(occupiedSquares.size());
for(Entity& entity: entities)
for(auto& entity: entities)
{
entity.update();
entity->update();
int heuristicSign = 0;
switch (entity.getState())
switch (entity->getState())
{
case State::Moving:
{
@ -57,9 +67,9 @@ void Level::runStep()
{
heuristicSign = -1;
}
if(entity.getTarget() != entity.getPosition())
if(entity->getTarget() != entity->getPosition())
{
entity.move(findPath(entity.getPosition(),entity.getTarget(),heuristicSign));
entity->move(findPath(entity->getPosition(),entity->getTarget(),heuristicSign));
}
break;
}
@ -67,7 +77,7 @@ void Level::runStep()
case State::Idle:break;
}
std::vector<pro_maat::GridPos> entitySquares = entity.getOccupiedSquares();
std::vector<pro_maat::GridPos> entitySquares = entity->getOccupiedSquares();
// FIXME : Very heavy memory usage and a lot of duplicates, slows down occupiedSquares.find() calls too.
// Copy the squares to the currently occupied squares : prevents moving into an entity that just moved