-
Notifications
You must be signed in to change notification settings - Fork 0
/
Animator.h
77 lines (66 loc) · 2.2 KB
/
Animator.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#pragma once
#include <QPixmap>
#include "GameObject.h"
#include "TickableObject.h"
#include "CollisionDetection.h"
#include <iostream>
struct Animatable {
QPixmap* pixmaps = 0;
float* duration = 0;
KA::Vec2Df* offset = 0;
unsigned int size = 0;
};
class Animator : public TickableObject, public Cloneable {
protected:
Animatable* next_anim = 0;
float savedtimescale = 1;
Animatable* current_anim = 0;
unsigned int cur = 0;
float time = 0;
int repeat = 1, repetitions = 0;
float timescale = 1;
public:
void tick(double delta);
void playSecondary(Animatable* anim, int repeat = 0, float timescale = 1) {
if(!isPlaying(anim))
if (isPlayingOneShot()) {
next_anim = anim;
savedtimescale = timescale;
} else {
setAnimatable(anim, repeat, timescale);
}
}
void setAnimatable(Animatable* anim, int repeat = 0, float timescale = 1) {
if (!anim) { std::cout << "Animator received nullptr\n"; }
this->timescale = timescale;
if (current_anim == anim) { repetitions = 0; return; }
repetitions = 0;
this->repeat = repeat;
current_anim = anim;
cur = 0;
restartAnimation();
}
QPixmap getCurrentPixmap(bool mirror = 0) { return /*!current_anim ? QPixmap() :*/ mirror ? current_anim->pixmaps[cur].transformed(QTransform().scale(-1, 1)) : current_anim->pixmaps[cur]; }
void playOneShot(Animatable* anim, int repeat = 0, float timescale = 1) { if (!next_anim) { next_anim = current_anim; savedtimescale = timescale; } setAnimatable(anim, repeat, timescale); }
bool isPlayingOneShot() { return next_anim != 0; }
bool isPlaying(Animatable* test) { return current_anim == test; }
void interruptOneShot();
void restartAnimation() { cur = 0; time = 0; }
KA::Vec2Df getCurrentOffset() { return !current_anim ? KA::Vec2Df(0,0) : current_anim->offset[cur]; }
Animator() {}
Animator(const Animator& go) {
*this = go;
}
Animator& operator= (const Animator& go) {
this->repeat = go.repeat;
this->timescale = go.timescale;
this->next_anim = go.next_anim;
this->current_anim = go.current_anim;
this->cur = go.cur;
this->time = go.time;
this->repetitions = go.repetitions;
this->savedtimescale = go.savedtimescale;
return *this;
}
Cloneable* clone() const { return new Animator(*this); }
};