-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimatedObject.cpp
More file actions
113 lines (98 loc) · 2.46 KB
/
Copy pathAnimatedObject.cpp
File metadata and controls
113 lines (98 loc) · 2.46 KB
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
#include "AnimatedObject.h"
#include <iostream>
#include "ResourceLocator.h"
AnimatedObject::AnimatedObject()
{
m_CurrentAnimation = NULL;
m_XFlipped = false;
m_YFlipped = false;
m_Blinking = false;
m_Visible = true;
m_TimeBetweenBlinksInMs = 50;
}
AnimatedObject::~AnimatedObject()
{
if(m_CurrentAnimation)
{
delete m_CurrentAnimation;
}
}
bool AnimatedObject::registerAnimation(std::string local_name, std::string global_name)
{
Animation* anim = ResourceLocator::requestAnimation(global_name, &m_Sprite);
m_CurrentAnimation = anim;
if(anim)
{
if(m_RegisteredAnimations.count(local_name) == 0)
{
m_RegisteredAnimations[local_name] = anim;
}
else
{
std::cerr << "Attempted to register two animations by the same name in AnimatedObject. " << std::endl;
return false;
}
}
return false;
}
bool AnimatedObject::setCurrentAnimation(std::string local_name)
{
if(m_RegisteredAnimations.count(local_name))
{
if(m_CurrentAnimation != m_RegisteredAnimations[local_name])
{
m_CurrentAnimation = m_RegisteredAnimations[local_name];
m_CurrentAnimation->reset();
m_Sprite.setTexture(*m_CurrentAnimation->getTexture());
}
return true;
}
return false;
}
Animation* AnimatedObject::getCurrentAnimation()
{
return m_CurrentAnimation;
}
void AnimatedObject::renderAt(const Camera& camera, int brightness)
{
if( m_Blinking )
{
if( m_CurrentBlink.getElapsedTime().asMilliseconds() > m_TimeBetweenBlinksInMs )
{
m_CurrentBlink.restart();
m_Visible = !m_Visible;
}
if( m_BlinkTimer.getElapsedTime().asMilliseconds() > m_TimeToBlink )
{
m_Blinking = false;
m_Visible = true;
}
}
if(m_Visible && getCurrentAnimation())
{
int texture_w = m_Sprite.getTextureRect().width;
int texture_h = m_Sprite.getTextureRect().height;
float offset_x = m_Location.x - camera.GetPosition().x;
float offset_y = m_Location.y - camera.GetPosition().y;
m_Sprite.setColor(sf::Color(brightness, brightness, brightness, 255));
m_Sprite.setPosition(offset_x, offset_y);
if(m_XFlipped)
{
m_Sprite.scale(-1.f,1.f);
m_Sprite.move((float)texture_w,0.f);
getCurrentAnimation()->render();
m_Sprite.scale(-1.f,1.f);
}
else
{
getCurrentAnimation()->render();
}
}
}
void AnimatedObject::blinkFor( sf::Int32 blinkTime )
{
m_Blinking = true;
m_BlinkTimer.restart();
m_CurrentBlink.restart();
m_TimeToBlink = blinkTime;
}