22#include < glm/gtx/norm.hpp>
33#include < le2d/context.hpp>
44#include < cstddef>
5+ #include < format>
6+ #include < iterator>
57#include < string>
68#include < vector>
79#include " enemy.hpp"
1517#include " util/random.hpp"
1618
1719namespace miracle {
18- Game::Game (gsl::not_null<le::ServiceLocator const *> services)
19- : m_services(services), m_lighthouse(services), m_light(services), m_score_text(le::drawable::Text()) {
20+ Game::Game (gsl::not_null<le::ServiceLocator const *> services) : m_services(services), m_lighthouse(services), m_light(services) {
2021 spawn_wave ();
2122 auto const & data_loader = services->get <le::IDataLoader>();
2223 auto const & context = services->get <le::Context>();
@@ -45,13 +46,12 @@ void Game::tick([[maybe_unused]] kvf::Seconds const dt) {
4546 for (auto & enemy : m_enemies) {
4647 m_light.check_enemy_collision (enemy);
4748 m_lighthouse.check_visibility_range (enemy);
48- set_health_text ();
49+ update_health_text ();
4950 enemy.translate (dt);
5051 }
5152 // Keep track of how many enemies were defeated and calculate score
5253 auto res = std::erase_if (m_enemies, [](Enemy const & enemy) { return !enemy.get_health (); });
53- increase_score (res * 10 );
54-
54+ update_score (static_cast <int >(res * 10 ));
5555 m_light.set_position (m_cursor_pos);
5656 m_lighthouse.rotate_towards_cursor (m_cursor_pos);
5757}
@@ -76,15 +76,14 @@ void Game::spawn_wave() {
7676 m_enemies.insert (m_enemies.end (), std::make_move_iterator (new_wave.begin ()), std::make_move_iterator (new_wave.end ()));
7777}
7878
79- void Game::increase_score (std::size_t points) {
79+ void Game::update_score (int points) {
80+ auto const framebuffer_size = m_services->get <le::Context>().framebuffer_size ();
81+ m_score_text.transform .position .y = static_cast <float >(framebuffer_size.y ) / 2 .0f - 50 .0f ;
8082 m_score += points;
81- m_score_text.set_string (m_font, " Score: " + std::to_string (m_score));
82- }
83-
84- void Game::set_health_text () {
85- float const health = m_lighthouse.get_health ();
86- std::string text = health > 0 ? " Health: " + std::format (" {:.1f}" , health) : " Game Over" ;
87- m_health_text.set_string (m_font, text);
83+ m_score_str.clear ();
84+ std::format_to (std::back_inserter (m_score_str), " Score: {}" , m_score);
85+ m_score_text.set_string (m_font, m_score_str);
8886}
87+ void Game::update_health_text () {}
8988
9089} // namespace miracle
0 commit comments