diff options
Diffstat (limited to 'game/menus/endgame')
-rw-r--r-- | game/menus/endgame/EndGameSubScene.cpp | 127 | ||||
-rw-r--r-- | game/menus/endgame/EndGameSubScene.h | 9 | ||||
-rw-r--r-- | game/menus/endgame/EndGameSubScript.cpp | 101 | ||||
-rw-r--r-- | game/menus/endgame/EndGameSubScript.h | 18 |
4 files changed, 255 insertions, 0 deletions
diff --git a/game/menus/endgame/EndGameSubScene.cpp b/game/menus/endgame/EndGameSubScene.cpp new file mode 100644 index 0000000..b33072a --- /dev/null +++ b/game/menus/endgame/EndGameSubScene.cpp @@ -0,0 +1,127 @@ + +#include "EndGameSubScene.h" +#include "EndGameSubScript.h" + +#include "../../Config.h" +#include "../ButtonSubScene.h" +#include "../FloatingWindowSubScene.h" + +#include <string> + +#include <crepe/api/BehaviorScript.h> +#include <crepe/api/GameObject.h> +#include <crepe/api/Text.h> +#include <crepe/types.h> + +using namespace crepe; +using namespace std; + +void EndGameSubScene::create(Scene & scn) { + + const std::string TAG = "end_game_tag"; + GameObject script = scn.new_object("script"); + script.add_component<BehaviorScript>().set_script<EndGameSubScript>(TAG); + + // Window + FloatingWindowSubScene window; + window.create( + scn, + FloatingWindowSubScene::Data { + .group_tag = TAG, + .width = 500, + .offset = {0, -50}, + .width_middle_offset = -2, + } + ); + + // Titel + const string TITEL_STRING = "GAME OVER"; + GameObject titel = scn.new_object("titel", TAG); + crepe::vec2 size = {200, (200.0f / TITEL_STRING.size()) * 2}; + titel.add_component<Text>( + size, FONT, + Text::Data { + .world_space = false, + .text_color = Color::WHITE, + }, + vec2 {0, -207} + FONTOFFSET, TITEL_STRING + ); + + const float Y_SPACING = 50; + const float Y_OFFSET = -100; + + // Gold gathered + const string GOLD_STRING = "gold:0"; + GameObject gold = scn.new_object("gold_endgame", TAG); + crepe::vec2 size_gold = {200, (200.0f / GOLD_STRING.size()) * 2}; + gold.add_component<Text>( + size_gold, FONT, + Text::Data { + .world_space = false, + .text_color = Color::GOLD, + }, + vec2 {0, Y_OFFSET} + FONTOFFSET, GOLD_STRING + ); + + // Distance + const string DISTANCE_STRING = "0M"; + GameObject distance = scn.new_object("distance_endgame", TAG); + crepe::vec2 size_distance = {200, (200.0f / DISTANCE_STRING.size()) * 2}; + distance.add_component<Text>( + size_distance, FONT, + Text::Data { + .world_space = false, + .text_color = Color::WHITE, + }, + vec2 {0, Y_SPACING + Y_OFFSET} + FONTOFFSET, DISTANCE_STRING + ); + + // Highscore + const string HIGHSCORE_STRING = "NEW HIGHSCORE"; + GameObject highscore = scn.new_object("highscore_endgame", "highscore_tag_end"); + crepe::vec2 size_highscore = {200, (200.0f / HIGHSCORE_STRING.size()) * 2}; + highscore + .add_component<Text>( + size_highscore, FONT, + Text::Data { + .world_space = false, + .text_color = Color::WHITE, + }, + vec2 {0, Y_SPACING * 2 + Y_OFFSET} + FONTOFFSET, HIGHSCORE_STRING + ) + .active + = false; + + // Buttons + vec2 button_position = {190, 190}; + ButtonSubScene button; + button.create( + scn, + ButtonSubScene::Data { + .text = "NEXT", + .text_width = 100, + .position = button_position, + .script_type = ButtonSubScene::ScriptSelect::NEXT, + .button_type = ButtonSubScene::ButtonSelect::NEXT, + .scale = 0.6, + .worldspace = false, + .tag = TAG, + .sorting_layer_offset = 20, + } + ); + + button.create( + scn, + ButtonSubScene::Data { + .text = "REPLAY", + .text_width = 150, + .position = {-button_position.x, button_position.y}, + .script_type = ButtonSubScene::ScriptSelect::REPLAY, + .button_type = ButtonSubScene::ButtonSelect::BACK, + .scale = 0.6, + .worldspace = false, + .tag = TAG, + .sorting_layer_offset = 20, + } + ); +} diff --git a/game/menus/endgame/EndGameSubScene.h b/game/menus/endgame/EndGameSubScene.h new file mode 100644 index 0000000..204f3b7 --- /dev/null +++ b/game/menus/endgame/EndGameSubScene.h @@ -0,0 +1,9 @@ +#pragma once + +#include <crepe/api/Scene.h> + +class EndGameSubScene { + +public: + void create(crepe::Scene & scn); +}; diff --git a/game/menus/endgame/EndGameSubScript.cpp b/game/menus/endgame/EndGameSubScript.cpp new file mode 100644 index 0000000..6793f3e --- /dev/null +++ b/game/menus/endgame/EndGameSubScript.cpp @@ -0,0 +1,101 @@ +#include "EndGameSubScript.h" + +#include "../../Config.h" +#include "../../Events.h" +#include "../ButtonReplaySubScript.h" +#include "../IFloatingWindowScript.h" +#include "ValueBroker.h" +#include "manager/SaveManager.h" + +#include <string> + +#include <crepe/api/Button.h> +#include <crepe/api/Sprite.h> +#include <crepe/api/Text.h> +#include <crepe/types.h> + +using namespace crepe; + +EndGameSubScript::EndGameSubScript(const std::string & tag) { this->tag = tag; } + +void EndGameSubScript::init() { + this->disable_all(); + this->subscribe<EndGameEvent>([this](const EndGameEvent e) { return this->enable_all(); }); + this->subscribe<EndGameEvent>([this](const EndGameEvent e) { + return this->reset_timescale(); + }); + this->subscribe<ShowScoreEvent>([this](const ShowScoreEvent e) { + return this->showscore(); + }); +} + +bool EndGameSubScript::disable_all() { + IFloatingWindowScript::disable_all_sprites(); + RefVector<Button> buttons = this->get_components_by_tag<Button>(this->tag); + for (Button & button : buttons) { + button.active = false; + } + RefVector<Text> texts = this->get_components_by_tag<Text>(this->tag); + for (Text & text : texts) { + text.active = false; + } + return false; +} + +bool EndGameSubScript::enable_all() { + IFloatingWindowScript::enable_all_sprites(); + RefVector<Button> buttons = this->get_components_by_tag<Button>(this->tag); + for (Button & button : buttons) { + button.active = true; + } + RefVector<Text> texts = this->get_components_by_tag<Text>(this->tag); + for (Text & text : texts) { + text.active = true; + } + return false; +} + +bool EndGameSubScript::reset_timescale() { + this->get_loop_timer().set_time_scale(1); + return false; +} + +bool EndGameSubScript::showscore() { + // Gather text + Text & coins_text = this->get_components_by_name<Text>("gold_endgame").front().get(); + Text & distance_text + = this->get_components_by_name<Text>("distance_endgame").front().get(); + Text & highscore_text + = this->get_components_by_name<Text>("highscore_endgame").front().get(); + highscore_text.active = false; + + // Gather saved data + SaveManager & savemgr = this->get_save_manager(); + ValueBroker<std::string> coins = savemgr.get<std::string>(TOTAL_COINS_RUN, "0"); + ValueBroker<std::string> distance = savemgr.get<std::string>(DISTANCE_RUN, "0"); + int distance_run = savemgr.get<int>(DISTANCE_RUN, 0).get(); + int distance_game = savemgr.get<int>(DISTANCE_GAME, 0).get(); + + // Show highscore + if (distance_run > distance_game) highscore_text.active = true; + + const float CHAR_SIZE_DIS = 20; + // Show distance + std::string distance_string = "DISTANCE:" + distance.get(); + distance_text.text = distance_string; + crepe::vec2 size_distance + = {CHAR_SIZE_DIS * distance_string.size(), + (CHAR_SIZE_DIS * distance_string.size() / distance_string.size()) * 2}; + distance_text.dimensions = size_distance; + + const float CHAR_SIZE_COIN = 16; + // Show coins + std::string coins_string = "Coins:" + coins.get(); + coins_text.text = coins_string; + crepe::vec2 size_coins + = {CHAR_SIZE_COIN * coins_string.size(), + (CHAR_SIZE_COIN * coins_string.size() / coins_string.size()) * 2}; + coins_text.dimensions = size_coins; + + return false; +} diff --git a/game/menus/endgame/EndGameSubScript.h b/game/menus/endgame/EndGameSubScript.h new file mode 100644 index 0000000..c25f6ca --- /dev/null +++ b/game/menus/endgame/EndGameSubScript.h @@ -0,0 +1,18 @@ +#pragma once + +#include "../IFloatingWindowScript.h" + +#include <crepe/api/Event.h> +#include <crepe/api/Script.h> + +struct ShowScoreEvent : public crepe::Event {}; + +class EndGameSubScript : public IFloatingWindowScript { +public: + EndGameSubScript(const std::string & tag); + void init() override; + bool disable_all(); + bool enable_all(); + bool reset_timescale(); + bool showscore(); +}; |