-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPiece.cpp
74 lines (62 loc) · 1.51 KB
/
Piece.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
#include "Piece.hpp"
Piece::Piece(const sf::Texture& texture)
{
m_frame = 6;
m_size = sf::Vector2f(14, 16);
m_duration = 1.4;
m_texture = &texture;
m_sprite.setTexture(*m_texture);
for( int i = 0; i < m_frame; i++ )
{
m_anim.push_back(sf::IntRect(m_size.x*i, 0, m_size.x, m_size.y));
}
m_activeFrame = 0;
m_sprite.setTextureRect(m_anim[m_activeFrame]);
m_time = 0;
}
void Piece::setPos(sf::Vector2f pos)
{
m_sprite.setPosition(pos);
}
int Piece::getRectW()
{
sf::IntRect tRect = m_sprite.getTextureRect();
return tRect.width;
}
int Piece::getRectH()
{
sf::IntRect tRect = m_sprite.getTextureRect();
return tRect.height;
}
sf::Sprite Piece::getSprite(float elapsedTime)
{
m_time += elapsedTime;
if (m_time > m_duration)
{
m_time = 0;
m_activeFrame = 0;
m_sprite.setTextureRect(m_anim[m_activeFrame]);
}else
{
for (int i = 1; i <= m_frame; i++)
{
if (m_time < (m_duration/m_frame)*i)
{
int f = i-1;
if (m_activeFrame != f)
{
m_activeFrame = f;
m_sprite.setTextureRect(m_anim[m_activeFrame]);
}
break;
}
}
}
return m_sprite;
}
sf::Vector2f Piece::getCenter()
{
sf::Vector2f pos = m_sprite.getPosition();
sf::IntRect rect = m_sprite.getTextureRect();
return sf::Vector2f(pos.x+(rect.width/2), pos.y+(rect.height/2));
}