This repository has been archived on 2019-12-07. You can view files and clone it, but cannot push or open issues or pull requests.
dungeon-gunner/source/abstract/Sprite.h

82 lines
2 KiB
C
Raw Normal View History

/*
* Sprite.h
*
2012-12-23 14:50:49 +00:00
* Created on: 11.08.2012
* Author: Felix
*/
#ifndef DG_SPRITE_H_
#define DG_SPRITE_H_
#include <memory>
#include <SFML/Graphics.hpp>
#include "../util/Vector.h"
/**
2012-12-23 14:50:49 +00:00
* An sprite that is rendered in the world.
*/
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.
public:
explicit Sprite(const Vector2f& position, Category category,
unsigned short mask, const Vector2f& size,
const std::string& texture, const Vector2f& direction);
virtual ~Sprite() = default;
Vector2f getPosition() const;
Vector2f getSpeed() const;
Vector2f getDirection() const;
2012-12-23 14:50:49 +00:00
bool getDelete() const;
Category getCategory() const;
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;
bool isInside(const sf::FloatRect& rect) const;
2012-12-23 14:50:49 +00:00
virtual bool testCollision(std::shared_ptr<Sprite> other,
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);
void setSpeed(Vector2f direction, float speed);
void setDirection(const Vector2f& direction);
void setPosition(const Vector2f& position);
void setTexture(const std::string& texture);
2013-03-03 20:27:40 +00:00
2012-09-16 18:45:12 +00:00
private:
friend class CollisionModel;
2013-03-03 20:27:40 +00:00
friend class World;
sf::RectangleShape mShape;
std::shared_ptr<sf::Texture> mTexture;
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;
};
#endif /* DG_SPRITE_H_ */