aboutsummaryrefslogtreecommitdiff
path: root/game/hud
diff options
context:
space:
mode:
authorJAROWMR <jarorutjes07@gmail.com>2025-01-07 15:03:53 +0100
committerJAROWMR <jarorutjes07@gmail.com>2025-01-07 15:03:53 +0100
commite7ff8a9d0cff724520c4fb7a13b77e93797759cf (patch)
treec9261f172eef20857010180d82cb30e51cd6e5e8 /game/hud
parent2c4091f845513cee2de4aac652f9720c74789327 (diff)
saving coins avaiable in menu
Diffstat (limited to 'game/hud')
-rw-r--r--game/hud/HudScript.cpp10
-rw-r--r--game/hud/HudScript.h3
2 files changed, 13 insertions, 0 deletions
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 <climits>
@@ -20,6 +21,7 @@ void HudScript::init() {
this->subscribe<GetCoinEvent>([this](const GetCoinEvent e)-> bool { return this->get_coin(e); });
this->subscribe<KeyPressEvent>([this](const KeyPressEvent & ev) -> bool { return this->toggle_fps(ev);});
+ this->subscribe<EndGameEvent>([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<Text>(HUD_DISTANCE).front();
Transform & tf = this->get_components_by_name<Transform>(PLAYER_NAME).front();
string distance = to_string(static_cast<int>(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<Text>(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 = "";
};