diff options
author | JAROWMR <jarorutjes07@gmail.com> | 2025-01-07 15:03:53 +0100 |
---|---|---|
committer | JAROWMR <jarorutjes07@gmail.com> | 2025-01-07 15:03:53 +0100 |
commit | e7ff8a9d0cff724520c4fb7a13b77e93797759cf (patch) | |
tree | c9261f172eef20857010180d82cb30e51cd6e5e8 /game | |
parent | 2c4091f845513cee2de4aac652f9720c74789327 (diff) |
saving coins avaiable in menu
Diffstat (limited to 'game')
-rw-r--r-- | game/CMakeLists.txt | 1 | ||||
-rw-r--r-- | game/coins/CoinScript.cpp | 2 | ||||
-rw-r--r-- | game/hud/HudScript.cpp | 10 | ||||
-rw-r--r-- | game/hud/HudScript.h | 3 | ||||
-rw-r--r-- | game/menus/ButtonNextMainMenuScript.cpp | 37 | ||||
-rw-r--r-- | game/menus/ButtonNextMainMenuScript.h | 15 | ||||
-rw-r--r-- | game/menus/ButtonSetMainMenuScript.cpp | 4 | ||||
-rw-r--r-- | game/menus/ButtonSubScene.cpp | 4 | ||||
-rw-r--r-- | game/menus/ButtonSubScene.h | 1 | ||||
-rw-r--r-- | game/menus/endgame/EndGameSubScene.cpp | 2 |
10 files changed, 72 insertions, 7 deletions
diff --git a/game/CMakeLists.txt b/game/CMakeLists.txt index a936ca0..ccafc9b 100644 --- a/game/CMakeLists.txt +++ b/game/CMakeLists.txt @@ -28,6 +28,7 @@ add_executable(main menus/IButtonScript.cpp menus/ButtonSetShopScript.cpp menus/ButtonSetMainMenuScript.cpp + menus/ButtonNextMainMenuScript.cpp menus/FloatingWindowSubScene.cpp menus/IFloatingWindowScript.cpp menus/shop/ShopMenuScene.cpp diff --git a/game/coins/CoinScript.cpp b/game/coins/CoinScript.cpp index a3ee3b9..2ecbb98 100644 --- a/game/coins/CoinScript.cpp +++ b/game/coins/CoinScript.cpp @@ -19,7 +19,6 @@ bool CoinScript::on_collision(const CollisionEvent & collisionData){ void CoinScript::init(){ this->subscribe<CollisionEvent>([this](const CollisionEvent & ev) -> bool { return this->on_collision(ev); }); - this->subscribe<EndGameEvent>([this](const EndGameEvent e)-> bool { return this->save(); }); } void CoinScript::fixed_update(crepe::duration_t dt) { @@ -31,6 +30,5 @@ void CoinScript::fixed_update(crepe::duration_t dt) { bool CoinScript::save(){ SaveManager & savemgr = this->get_save_manager(); savemgr.set(TOTAL_COINS_RUN, this->amount); - this->amount = 0; return false; } 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 = ""; }; diff --git a/game/menus/ButtonNextMainMenuScript.cpp b/game/menus/ButtonNextMainMenuScript.cpp new file mode 100644 index 0000000..d7f48e2 --- /dev/null +++ b/game/menus/ButtonNextMainMenuScript.cpp @@ -0,0 +1,37 @@ +#include "ButtonNextMainMenuScript.h" +#include "MenusConfig.h" +#include "ValueBroker.h" +#include "api/AudioSource.h" +#include "manager/SaveManager.h" +#include "types.h" +#include "../Config.h" + +using namespace crepe; +using namespace std; + +void ButtonNextMainMenuScript::init(){ + IButtonScript::init(); + this->subscribe<ButtonPressEvent>([this](const ButtonPressEvent& e) { return this->on_button_press(e); }); +} + +bool ButtonNextMainMenuScript::on_button_press(const ButtonPressEvent& e){ + RefVector<AudioSource> audios = this->get_components_by_name<AudioSource>("background_music"); + + for (AudioSource & audio : audios) { + audio.stop(); + } + + SaveManager & savemgr = this->get_save_manager(); + + ValueBroker<int> coins = savemgr.get<int>(TOTAL_COINS_RUN,0); + ValueBroker<int> coins_game = savemgr.get<int>(TOTAL_COINS_GAME,0); + savemgr.set(TOTAL_COINS_GAME, coins_game.get()+coins.get()); + + ValueBroker<int> distance = savemgr.get<int>(DISTANCE_RUN,0); + ValueBroker<int> distance_game = savemgr.get<int>(DISTANCE_GAME,0); + if(distance.get() > distance_game.get()) savemgr.set(DISTANCE_GAME, distance.get()); + + this->set_next_scene(MAINMENU_SCENE); + return false; +} + diff --git a/game/menus/ButtonNextMainMenuScript.h b/game/menus/ButtonNextMainMenuScript.h new file mode 100644 index 0000000..b9f70b3 --- /dev/null +++ b/game/menus/ButtonNextMainMenuScript.h @@ -0,0 +1,15 @@ +#pragma once + +#include "IButtonScript.h" + +#include <crepe/api/Script.h> + +class ButtonNextMainMenuScript : public IButtonScript { +public: + void init() override; + bool on_button_press(const crepe::ButtonPressEvent& e); +private: + float velocity = 20; +protected: + bool transition = false; +}; diff --git a/game/menus/ButtonSetMainMenuScript.cpp b/game/menus/ButtonSetMainMenuScript.cpp index e9232b5..e967dfb 100644 --- a/game/menus/ButtonSetMainMenuScript.cpp +++ b/game/menus/ButtonSetMainMenuScript.cpp @@ -14,10 +14,6 @@ void ButtonSetMainMenuScript::init(){ bool ButtonSetMainMenuScript::on_button_press(const ButtonPressEvent& e){ RefVector<AudioSource> audios = this->get_components_by_name<AudioSource>("background_music"); - for (AudioSource & audio : audios) { - audio.stop(); - } - this->set_next_scene(MAINMENU_SCENE); return false; } diff --git a/game/menus/ButtonSubScene.cpp b/game/menus/ButtonSubScene.cpp index 8574b9b..ea0df8a 100644 --- a/game/menus/ButtonSubScene.cpp +++ b/game/menus/ButtonSubScene.cpp @@ -1,4 +1,5 @@ #include "ButtonSubScene.h" +#include "ButtonNextMainMenuScript.h" #include "ButtonSetMainMenuScript.h" #include "ButtonSetShopScript.h" #include "IButtonScript.h" @@ -46,6 +47,9 @@ void ButtonSubScene::set_script(crepe::GameObject & button_object,const Data & d case ScriptSelect::MAINMENU: button_object.add_component<BehaviorScript>().set_script<ButtonSetMainMenuScript>(); break; + case ScriptSelect::NEXT: + button_object.add_component<BehaviorScript>().set_script<ButtonNextMainMenuScript>(); + break; case ScriptSelect::NONE: button_object.add_component<BehaviorScript>().set_script<IButtonScript>(); break; diff --git a/game/menus/ButtonSubScene.h b/game/menus/ButtonSubScene.h index 28daed2..09c2fcd 100644 --- a/game/menus/ButtonSubScene.h +++ b/game/menus/ButtonSubScene.h @@ -15,6 +15,7 @@ public: PREVIEW, SHOP, MAINMENU, + NEXT, NONE, }; //icon enum diff --git a/game/menus/endgame/EndGameSubScene.cpp b/game/menus/endgame/EndGameSubScene.cpp index 41556af..5360c96 100644 --- a/game/menus/endgame/EndGameSubScene.cpp +++ b/game/menus/endgame/EndGameSubScene.cpp @@ -45,7 +45,7 @@ void EndGameSubScene::create(Scene & scn){ .text = "NEXT", .text_width = 100, .position = button_position, - .script_type = ButtonSubScene::ScriptSelect::MAINMENU, + .script_type = ButtonSubScene::ScriptSelect::NEXT, .button_type = ButtonSubScene::ButtonSelect::NEXT, .scale = 0.6, .worldspace = false, |