-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnimationComponent.h
150 lines (120 loc) · 3.08 KB
/
AnimationComponent.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#pragma once
class AnimationComponent
{
private:
//Animation class:
class Animation
{
public:
//Variables:
sf::Sprite& sprite;
sf::Texture& textureSheet;
float animationTimer;
float timer;
bool done;
int width;
int height;
sf::IntRect startRect;
sf::IntRect currentRect;
sf::IntRect endRect;
//Constructors:
Animation
(
sf::Sprite& sprite, sf::Texture& textureSheet,
float animation_timer,
int start_frame_x, int start_frame_y, int frames_x, int frames_y, int width, int height
)
: sprite(sprite), textureSheet(textureSheet),
animationTimer(animation_timer), timer(animation_timer), done(false),
width(width), height(height)
{
this->startRect = sf::IntRect(start_frame_x * width, start_frame_y * height, width, height);
this->currentRect = this->startRect;
this->endRect = sf::IntRect(frames_x * width, frames_y * height, width, height);
this->sprite.setTexture(this->textureSheet, true);
this->sprite.setTextureRect(this->startRect);
}
//Accessors:
const bool& isDone() const
{
return this->done;
}
//Functions:
const bool& play(const float& dt)
{
//Updating timer
this->done = false;
this->timer += 100.f * dt;
if (this->timer >= this->animationTimer)
{
//Resetting timer
this->timer = 0.f;
//Animating
if (this->currentRect != this->endRect)
{
this->currentRect.left += this->width;
}
else //Resetting animation
{
this->currentRect.left = this->startRect.left;
this->done = true;
}
this->sprite.setTextureRect(this->currentRect);
}
return this->done;
}
const bool& play(float mod_percent, const float& dt)
{
//Updating timer
if (mod_percent < 0.8f)
{
mod_percent = 0.8f;
}
this->done = false;
this->timer += mod_percent * 100.f * dt;
if (this->timer >= this->animationTimer)
{
//Resetting timer
this->timer = 0.f;
//Animating
if (this->currentRect != this->endRect)
{
this->currentRect.left += this->width;
}
else //Resetting animation
{
this->currentRect.left = this->startRect.left;
this->done = true;
}
this->sprite.setTextureRect(this->currentRect);
}
return this->done;
}
void reset()
{
this->timer = this->animationTimer;
this->currentRect = this->startRect;
}
};
//Variables:
sf::Sprite& sprite;
sf::Texture& textureSheet;
std::map<std::string, Animation*> animations;
Animation* lastAnimation;
Animation* priorityAnimation;
public:
//Constructors and Destructor:
AnimationComponent(sf::Sprite& sprite, sf::Texture& texture_sheet);
virtual ~AnimationComponent();
//Accessors:
const bool& isDone(const std::string key);
//Functions:
void addAnimation
(
const std::string key,
float animation_timer,
int start_frame_x, int start_frame_y, int frames_x, int frames_y, int width, int height
);
const bool& play(const std::string key, const float& dt, const bool priority = false);
const bool& play(const std::string key, const float& dt, const float& modifier, const float& modifier_max, const bool priority = false);
};