88#include < vector>
99#include " enemy.hpp"
1010#include " enemy_params.hpp"
11+ #include " glm/ext/vector_float2.hpp"
1112#include " kvf/time.hpp"
1213#include " le2d/asset_loader.hpp"
1314#include " le2d/data_loader.hpp"
@@ -30,14 +31,18 @@ void Game::on_cursor_pos(le::event::CursorPos const& cursor_pos) {
3031}
3132
3233void Game::tick ([[maybe_unused]] kvf::Seconds const dt) {
34+ m_running = m_lighthouse.get_health () > 0 ;
3335 if (!m_running) { return ; }
36+
3437 m_time_since_last_wave_spawn += dt;
3538 if (m_time_since_last_wave_spawn >= m_wave_interval) {
3639 spawn_wave ();
3740 m_time_since_last_wave_spawn = kvf::Seconds{};
3841 }
3942 for (auto & enemy : m_enemies) {
4043 m_light.check_enemy_collision (enemy);
44+ m_lighthouse.check_visibility_range (enemy);
45+ update_health_text ();
4146 enemy.translate (dt);
4247 }
4348 // Keep track of how many enemies were defeated and calculate score
@@ -52,6 +57,7 @@ void Game::render(le::Renderer& renderer) const {
5257 m_lighthouse.render (renderer);
5358 for (auto const & enemy : m_enemies) { enemy.render (renderer); }
5459 m_score_text.draw (renderer);
60+ m_health_text.draw (renderer);
5561}
5662
5763void Game::spawn_wave () {
@@ -74,4 +80,21 @@ void Game::update_score(int points) {
7480 std::format_to (std::back_inserter (m_score_str), " Score: {}" , m_score);
7581 m_score_text.set_string (m_font, m_score_str);
7682}
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+ }
99+
77100} // namespace miracle
0 commit comments