2019-06-06 17:07:24 +02:00
|
|
|
//
|
|
|
|
// Created by trotfunky on 06/06/19.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "Entity.h"
|
2019-06-07 02:59:27 +02:00
|
|
|
|
|
|
|
const std::map<std::string,EntityType> Entity::entityTypeLookup = {
|
|
|
|
{"Citizen",EntityType::Citizen},
|
2019-06-11 00:33:44 +02:00
|
|
|
{"Noble",EntityType::Noble},
|
2019-06-07 02:59:27 +02:00
|
|
|
{"House",EntityType::House},
|
|
|
|
{"Car",EntityType::Car}};
|
|
|
|
|
2019-06-11 00:33:44 +02:00
|
|
|
const std::map<std::string,State> Entity::stateLookup = {
|
|
|
|
{"Seek",State::Moving},
|
|
|
|
{"Flee",State::Fleeing}};
|
|
|
|
|
2019-06-09 15:56:54 +02:00
|
|
|
Entity::Entity(pro_maat::GridUnit x, pro_maat::GridUnit y, EntityType type, sf::Texture* texture, int width, int height) : type(type)
|
2019-06-07 02:59:27 +02:00
|
|
|
{
|
2019-06-07 22:20:34 +02:00
|
|
|
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);
|
|
|
|
|
2019-06-09 05:37:08 +02:00
|
|
|
currentState = State::Idle;
|
2019-06-07 22:20:34 +02:00
|
|
|
nextState = State::Idle;
|
2019-06-09 05:37:08 +02:00
|
|
|
target = pro_maat::GridPos(x,y);
|
2019-06-07 22:20:34 +02:00
|
|
|
nextTarget = target;
|
2019-06-07 02:59:27 +02:00
|
|
|
}
|
|
|
|
|
2019-06-07 22:20:34 +02:00
|
|
|
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)) {}
|
2019-06-07 02:59:27 +02:00
|
|
|
|
2019-06-09 23:26:46 +02:00
|
|
|
Entity::Entity() : Entity(0,0,EntityType::Citizen,nullptr,0,0)
|
|
|
|
{}
|
|
|
|
|
2019-06-07 22:38:55 +02:00
|
|
|
void Entity::move(Orientation orientation)
|
2019-06-07 02:59:27 +02:00
|
|
|
{
|
2019-06-08 02:32:06 +02:00
|
|
|
// TODO : Add speed ?
|
2019-06-07 02:59:27 +02:00
|
|
|
|
2019-06-07 22:38:55 +02:00
|
|
|
sf::Vector2f movementVector(0,0);
|
|
|
|
switch (orientation)
|
|
|
|
{
|
2019-06-08 02:32:06 +02:00
|
|
|
case Orientation::North:
|
2019-06-07 22:38:55 +02:00
|
|
|
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;
|
2019-06-09 05:18:28 +02:00
|
|
|
case Orientation::None:
|
|
|
|
return;
|
2019-06-07 22:38:55 +02:00
|
|
|
}
|
2019-06-07 02:59:27 +02:00
|
|
|
|
2019-06-09 05:18:28 +02:00
|
|
|
shape.setRotation(static_cast<float>(orientation));
|
2019-06-07 22:38:55 +02:00
|
|
|
shape.setPosition(shape.getPosition()+movementVector);
|
2019-06-07 02:59:27 +02:00
|
|
|
}
|
|
|
|
|
2019-06-07 22:38:55 +02:00
|
|
|
void Entity::update()
|
2019-06-09 05:37:08 +02:00
|
|
|
{
|
|
|
|
currentState = nextState;
|
|
|
|
target = nextTarget;
|
|
|
|
}
|
2019-06-07 22:38:55 +02:00
|
|
|
|
2019-06-07 02:59:27 +02:00
|
|
|
const sf::RectangleShape& Entity::getShape() const
|
|
|
|
{
|
|
|
|
return(shape);
|
|
|
|
}
|
2019-06-08 02:32:06 +02:00
|
|
|
|
2019-06-09 14:59:17 +02:00
|
|
|
State Entity::getState() const
|
2019-06-08 02:32:06 +02:00
|
|
|
{
|
|
|
|
return currentState;
|
|
|
|
}
|
|
|
|
|
2019-06-10 04:04:03 +02:00
|
|
|
EntityType Entity::getType() const
|
|
|
|
{
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2019-06-10 01:58:14 +02:00
|
|
|
pro_maat::GridPos Entity::getTarget() const
|
|
|
|
{
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
2019-06-08 16:11:29 +02:00
|
|
|
const pro_maat::GridPos Entity::getPosition() const
|
2019-06-08 02:32:06 +02:00
|
|
|
{
|
|
|
|
// Safe : size is a multiple of pro_maat::pixelsPerUnit
|
2019-06-09 05:18:28 +02:00
|
|
|
pro_maat::GridUnit x = shape.getPosition().x/pro_maat::pixelsPerUnit;
|
|
|
|
pro_maat::GridUnit y = shape.getPosition().y/pro_maat::pixelsPerUnit;
|
2019-06-08 16:11:29 +02:00
|
|
|
return pro_maat::GridPos(x,y);
|
2019-06-08 02:32:06 +02:00
|
|
|
}
|
|
|
|
|
2019-06-08 16:11:29 +02:00
|
|
|
const std::vector<pro_maat::GridPos> Entity::getOccupiedSquares() const
|
2019-06-08 02:32:06 +02:00
|
|
|
{
|
2019-06-08 16:11:29 +02:00
|
|
|
std::vector<pro_maat::GridPos> occupiedSquares;
|
2019-06-09 05:18:28 +02:00
|
|
|
|
2019-06-08 02:32:06 +02:00
|
|
|
// Safe : size is a multiple of pro_maat::pixelsPerUnit
|
2019-06-09 05:18:28 +02:00
|
|
|
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);
|
2019-06-08 02:32:06 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|