#include "../app/Application.h"
#include "../app/State.h"
#include "../app/Timer.h"
+#include "../loader/fwd.h"
#include "../geometry/Vector.h"
+#include <memory>
#include <SDL.h>
namespace graphics {
class Animation {
+public:
+ static const int TYPE_ID = 401;
+
public:
Animation()
: sprite(0), frameTime(0), repeat(false) { }
virtual ~Animation() { };
public:
-
const Sprite *GetSprite() const { return sprite; }
int FrameTime() const { return frameTime; }
bool Repeat() const { return repeat; }
+public:
+ void SetSprite(const Sprite *s) { sprite = s; }
+ void SetFrameTime(int t) { frameTime = t; }
+ void SetRepeat(bool r) { repeat = r; }
+
public:
virtual int NumFrames() const = 0;
virtual int Col(int frame) const = 0;
virtual int Row(int frame) const = 0;
virtual geometry::Vector<int> Offset(int frame) const { return geometry::Vector<int>(); }
+ static void CreateTypeDescription();
+
+protected:
+ static void AddFields(loader::TypeDescription &, const Animation &, std::ptrdiff_t offset);
+
private:
const Sprite *sprite;
int frameTime;
public:
explicit AnimationRunner(const Animation *a = 0, int colOffset = 0, int rowOffset = 0)
- : animation(a), sprite(a ? a->GetSprite() : 0), colOffset(colOffset), rowOffset(rowOffset) { }
+ : animation(a), sprite(0), frameShift(0), colOffset(colOffset), rowOffset(rowOffset) { }
public:
bool Valid() const { return animation; }
const app::Timer<Uint32> &GetTimer() { return timer; }
+ void SetFrameShift(int offset) { frameShift = offset; }
+ int FrameShift() const { return frameShift; }
void SetColOffset(int offset) { colOffset = offset; }
int ColOffset() const { return colOffset; }
void SetRowOffset(int offset) { rowOffset = offset; }
int RowOffset() const { return rowOffset; }
+ void ChangeAnimation(const Animation *a) { animation = a; }
+ const Animation *GetAnimation() const { return animation; }
+
void ChangeSprite(const Sprite *s) { sprite = s; }
- const Sprite *GetSprite() const { return sprite; }
+ const Sprite *GetSprite() const { return sprite ? sprite : animation->GetSprite(); }
void Draw(SDL_Surface *dest, geometry::Vector<int> position) const {
- sprite->Draw(dest, position + animation->Offset(Frame()), animation->Col(Frame()) + ColOffset(), animation->Row(Frame()) + RowOffset());
+ GetSprite()->Draw(dest, position + animation->Offset(Frame()), animation->Col(Frame()) + ColOffset(), animation->Row(Frame()) + RowOffset());
}
void DrawTopRight(SDL_Surface *dest, geometry::Vector<int> position) const {
- geometry::Vector<int> offset(-sprite->Width(), 0);
+ geometry::Vector<int> offset(-GetSprite()->Width(), 0);
Draw(dest, position + offset);
}
void DrawCenter(SDL_Surface *dest, geometry::Vector<int> position) const {
- geometry::Vector<int> offset(-sprite->Width() / 2, -sprite->Height() / 2);
- Draw(dest, position + offset);
+ Draw(dest, position - (GetSprite()->Size() / 2));
}
void DrawCenterBottom(SDL_Surface *dest, geometry::Vector<int> position) const {
- geometry::Vector<int> offset(-sprite->Width() / 2, -sprite->Height());
+ geometry::Vector<int> offset(-GetSprite()->Width() / 2, -GetSprite()->Height());
Draw(dest, position + offset);
}
- int Frame() const { return Running() ? (timer.Iteration() % animation->NumFrames()) : 0; }
+ int Frame() const { return Running() ? ((timer.Iteration() + frameShift) % animation->NumFrames()) : 0; }
private:
const Animation *animation;
const graphics::Sprite *sprite;
app::Timer<Uint32> timer;
+ int frameShift;
int colOffset;
int rowOffset;