2012-09-09 20:50:15 +00:00
|
|
|
/*
|
|
|
|
* Sprite.h
|
|
|
|
*
|
2012-12-23 14:50:49 +00:00
|
|
|
* Created on: 11.08.2012
|
2012-09-09 20:50:15 +00:00
|
|
|
* Author: Felix
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef DG_SPRITE_H_
|
|
|
|
#define DG_SPRITE_H_
|
|
|
|
|
2013-03-29 16:59:35 +00:00
|
|
|
#include <memory>
|
2012-09-09 20:50:15 +00:00
|
|
|
|
2013-03-29 16:59:35 +00:00
|
|
|
#include <SFML/Graphics.hpp>
|
2012-09-09 20:50:15 +00:00
|
|
|
|
|
|
|
/**
|
2012-12-23 14:50:49 +00:00
|
|
|
* An sprite that is rendered in the world.
|
2012-09-09 20:50:15 +00:00
|
|
|
*/
|
2012-12-23 14:50:49 +00:00
|
|
|
class Sprite : public sf::Drawable {
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Categories of objects for filtering.
|
|
|
|
* The order of categories is also used for render order (higher number on top).
|
|
|
|
*/
|
|
|
|
enum Category {
|
|
|
|
CATEGORY_WORLD = 1 << 1,
|
|
|
|
CATEGORY_NONSOLID = 1 << 2,
|
|
|
|
CATEGORY_PARTICLE = 1 << 3,
|
|
|
|
CATEGORY_ACTOR = 1 << 4
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Common collision masking values.
|
|
|
|
*/
|
|
|
|
enum Mask : unsigned short {
|
2013-03-03 20:27:40 +00:00
|
|
|
MASK_ALL = 0xffff, //< Enables all collisions.
|
|
|
|
MASK_NONE = 0 //< Disables any collisions.
|
2012-12-23 14:50:49 +00:00
|
|
|
};
|
|
|
|
|
2012-09-16 18:45:12 +00:00
|
|
|
// Public functions.
|
2012-09-09 20:50:15 +00:00
|
|
|
public:
|
2013-05-07 22:00:05 +00:00
|
|
|
explicit Sprite(const sf::Vector2f& position, Category category,
|
|
|
|
unsigned short mask, const sf::Vector2f& size,
|
|
|
|
const std::string& texture, const sf::Vector2f& direction);
|
|
|
|
virtual ~Sprite() = default;
|
2012-09-09 20:50:15 +00:00
|
|
|
|
2012-12-23 14:50:49 +00:00
|
|
|
sf::Vector2f getPosition() const;
|
|
|
|
sf::Vector2f getSpeed() const;
|
2013-03-30 01:30:11 +00:00
|
|
|
sf::Vector2f getDirection() const;
|
2012-12-23 14:50:49 +00:00
|
|
|
bool getDelete() const;
|
|
|
|
Category getCategory() const;
|
|
|
|
sf::Vector2f getSize() const;
|
|
|
|
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
|
2013-03-03 20:27:40 +00:00
|
|
|
bool collisionEnabled(Category category) const;
|
2013-04-27 19:29:05 +00:00
|
|
|
bool isInside(const sf::FloatRect& rect) const;
|
2012-12-23 14:50:49 +00:00
|
|
|
|
2013-05-26 18:16:36 +00:00
|
|
|
virtual bool testCollision(std::shared_ptr<Sprite> other,
|
|
|
|
sf::Vector2f& offsetFirst, const sf::Vector2f& offsetSecond) = 0;
|
2013-03-03 20:27:40 +00:00
|
|
|
virtual void onCollide(std::shared_ptr<Sprite> other);
|
2012-12-23 14:50:49 +00:00
|
|
|
|
2012-09-16 18:45:12 +00:00
|
|
|
protected:
|
2012-12-23 14:50:49 +00:00
|
|
|
void setDelete(bool value);
|
|
|
|
void setSpeed(sf::Vector2f direction, float speed);
|
2013-03-30 01:30:11 +00:00
|
|
|
void setDirection(const sf::Vector2f& direction);
|
2012-12-23 14:50:49 +00:00
|
|
|
void setPosition(const sf::Vector2f& position);
|
2013-03-03 20:27:40 +00:00
|
|
|
|
2012-09-16 18:45:12 +00:00
|
|
|
private:
|
2013-07-09 20:29:56 +00:00
|
|
|
friend class CollisionModel;
|
2013-03-03 20:27:40 +00:00
|
|
|
friend class World;
|
|
|
|
|
2013-05-07 22:00:05 +00:00
|
|
|
sf::RectangleShape mShape;
|
2012-09-09 20:50:15 +00:00
|
|
|
std::shared_ptr<sf::Texture> mTexture;
|
2012-12-23 14:50:49 +00:00
|
|
|
sf::Vector2f mSpeed;
|
|
|
|
Category mCategory;
|
|
|
|
unsigned short mMask;
|
|
|
|
bool mDelete;
|
2012-09-09 20:50:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* DG_SPRITE_H_ */
|