-
Notifications
You must be signed in to change notification settings - Fork 0
/
component_animation.inl
173 lines (148 loc) · 4.9 KB
/
component_animation.inl
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include "stdafx.h"
//
#include "animation_.h"
AnimationComponent::AnimationComponent(sf::Sprite& sprite,
sf::Texture& texture_sheet)
: sprite_(sprite),
texture_sheet_(texture_sheet),
last_animation_(nullptr),
priority_animation_(nullptr) {}
AnimationComponent::~AnimationComponent() {
for (auto& i : animations_) {
delete i.second;
}
}
const bool& AnimationComponent::IsDone(const _::AString key) {
return animations_[key]->IsDone();
}
void AnimationComponent::AddAnimation(const _::AString key,
float animation_timer, int start_frame_x,
int start_frame_y, int frames_x,
int frames_y, int width, int height) {
animations_[key] =
new Animation(sprite_, texture_sheet_, animation_timer, start_frame_x,
start_frame_y, frames_x, frames_y, width, height);
}
const bool& AnimationComponent::Play(const _::AString key, const float& dt,
const bool priority) {
if (priority_animation_) {
if (priority_animation_ == animations_[key]) {
if (last_animation_ != animations_[key]) {
if (last_animation_ == nullptr)
last_animation_ = animations_[key];
else {
last_animation_->reset();
last_animation_ = animations_[key];
}
}
if (animations_[key]->Play(dt)) {
priority_animation_ = nullptr;
}
}
} else {
if (priority) {
priority_animation_ = animations_[key];
}
if (last_animation_ != animations_[key]) {
if (last_animation_ == nullptr)
last_animation_ = animations_[key];
else {
last_animation_->reset();
last_animation_ = animations_[key];
}
}
animations_[key]->Play(dt);
}
return animations_[key]->IsDone();
}
const bool& AnimationComponent::Play(const _::AString key, const float& dt,
const float& modifier,
const float& modifier_max,
const bool priority) {
if (priority_animation_) { // If there is a priority animation
if (priority_animation_ == animations_[key]) {
if (last_animation_ != animations_[key]) {
if (last_animation_ == nullptr)
last_animation_ = animations_[key];
else {
last_animation_->reset();
last_animation_ = animations_[key];
}
}
if (animations_[key]->Play(dt, abs(modifier / modifier_max))) {
priority_animation_ = nullptr;
}
}
} else {
if (priority) {
priority_animation_ = animations_[key];
}
if (last_animation_ != animations_[key]) {
if (last_animation_ == nullptr)
last_animation_ = animations_[key];
else {
last_animation_->reset();
last_animation_ = animations_[key];
}
}
animations_[key]->Play(dt, abs(modifier / modifier_max));
}
return animations_[key]->IsDone();
}
AnimationComponent::Animation::Animation(sf::Sprite& sprite,
sf::Texture& texture_sheet,
float animation_timer,
int start_frame_x, int start_frame_y,
int frames_x, int frames_y, int width,
int height)
: sprite(sprite),
textureSheet(texture_sheet),
animationTimer(animation_timer),
timer(0.f),
done(false),
width(width),
height(height) {
start_rect =
sf::IntRect(start_frame_x * width, start_frame_y * height, width, height);
current_rect = start_rect;
endRect = sf::IntRect(frames_x * width, frames_y * height, width, height);
sprite.setTexture(textureSheet, true);
sprite.setTextureRect(start_rect);
}
const bool& AnimationComponent::Animation::IsDone() const { return done; }
const bool& AnimationComponent::Animation::Play(const float& dt) {
done = false;
timer += 100.f * dt;
if (timer >= animationTimer) {
timer = 0.f;
if (current_rect != endRect) {
current_rect.left += width;
} else {
current_rect.left = start_rect.left;
done = true;
}
sprite.setTextureRect(current_rect);
}
return done;
}
const bool& AnimationComponent::Animation::Play(const float& dt,
float mod_percent) {
if (mod_percent < 0.5f) mod_percent = 0.5f;
done = false;
timer += mod_percent * 100.f * dt;
if (timer >= animationTimer) {
timer = 0.f;
if (current_rect != endRect) {
current_rect.left += width;
} else {
current_rect.left = start_rect.left;
done = true;
}
sprite.setTextureRect(current_rect);
}
return done;
}
void AnimationComponent::Animation::Reset() {
timer = animationTimer;
current_rect = start_rect;
}