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"
|
2012-09-09 20:50:15 +00:00
|
|
|
|
2013-08-07 15:39:43 +00:00
|
|
|
Sprite::Sprite(const Vector2f& position, Category category,
|
|
|
|
unsigned short mask, const Vector2f& size,
|
|
|
|
const std::string& texture, const Vector2f& direction) :
|
2013-05-07 22:00:05 +00:00
|
|
|
mCategory(category),
|
2013-07-14 19:56:44 +00:00
|
|
|
mMask(mask) {
|
2013-05-07 22:00:05 +00:00
|
|
|
mShape.setSize(size);
|
|
|
|
mShape.setOrigin(size / 2.0f);
|
2013-08-07 15:39:43 +00:00
|
|
|
mShape.setTextureRect(sf::IntRect(Vector2i(), Vector2i(size)));
|
2013-05-07 22:00:05 +00:00
|
|
|
setPosition(position);
|
|
|
|
setDirection(direction);
|
2013-08-20 19:44:11 +00:00
|
|
|
setTexture(texture);
|
2012-12-23 14:50:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the position of the sprite (center).
|
2012-09-09 20:50:15 +00:00
|
|
|
*/
|
2013-08-07 15:39:43 +00:00
|
|
|
Vector2f
|
2012-12-23 14:50:49 +00:00
|
|
|
Sprite::getPosition() const {
|
2013-05-07 22:00:05 +00:00
|
|
|
return mShape.getPosition();
|
2012-12-23 14:50:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the movement speed of the sprite.
|
|
|
|
*/
|
2013-08-07 15:39:43 +00:00
|
|
|
Vector2f
|
2012-12-23 14:50:49 +00:00
|
|
|
Sprite::getSpeed() const {
|
|
|
|
return mSpeed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the angle of the sprite.
|
|
|
|
*/
|
2013-08-07 15:39:43 +00:00
|
|
|
Vector2f
|
2013-03-30 01:30:11 +00:00
|
|
|
Sprite::getDirection() const {
|
2013-08-07 15:39:43 +00:00
|
|
|
return thor::rotatedVector(Vector2f(0, - 1), mShape.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.
|
|
|
|
*/
|
2013-08-07 15:39:43 +00:00
|
|
|
Vector2f
|
2012-12-23 14:50:49 +00:00
|
|
|
Sprite::getSize() const {
|
2013-05-07 22:00:05 +00:00
|
|
|
sf::FloatRect bounds = mShape.getLocalBounds();
|
2013-08-07 15:39:43 +00:00
|
|
|
return Vector2f(bounds.width, bounds.height);
|
2012-12-23 14:50:49 +00:00
|
|
|
}
|
|
|
|
|
2012-09-09 20:50:15 +00:00
|
|
|
void
|
|
|
|
Sprite::draw(sf::RenderTarget& target, sf::RenderStates states) const {
|
2013-05-07 22:00:05 +00:00
|
|
|
target.draw(mShape, states);
|
2013-03-03 20:27:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
}
|
|
|
|
|
2013-04-27 19:29:05 +00:00
|
|
|
bool
|
|
|
|
Sprite::isInside(const sf::FloatRect& rect) const {
|
2013-05-07 22:00:05 +00:00
|
|
|
return rect.intersects(mShape.getGlobalBounds());
|
2013-04-27 19:29:05 +00:00
|
|
|
}
|
2013-07-14 19:56:44 +00:00
|
|
|
|
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
|
2013-08-07 15:39:43 +00:00
|
|
|
Sprite::setSpeed(Vector2f direction, float speed) {
|
|
|
|
if (direction != Vector2f())
|
2012-12-23 14:50:49 +00:00
|
|
|
thor::setLength(direction, speed);
|
|
|
|
mSpeed = direction;
|
|
|
|
}
|
|
|
|
|
2013-06-21 16:00:38 +00:00
|
|
|
/**
|
|
|
|
* Rotates sprite in the direction of the vector. Vector length must not be null,
|
|
|
|
* but is otherwise meaningless.
|
|
|
|
*/
|
2012-12-23 14:50:49 +00:00
|
|
|
void
|
2013-08-07 15:39:43 +00:00
|
|
|
Sprite::setDirection(const Vector2f& direction) {
|
|
|
|
if (direction != Vector2f())
|
2013-05-07 22:00:05 +00:00
|
|
|
mShape.setRotation(thor::polarAngle(direction) + 90);
|
2012-12-23 14:50:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the position of thr Sprite.
|
|
|
|
*/
|
|
|
|
void
|
2013-08-07 15:39:43 +00:00
|
|
|
Sprite::setPosition(const Vector2f& position) {
|
2013-05-07 22:00:05 +00:00
|
|
|
mShape.setPosition(position);
|
2013-03-19 19:51:56 +00:00
|
|
|
}
|
2013-08-20 19:44:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a new texture. The old one is discarded through smart pointers if
|
|
|
|
* it isn't used any more.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Sprite::setTexture(const std::string& texture) {
|
|
|
|
try {
|
|
|
|
mTexture = ResourceManager::i().acquire(Loader::i()
|
|
|
|
.fromFile<sf::Texture>(texture));
|
|
|
|
mShape.setTexture(&*mTexture, false);
|
|
|
|
}
|
|
|
|
catch (thor::ResourceLoadingException&) {
|
|
|
|
LOG_W("Failed to load texture " << texture << ", coloring red.");
|
|
|
|
mShape.setFillColor(sf::Color(255, 0, 0));
|
|
|
|
}
|
|
|
|
}
|