Project_Maat/src/Entity.cpp

100 lines
3 KiB
C++

//
// Created by trotfunky on 06/06/19.
//
#include "Entity.h"
const std::map<std::string,EntityType> Entity::entityTypeLookup = {
{"Citizen",EntityType::Citizen},
{"Significant",EntityType::Significant},
{"House",EntityType::House},
{"Car",EntityType::Car}};
Entity::Entity(int x, int 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 = sf::Vector2i(shape.getPosition());
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)) {}
void Entity::move(Orientation orientation)
{
// TODO : Add speed ?
shape.setRotation(static_cast<float>(orientation));
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;
}
shape.setPosition(shape.getPosition()+movementVector);
}
void Entity::update()
{}
const sf::RectangleShape& Entity::getShape() const
{
return(shape);
}
const State Entity::getState() const
{
return currentState;
}
const sf::Vector2i Entity::getPosition() const
{
// Safe : size is a multiple of pro_maat::pixelsPerUnit
uint8_t x = (shape.getPosition().x-0.5*shape.getSize().x)/pro_maat::pixelsPerUnit;
uint8_t y = (shape.getPosition().y-0.5*shape.getSize().y)/pro_maat::pixelsPerUnit;
return sf::Vector2i(x,y);
}
const std::vector<std::pair<uint8_t, uint8_t>> Entity::getOccupiedSquares() const
{
std::vector<std::pair<uint8_t, uint8_t>> occupiedSquares;
// Safe : size is a multiple of pro_maat::pixelsPerUnit
uint8_t w = shape.getSize().x/pro_maat::pixelsPerUnit;
uint8_t h = shape.getSize().y/pro_maat::pixelsPerUnit;
uint8_t x = shape.getPosition().x/pro_maat::pixelsPerUnit - 0.5*w;
uint8_t y = shape.getPosition().y/pro_maat::pixelsPerUnit - 0.5*h;
for(int i = 0;i<w;i++)
{
for(int j = 0;j<h;j++)
{
occupiedSquares.emplace_back(x+i,y+j);
}
}
return std::move(occupiedSquares);
}