2012-09-09 20:50:15 +00:00
|
|
|
/*
|
|
|
|
* Sprite.cpp
|
|
|
|
*
|
2012-12-23 14:50:49 +00:00
|
|
|
* Created on: 11.08.2012
|
2012-09-09 20:50:15 +00:00
|
|
|
* Author: Felix
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Sprite.h"
|
|
|
|
|
2012-12-23 14:50:49 +00:00
|
|
|
#include <Thor/Vectors.hpp>
|
|
|
|
|
2012-09-09 20:50:15 +00:00
|
|
|
#include "../util/Loader.h"
|
2012-12-20 13:59:05 +00:00
|
|
|
#include "../util/Log.h"
|
2012-12-23 14:50:49 +00:00
|
|
|
#include "../util/ResourceManager.h"
|
2013-03-29 16:59:35 +00:00
|
|
|
#include "../util/Yaml.h"
|
2012-09-09 20:50:15 +00:00
|
|
|
|
2012-12-23 14:50:49 +00:00
|
|
|
const std::string Sprite::KEY_SIZE = "size";
|
|
|
|
const std::string Sprite::KEY_RADIUS = "radius";
|
2012-12-22 14:10:26 +00:00
|
|
|
const std::string Sprite::KEY_TEXTURE = "texture";
|
2012-10-12 18:59:33 +00:00
|
|
|
|
2012-09-09 20:50:15 +00:00
|
|
|
/**
|
2012-12-23 14:50:49 +00:00
|
|
|
* Initializes sprite data.
|
2012-09-09 20:50:15 +00:00
|
|
|
*
|
2012-12-23 14:50:49 +00:00
|
|
|
* @param data Container holding construction parameters.
|
|
|
|
* @param config Additional construction parameters
|
2012-09-09 20:50:15 +00:00
|
|
|
*/
|
2012-12-23 14:50:49 +00:00
|
|
|
Sprite::Sprite(const Data& data, const Yaml& config) :
|
|
|
|
mCategory(data.category),
|
|
|
|
mMask(data.mask),
|
|
|
|
mDelete(false) {
|
|
|
|
std::string texture = config.get<std::string>(KEY_TEXTURE, "");
|
2012-12-20 13:59:05 +00:00
|
|
|
if (texture == "") {
|
|
|
|
LOG_E("Failed to read texture from YAML file " << config.getFilename());
|
|
|
|
}
|
|
|
|
mTexture = ResourceManager::i().acquire(Loader::i()
|
2012-12-23 14:50:49 +00:00
|
|
|
.fromFile<sf::Texture>(texture));
|
|
|
|
|
|
|
|
float radius = config.get(KEY_RADIUS, 0.0f);
|
|
|
|
sf::Vector2f size = config.get(KEY_SIZE, sf::Vector2f());
|
|
|
|
if (radius != 0.0f) {
|
2013-03-03 20:27:40 +00:00
|
|
|
mShape.type = Shape::Type::CIRCLE;
|
|
|
|
mShape.shape = std::unique_ptr<sf::Shape>(new sf::CircleShape(radius));
|
|
|
|
mShape.shape->setOrigin(radius, radius);
|
|
|
|
mShape.shape->setTextureRect(sf::IntRect(sf::Vector2i(0, 0), sf::Vector2i(radius * 2, radius * 2)));
|
|
|
|
|
2012-12-23 14:50:49 +00:00
|
|
|
}
|
|
|
|
else if (size != sf::Vector2f()) {
|
2013-03-03 20:27:40 +00:00
|
|
|
mShape.type = Shape::Type::RECTANGLE;
|
|
|
|
mShape.shape = std::unique_ptr<sf::Shape>(new sf::RectangleShape(size));
|
|
|
|
mShape.shape->setOrigin(size / 2.0f);
|
|
|
|
mShape.shape->setTextureRect(sf::IntRect(sf::Vector2i(0, 0), sf::Vector2i(size)));
|
2012-12-23 14:50:49 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
LOG_E("Could not read size or radius from " << config.getFilename());
|
|
|
|
}
|
|
|
|
|
2013-03-03 20:27:40 +00:00
|
|
|
mShape.shape->setTexture(&*mTexture, false);
|
2012-12-23 14:50:49 +00:00
|
|
|
|
|
|
|
setPosition(data.position);
|
2013-03-30 01:30:11 +00:00
|
|
|
setDirection(data.direction);
|
2012-09-09 20:50:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-12-22 00:14:30 +00:00
|
|
|
* Used to make this class pure virtual without any pure virtual function.
|
2012-09-09 20:50:15 +00:00
|
|
|
*/
|
|
|
|
Sprite::~Sprite() {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-12-23 14:50:49 +00:00
|
|
|
* Initializes container.
|
|
|
|
*/
|
2013-03-30 01:30:11 +00:00
|
|
|
Sprite::Data::Data(const sf::Vector2f& position, const sf::Vector2f& direction,
|
2013-03-03 20:27:40 +00:00
|
|
|
Category category, unsigned short mask) :
|
2012-12-23 14:50:49 +00:00
|
|
|
position(position),
|
2013-03-30 01:30:11 +00:00
|
|
|
direction(direction),
|
2012-12-23 14:50:49 +00:00
|
|
|
category(category),
|
2013-03-03 20:27:40 +00:00
|
|
|
mask(mask) {
|
2012-12-23 14:50:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the position of the sprite (center).
|
2012-09-09 20:50:15 +00:00
|
|
|
*/
|
2012-12-23 14:50:49 +00:00
|
|
|
sf::Vector2f
|
|
|
|
Sprite::getPosition() const {
|
2013-03-03 20:27:40 +00:00
|
|
|
return mShape.shape->getPosition();
|
2012-12-23 14:50:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the movement speed of the sprite.
|
|
|
|
*/
|
|
|
|
sf::Vector2f
|
|
|
|
Sprite::getSpeed() const {
|
|
|
|
return mSpeed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the angle of the sprite.
|
|
|
|
*/
|
2013-03-30 01:30:11 +00:00
|
|
|
sf::Vector2f
|
|
|
|
Sprite::getDirection() const {
|
|
|
|
return thor::rotatedVector(sf::Vector2f(1, 0), mShape.shape->getRotation());
|
2012-12-23 14:50:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if this object should be deleted.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
Sprite::getDelete() const {
|
|
|
|
return mDelete;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the Category of this object.
|
|
|
|
*/
|
|
|
|
Sprite::Category
|
|
|
|
Sprite::getCategory() const {
|
|
|
|
return mCategory;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the size of the sprite as a vector (bottom left to top right),
|
|
|
|
* does not consider rotation.
|
|
|
|
*/
|
|
|
|
sf::Vector2f
|
|
|
|
Sprite::getSize() const {
|
2013-03-03 20:27:40 +00:00
|
|
|
sf::FloatRect bounds = mShape.shape->getLocalBounds();
|
2012-12-23 14:50:49 +00:00
|
|
|
return sf::Vector2f(bounds.width, bounds.height);
|
|
|
|
}
|
|
|
|
|
2012-09-09 20:50:15 +00:00
|
|
|
void
|
|
|
|
Sprite::draw(sf::RenderTarget& target, sf::RenderStates states) const {
|
2013-03-03 20:27:40 +00:00
|
|
|
target.draw(*mShape.shape, states);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if collisions with that category are enabled through mask.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
Sprite::collisionEnabled(Category category) const {
|
|
|
|
return (category & mMask) != 0;
|
2012-12-23 14:50:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when a collision with another Sprite occured. Override this method
|
|
|
|
* to manage collision events.
|
|
|
|
*
|
|
|
|
* @param other The other Sprite in the collision.
|
|
|
|
*/
|
|
|
|
void
|
2013-03-03 20:27:40 +00:00
|
|
|
Sprite::onCollide(std::shared_ptr<Sprite> other) {
|
2012-12-23 14:50:49 +00:00
|
|
|
}
|
2012-09-09 20:50:15 +00:00
|
|
|
|
2012-12-23 14:50:49 +00:00
|
|
|
/**
|
|
|
|
* Set to true to mark this object for deletion from the world.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Sprite::setDelete(bool value) {
|
|
|
|
mDelete = value;
|
|
|
|
}
|
2012-09-09 20:50:15 +00:00
|
|
|
|
2012-12-23 14:50:49 +00:00
|
|
|
/**
|
|
|
|
* Sets movement speed and direction of the Sprite. Set either value to zero to stop movement.
|
|
|
|
*
|
|
|
|
* @param direction The direction the Sprite moves in, does not have to be normalized.
|
|
|
|
* @param speed Movement speed in pixels per second.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Sprite::setSpeed(sf::Vector2f direction, float speed) {
|
|
|
|
if (direction != sf::Vector2f()) {
|
|
|
|
thor::setLength(direction, speed);
|
|
|
|
}
|
|
|
|
mSpeed = direction;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-03-30 01:30:11 +00:00
|
|
|
Sprite::setDirection(const sf::Vector2f& direction) {
|
|
|
|
if (direction != sf::Vector2f()) {
|
|
|
|
mShape.shape->setRotation(thor::polarAngle(direction) + 90);
|
|
|
|
}
|
2012-12-23 14:50:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the position of thr Sprite.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Sprite::setPosition(const sf::Vector2f& position) {
|
2013-03-03 20:27:40 +00:00
|
|
|
mShape.shape->setPosition(position);
|
2012-09-09 20:50:15 +00:00
|
|
|
}
|
2013-03-19 19:51:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the radius of this sprite. Will fail if this is not a circle.
|
|
|
|
*
|
|
|
|
* @return The radius of this sprite.
|
|
|
|
*/
|
|
|
|
float
|
|
|
|
Sprite::getRadius() const {
|
|
|
|
std::shared_ptr<sf::CircleShape> circleShape =
|
|
|
|
std::dynamic_pointer_cast<sf::CircleShape>(mShape.shape);
|
|
|
|
assert(circleShape);
|
|
|
|
return circleShape->getRadius();
|
|
|
|
}
|