// // Created by trotfunky on 06/06/19. // #include "Entity.h" const std::map Entity::entityTypeLookup = { {"Citizen",EntityType::Citizen}, {"Noble",EntityType::Noble}, {"House",EntityType::House}, {"Car",EntityType::Car}}; const std::map Entity::stateLookup = { {"Seek",State::Moving}, {"Flee",State::Fleeing}}; Entity::Entity(pro_maat::GridUnit x, pro_maat::GridUnit y, EntityType type, sf::Texture* texture, int width, int height) : type(type) { shape = sf::RectangleShape(sf::Vector2f(width*pro_maat::pixelsPerUnit,height*pro_maat::pixelsPerUnit)); // Sets the origin at the center of the entity shape.setOrigin(shape.getSize()/2.0f); // Adjust position for offset origin shape.setPosition((x+0.5*width)*pro_maat::pixelsPerUnit,(y+0.5*width)*pro_maat::pixelsPerUnit); shape.setTexture(texture); currentState = State::Idle; nextState = State::Idle; target = pro_maat::GridPos(x,y); nextTarget = target; } Entity::Entity(const pugi::xml_node& entityNode, sf::Texture* texture) : Entity(entityNode.attribute("x").as_int(), entityNode.attribute("y").as_int(), entityTypeLookup.at(entityNode.attribute("type").as_string()), texture, entityNode.attribute("w").as_int(1), entityNode.attribute("h").as_int(1)) {} Entity::Entity() : Entity(0,0,EntityType::Citizen,nullptr,0,0) {} void Entity::move(Orientation orientation) { // TODO : Add speed ? sf::Vector2f movementVector(0,0); switch (orientation) { case Orientation::North: movementVector.y = -pro_maat::pixelsPerUnit; break; case Orientation::East: movementVector.x = pro_maat::pixelsPerUnit; break; case Orientation::South: movementVector.y = pro_maat::pixelsPerUnit; break; case Orientation::West: movementVector.x = -pro_maat::pixelsPerUnit; break; case Orientation::None: return; } shape.setRotation(static_cast(orientation)); shape.setPosition(shape.getPosition()+movementVector); } void Entity::update() { currentState = nextState; target = nextTarget; } const sf::RectangleShape& Entity::getShape() const { return(shape); } State Entity::getState() const { return currentState; } EntityType Entity::getType() const { return type; } pro_maat::GridPos Entity::getTarget() const { return target; } const pro_maat::GridPos Entity::getPosition() const { // Safe : size is a multiple of pro_maat::pixelsPerUnit pro_maat::GridUnit x = shape.getPosition().x/pro_maat::pixelsPerUnit; pro_maat::GridUnit y = shape.getPosition().y/pro_maat::pixelsPerUnit; return pro_maat::GridPos(x,y); } const std::vector Entity::getOccupiedSquares() const { std::vector occupiedSquares; // Safe : size is a multiple of pro_maat::pixelsPerUnit pro_maat::GridUnit w = shape.getSize().x/pro_maat::pixelsPerUnit; pro_maat::GridUnit h = shape.getSize().y/pro_maat::pixelsPerUnit; pro_maat::GridUnit x = shape.getPosition().x/pro_maat::pixelsPerUnit - 0.5*w; pro_maat::GridUnit y = shape.getPosition().y/pro_maat::pixelsPerUnit - 0.5*h; occupiedSquares.reserve(w*h); for(int i = 0;i