farmlands/src/base/Sprite.h

90 lines
1.3 KiB
C++

/*
* Sprite.h
*
* Created on: Nov 29, 2016
* Author: tibi
*/
#ifndef MODEL_SPRITE_H_
#define MODEL_SPRITE_H_
#include <utils/Exceptions.h>
#include <vector>
#include <unordered_map>
namespace farmlands {
namespace base {
/**
* Defines an animation frame
*/
struct Frame
{
uint32_t tileSetId;
uint32_t tileSetCell;
uint32_t width, height;
uint32_t duration;
};
/**
* Defines a sprite state (aka an animation).
*/
struct SpriteState
{
std::string name;
std::vector<Frame> frames;
};
/**
* Defines a sprite
*/
class Sprite
{
public:
Sprite();
virtual ~Sprite();
/**
* Adds a state to the sprite.
*/
void addState(const SpriteState& state);
/**
* Sets the current state.
*/
void setState(size_t stateId);
/**
* Sets the current state.
*/
void setState(const std::string& name);
/**
* Advances the current frame
*/
void advanceTime(uint32_t steps);
// Getters
SpriteState& currentState();
Frame& currentFrame();
// Public fields
float anchorX, anchorY;
std::string name;
private:
std::vector<SpriteState> m_states;
std::unordered_map<std::string, size_t> m_stateNames;
size_t m_currentState;
size_t m_currentFrame;
uint32_t m_currentFrameTimeLeft;
};
} /* namespace model */
} /* namespace farmlands */
#endif /* MODEL_SPRITE_H_ */