2019-06-06 17:07:24 +02:00
|
|
|
//
|
|
|
|
// Created by trotfunky on 06/06/19.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "Level.h"
|
2019-06-07 02:59:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
Level::Level(const pugi::xml_document& xmlDoc, const TextureStore& textureStore)
|
|
|
|
: textures(textureStore),
|
|
|
|
size(xmlDoc.child("Level").attribute("width").as_int(),xmlDoc.child("Level").attribute("width").as_int())
|
|
|
|
{
|
|
|
|
pugi::xml_node levelNode = xmlDoc.child("Level");
|
|
|
|
for(const pugi::xml_node& child : levelNode.children())
|
|
|
|
{
|
|
|
|
if(!strncmp(child.name(),"Entity",6))
|
|
|
|
{
|
2019-06-07 22:20:34 +02:00
|
|
|
entities.emplace_back(child,textures.at(child.attribute("textureId").as_int(0)).get());
|
2019-06-08 02:32:06 +02:00
|
|
|
|
|
|
|
// Initialize the occupied squares vector with the new entity's squares
|
|
|
|
std::vector<std::pair<uint8_t,uint8_t>> entitySquares = entities.rbegin()->getOccupiedSquares();
|
|
|
|
std::move(entitySquares.begin(),entitySquares.end(),std::back_inserter(occupiedSquares));
|
2019-06-07 02:59:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Level::render(sf::RenderWindow& renderWindow) const
|
|
|
|
{
|
2019-06-07 22:20:34 +02:00
|
|
|
for(const Entity& entity : entities)
|
|
|
|
{
|
|
|
|
renderWindow.draw(entity.getShape());
|
|
|
|
}
|
2019-06-07 02:59:27 +02:00
|
|
|
}
|
|
|
|
|
2019-06-07 22:20:34 +02:00
|
|
|
void Level::runStep()
|
2019-06-07 02:59:27 +02:00
|
|
|
{
|
2019-06-08 02:32:06 +02:00
|
|
|
std::vector<std::pair<uint8_t,uint8_t>> newOccupiedSquares{};
|
|
|
|
newOccupiedSquares.reserve(occupiedSquares.size());
|
|
|
|
|
2019-06-07 22:20:34 +02:00
|
|
|
for(Entity& entity: entities)
|
|
|
|
{
|
2019-06-08 02:32:06 +02:00
|
|
|
entity.update();
|
|
|
|
int heuristicSign = 0;
|
|
|
|
switch (entity.getState())
|
|
|
|
{
|
|
|
|
case State::Moving:
|
|
|
|
{
|
|
|
|
heuristicSign = 1;
|
|
|
|
}
|
|
|
|
case State::Fleeing:
|
|
|
|
{
|
|
|
|
if(heuristicSign == 0)
|
|
|
|
{
|
|
|
|
heuristicSign = -1;
|
|
|
|
}
|
|
|
|
if(entity.target != entity.getPosition())
|
|
|
|
{
|
|
|
|
entity.move(findPath(entity.getPosition(),entity.target,heuristicSign));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case State::Waiting:break;
|
|
|
|
case State::Idle:break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Moves the occupied squares from the entity to the new occupied squares vector
|
|
|
|
std::vector<std::pair<uint8_t,uint8_t>> entitySquares = entity.getOccupiedSquares();
|
|
|
|
std::move(entitySquares.begin(),entitySquares.end(),std::back_inserter(newOccupiedSquares));
|
2019-06-07 22:20:34 +02:00
|
|
|
}
|
2019-06-08 02:32:06 +02:00
|
|
|
|
|
|
|
occupiedSquares.swap(newOccupiedSquares);
|
|
|
|
// Sort the vector as to get O(ln(n)) complexity when searching for a square
|
|
|
|
std::sort(occupiedSquares.begin(),occupiedSquares.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
Orientation Level::findPath(sf::Vector2i start, sf::Vector2i end, int sign)
|
|
|
|
{
|
|
|
|
// TODO : A* which returns the next move
|
|
|
|
return Orientation::East;
|
2019-06-07 02:59:27 +02:00
|
|
|
}
|