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

104 lines
2.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>
class Yaml;
/**
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
};
/**
* Container that carries all data required to construct an object of
* this class.
2012-12-23 14:50:49 +00:00
*/
class Data {
public:
explicit Data(const sf::Vector2f& position,
Category category, unsigned short mask,
const sf::Vector2f& direction = sf::Vector2f(0, 0));
2012-12-23 14:50:49 +00:00
const sf::Vector2f& position;
const sf::Vector2f& direction;
2012-12-23 14:50:49 +00:00
Category category;
unsigned short mask;
};
/**
* 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 Data& data, const Yaml& config);
virtual ~Sprite() = 0;
2012-12-23 14:50:49 +00:00
sf::Vector2f getPosition() const;
sf::Vector2f getSpeed() const;
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;
bool isMovable() const;
2012-12-23 14:50:49 +00:00
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);
void setDirection(const sf::Vector2f& direction);
2012-12-23 14:50:49 +00:00
void setPosition(const sf::Vector2f& position);
2013-03-19 19:51:56 +00:00
float getRadius() const;
2013-03-03 20:27:40 +00:00
private:
class Shape {
public:
enum class Type {
CIRCLE,
RECTANGLE
};
Type type;
std::shared_ptr<sf::Shape> shape;
};
2012-09-16 18:45:12 +00:00
private:
2013-03-03 20:27:40 +00:00
friend class World;
Shape mShape;
std::shared_ptr<sf::Texture> mTexture;
2012-12-23 14:50:49 +00:00
sf::Vector2f mSpeed;
Category mCategory;
unsigned short mMask;
bool mDelete;
};
#endif /* DG_SPRITE_H_ */