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
|
|
|
|
2013-08-07 15:39:43 +00:00
|
|
|
#include "../util/Vector.h"
|
|
|
|
|
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-08-07 15:39:43 +00:00
|
|
|
explicit 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
|
|
|
virtual ~Sprite() = default;
|
2012-09-09 20:50:15 +00:00
|
|
|
|
2013-08-07 15:39:43 +00:00
|
|
|
Vector2f getPosition() const;
|
|
|
|
Vector2f getSpeed() const;
|
|
|
|
Vector2f getDirection() const;
|
2012-12-23 14:50:49 +00:00
|
|
|
bool getDelete() const;
|
|
|
|
Category getCategory() const;
|
2013-08-07 15:39:43 +00:00
|
|
|
Vector2f getSize() const;
|
2012-12-23 14:50:49 +00:00
|
|
|
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,
|
2013-08-07 15:39:43 +00:00
|
|
|
Vector2f& offsetFirst, const 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);
|
2013-08-07 15:39:43 +00:00
|
|
|
void setSpeed(Vector2f direction, float speed);
|
|
|
|
void setDirection(const Vector2f& direction);
|
|
|
|
void setPosition(const Vector2f& position);
|
2013-08-20 19:44:11 +00:00
|
|
|
void setTexture(const std::string& texture);
|
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;
|
2013-08-07 15:39:43 +00:00
|
|
|
Vector2f mSpeed;
|
2012-12-23 14:50:49 +00:00
|
|
|
Category mCategory;
|
|
|
|
unsigned short mMask;
|
2013-07-14 19:56:44 +00:00
|
|
|
bool mDelete = false;
|
2012-09-09 20:50:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* DG_SPRITE_H_ */
|