-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHudObject.cpp
More file actions
156 lines (128 loc) · 5.24 KB
/
Copy pathHudObject.cpp
File metadata and controls
156 lines (128 loc) · 5.24 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
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
// Copyright 2018 Corey Selover
#include "HudObject.h"
#include "Game.h"
#include "Texture.h"
#include "Player.h"
HudObject::HudObject(Game* game,
std::string fileName,
sf::IntRect textureRect,
int x,
int y,
int quadrant,
bool visibleAtStart = true)
: ManagedObject(game) {
m_sprite.setTexture(m_game->addTexture(fileName)->get());
// TODO - un-hard code this
m_sprite.setOrigin(sf::Vector2f(0,0));
m_sprite.setTextureRect(textureRect);
m_textureRect = textureRect;
switch(quadrant) {
case 1:
m_position = sf::Vector2f(m_game->windowCenter().x + m_game->windowSize().x / 2 - x,
m_game->windowCenter().y - m_game->windowSize().y / 2 + y);
break;
case 2:
m_position = sf::Vector2f(m_game->windowCenter().x - m_game->windowSize().x / 2 + x,
m_game->windowCenter().y - m_game->windowSize().y / 2 + y);
break;
case 3:
m_position = sf::Vector2f(m_game->windowCenter().x - m_game->windowSize().x / 2 + x,
m_game->windowCenter().y + m_game->windowSize().y / 2 - y);
break;
case 4:
m_position = sf::Vector2f(m_game->windowCenter().x + m_game->windowSize().x / 2 - x,
m_game->windowCenter().y + m_game->windowSize().y / 2 - y);
break;
default:
std::cout << "Invalid HudObject quadrant: " << quadrant << std::endl;
}
m_sprite.setPosition(m_position);
m_visible = visibleAtStart;
}
void HudObject::update() {}
void HudObject::draw() {
if(m_visible) m_game->draw(m_sprite);
}
sf::Vector2f HudObject::getPosition() {
return m_position;
}
void HudObject::setPosition(sf::Vector2f pos) {
m_position = pos;
}
/////////////////////////////////////////////////////////////////////////////////
// MANA_HUD_OBJECT //
/////////////////////////////////////////////////////////////////////////////////
ManaHudObject::ManaHudObject(Game* game,
std::string fileName,
sf::IntRect textureRect,
int x,
int y,
int quadrant,
bool visibleAtStart = true)
: HudObject(game, fileName, textureRect, x, y, quadrant, visibleAtStart) {
m_meterSprite.setTexture(m_game->addTexture(fileName)->get());
m_meterSprite.setOrigin(sf::Vector2f(0,0));
m_meterSprite.setTextureRect(sf::IntRect(textureRect.left,
textureRect.top + textureRect.height,
textureRect.width,
textureRect.height));
m_meterSprite.setPosition(m_position);
// Get player mana values.
m_maxMana = static_cast<Player*>(m_game->entity("Player"))->maxMana();
m_currentMana = static_cast<Player*>(m_game->entity("Player"))->currentMana();
m_currentMeterPercentage = 100;
}
void ManaHudObject::update() {
m_currentMana = static_cast<Player*>(m_game->entity("Player"))->currentMana();
int manaPercentage = int(100 * m_currentMana / m_maxMana);
m_meterSprite.setTextureRect(sf::IntRect(m_textureRect.left,
m_textureRect.top + m_textureRect.height,
int(m_textureRect.width * manaPercentage / 100),
m_textureRect.height));
}
void ManaHudObject::draw() {
HudObject::draw();
if(m_visible) m_game->draw(m_meterSprite);
}
/////////////////////////////////////////////////////////////////////////////////
// AURA_HUD_OBJECT //
/////////////////////////////////////////////////////////////////////////////////
AuraHudObject::AuraHudObject(Game* game,
std::string fileName,
sf::IntRect textureRect,
int x,
int y,
int quadrant,
bool visibleAtStart = true)
: HudObject(game, fileName, textureRect, x, y, quadrant, visibleAtStart) {
m_active = false;
}
void AuraHudObject::update() {
}
void AuraHudObject::toggle() {
m_active = !m_active;
if (m_active) {
m_sprite.setTextureRect(sf::IntRect(m_textureRect.left,
m_textureRect.top + m_textureRect.height,
m_textureRect.width,
m_textureRect.height));
}
else {
m_sprite.setTextureRect(m_textureRect);
}
}
void AuraHudObject::setActive(bool active) {
m_active = active;
if (m_active) {
m_sprite.setTextureRect(sf::IntRect(m_textureRect.left,
m_textureRect.top + m_textureRect.height,
m_textureRect.width,
m_textureRect.height));
}
else {
m_sprite.setTextureRect(m_textureRect);
}
}
bool AuraHudObject::active() const {
return m_active;
}