Skip to content

Commit 66c8cf0

Browse files
committed
Improve Lighthouse health checks and tracking
1 parent 9b69671 commit 66c8cf0

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

src/game.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ Game::Game(gsl::not_null<le::ServiceLocator const*> services) : m_services(servi
2323
auto const& context = services->get<le::Context>();
2424
auto const asset_loader = le::AssetLoader{&data_loader, &context};
2525
m_font = asset_loader.load_font("fonts/specialElite.ttf");
26-
auto const framebuffer_size = m_services->get<le::Context>().framebuffer_size();
27-
auto const y = (static_cast<float>(framebuffer_size.y) / 2.0f) - 50.0f;
28-
m_score_text.transform.position.y = y;
29-
m_health_text.transform.position = glm::vec2{(static_cast<float>(framebuffer_size.y) / 2.0f) - 50.0f, y};
3026
}
3127

3228
void Game::on_cursor_pos(le::event::CursorPos const& cursor_pos) {
@@ -77,13 +73,28 @@ void Game::spawn_wave() {
7773
}
7874

7975
void Game::update_score(int points) {
80-
auto const framebuffer_size = m_services->get<le::Context>().framebuffer_size();
76+
auto const& framebuffer_size = m_services->get<le::Context>().framebuffer_size();
8177
m_score_text.transform.position.y = static_cast<float>(framebuffer_size.y) / 2.0f - 50.0f;
8278
m_score += points;
8379
m_score_str.clear();
8480
std::format_to(std::back_inserter(m_score_str), "Score: {}", m_score);
8581
m_score_text.set_string(m_font, m_score_str);
8682
}
87-
void Game::update_health_text() {}
83+
84+
void Game::update_health_text() {
85+
auto const& framebuffer_size = m_services->get<le::Context>().framebuffer_size();
86+
float const x = (static_cast<float>(framebuffer_size.x) * 0.5f) - 150.0f;
87+
float const y = (static_cast<float>(framebuffer_size.y) * 0.5f) - 50.0f;
88+
m_health_text.transform.position = {x, y};
89+
90+
m_health_str.clear();
91+
if (m_lighthouse.get_health() <= 0.0f) {
92+
std::format_to(std::back_inserter(m_health_str), "Game Over");
93+
} else {
94+
std::format_to(std::back_inserter(m_health_str), "Health: {:.1f}", m_lighthouse.get_health());
95+
}
96+
97+
m_health_text.set_string(m_font, m_health_str);
98+
}
8899

89100
} // namespace miracle

src/game.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class Game {
3434
le::drawable::Text m_health_text{};
3535
int m_score{};
3636
std::string m_score_str;
37-
37+
std::string m_health_str;
3838
glm::vec2 m_cursor_pos{};
3939
std::size_t m_wave_count{};
4040
bool m_running{true};

0 commit comments

Comments
 (0)