From e7ff8a9d0cff724520c4fb7a13b77e93797759cf Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Tue, 7 Jan 2025 15:03:53 +0100 Subject: saving coins avaiable in menu --- game/hud/HudScript.cpp | 10 ++++++++++ game/hud/HudScript.h | 3 +++ 2 files changed, 13 insertions(+) (limited to 'game/hud') diff --git a/game/hud/HudScript.cpp b/game/hud/HudScript.cpp index 30a5c15..165f245 100644 --- a/game/hud/HudScript.cpp +++ b/game/hud/HudScript.cpp @@ -3,6 +3,7 @@ #include "api/Transform.h" #include "manager/SaveManager.h" #include "../Config.h" +#include "../Events.h" #include "HudConfig.h" #include @@ -20,6 +21,7 @@ void HudScript::init() { this->subscribe([this](const GetCoinEvent e)-> bool { return this->get_coin(e); }); this->subscribe([this](const KeyPressEvent & ev) -> bool { return this->toggle_fps(ev);}); + this->subscribe([this](const EndGameEvent e)-> bool { return this->save(); }); } bool HudScript::toggle_fps(crepe::KeyPressEvent ev){ @@ -42,6 +44,7 @@ void HudScript::frame_update(crepe::duration_t dt) { Text & txt_dt = this->get_components_by_name(HUD_DISTANCE).front(); Transform & tf = this->get_components_by_name(PLAYER_NAME).front(); string distance = to_string(static_cast(tf.position.x/STEP_SIZE_DISTANCE)) + DISTANCE_UNIT; + this->distance_st = distance; txt_dt.text = distance; txt_dt.dimensions = {DISTANCE_CHAR_WIDTH*distance.size(),(DISTANCE_CHAR_WIDTH)*2}; txt_dt.offset = TOP_LEFT+FONTOFFSET + vec2{distance.size() * DISTANCE_CHAR_WIDTH/2,0}; @@ -49,6 +52,7 @@ void HudScript::frame_update(crepe::duration_t dt) { // Coins Text & txt_co = this->get_components_by_name(HUD_COINS).front(); string amount_of_coins = to_string(this->coin_amount); + this->coin_amount_st = amount_of_coins; txt_co.text = amount_of_coins; txt_co.dimensions = {COINS_CHAR_WIDTH*amount_of_coins.size(),(COINS_CHAR_WIDTH)*2}; txt_co.offset = TOP_LEFT+FONTOFFSET+COINS_OFFSET + vec2{amount_of_coins.size() * COINS_CHAR_WIDTH/2,0}; @@ -71,3 +75,9 @@ bool HudScript::get_coin(const GetCoinEvent e){ return true; } +bool HudScript::save(){ + SaveManager & savemgr = this->get_save_manager(); + savemgr.set(TOTAL_COINS_RUN, this->coin_amount); + savemgr.set(DISTANCE_RUN, this->distance_st); + return false; +} diff --git a/game/hud/HudScript.h b/game/hud/HudScript.h index d780d4b..cf939f4 100644 --- a/game/hud/HudScript.h +++ b/game/hud/HudScript.h @@ -14,8 +14,11 @@ public: void frame_update(crepe::duration_t dt) override; bool get_coin(const GetCoinEvent e); bool toggle_fps(crepe::KeyPressEvent ev); + bool save(); private: crepe::SaveManager* savemgr; bool show_fps = false; int coin_amount = 0; + std::string coin_amount_st = ""; + std::string distance_st = ""; }; -- cgit v1.2.3