-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnimationHolder.cpp
61 lines (50 loc) · 1.37 KB
/
AnimationHolder.cpp
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
#include "AnimationHolder.h"
#include <SFML/Graphics/RenderTarget.hpp>
Animation::Animation(sf::Texture& texture, int x, int y, int width, int height, int count, float speed, int step)
: speed(speed)
{
sprite.setTexture(texture);
for (int i = 0; i < count; ++i)
{
frames.push_back(sf::IntRect(x + i*step, y, width, height));
frames_flip.push_back(sf::IntRect(x + i*step + width, y, -width, height));
}
}
void Animation::Tick(float time)
{
if (!bPlaying)
{
return;
}
currentFrame += speed * time;
if (currentFrame > frames.size())
{
currentFrame -= frames.size();
if (!bLoop)
{
bPlaying = false;
return;
}
}
const int iFrame = static_cast<int>(currentFrame);
if (bFlip)
{
sprite.setTextureRect(frames_flip[iFrame]);
}
else
{
sprite.setTextureRect(frames[iFrame]);
}
}
////////////////////////////////////////////////////////////////////
void AnimationHolder::Create(const std::string& name, sf::Texture& texture, int x, int y, int width, int height, int count,
float speed, int step)
{
animations.emplace(name, Animation(texture, x, y, width, height, count, speed, step));
currentAnimationName = name;
}
void AnimationHolder::Draw(sf::RenderTarget& renderTarget, int x, int y)
{
animations[currentAnimationName].GetSprite().setPosition(x, y);
renderTarget.draw(animations[currentAnimationName].GetSprite());
}