/
animation.cpp
102 lines (80 loc) · 1.82 KB
/
animation.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
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
#include "pch.hpp"
#include "animation.hpp"
#include "mainGame.hpp"
animation::animation(std::string animationName, int _frames, float frameTim)
{
frames = _frames;
frameTime = sf::seconds(frameTim);
texture = mainGame::getInstance()->textures.getAnimation(animationName);
spriteWidth = texture->getSize().x / 5;
if (frames >= 21)
{
spriteHeight = texture->getSize().y / 5;
}
else if (frames >= 16)
{
spriteHeight = texture->getSize().y / 4;
}
else if (frames >= 11)
{
spriteHeight = texture->getSize().y / 3;
}
else if (frames >= 6)
{
spriteHeight = texture->getSize().y / 2;
}
else spriteHeight = texture->getSize().y;
setTexture(*texture);
setTextureRect(sf::IntRect(0, 0, spriteWidth, spriteHeight));
timer = sf::Time::Zero;
frameCounter = 1;
}
animation::~animation()
{
}
void animation::update()
{
timer += game::getInstance()->getTimer();
if (timer >= frameTime)
{
nextFrame();
timer = sf::Time::Zero;
frameCounter++;
}
}
bool animation::end()
{
return (frameCounter == frames);
}
void animation::repeat()
{
setTextureRect(sf::IntRect(0, 0, spriteWidth, spriteHeight));
timer = sf::Time::Zero;
frameCounter = 1;
}
sf::Vector2f animation::getCenter()
{
sf::Vector2f center;
center.x = getPosition().x + getGlobalBounds().width / 2;
center.y = getPosition().y + getGlobalBounds().height / 2;
return center;
}
sf::Vector2f animation::getSize()
{
sf::Vector2f size;
size.x = getGlobalBounds().width;
size.y = getGlobalBounds().height;
return size;
}
void animation::nextFrame()
{
sf::IntRect currentRect = getTextureRect();
if (frameCounter % 5 == 0)
{
currentRect.top += spriteHeight;
currentRect.left = 0;
}
else
currentRect.left += spriteWidth;
setTextureRect(currentRect);
}