diff options
Diffstat (limited to 'game/hud')
-rw-r--r-- | game/hud/HudConfig.h | 27 | ||||
-rw-r--r-- | game/hud/HudScript.cpp | 40 | ||||
-rw-r--r-- | game/hud/HudScript.h | 12 | ||||
-rw-r--r-- | game/hud/HudSubScene.cpp | 36 | ||||
-rw-r--r-- | game/hud/HudSubScene.h | 8 | ||||
-rw-r--r-- | game/hud/SpeedScript.cpp | 35 | ||||
-rw-r--r-- | game/hud/SpeedScript.h | 14 |
7 files changed, 172 insertions, 0 deletions
diff --git a/game/hud/HudConfig.h b/game/hud/HudConfig.h new file mode 100644 index 0000000..5972f0a --- /dev/null +++ b/game/hud/HudConfig.h @@ -0,0 +1,27 @@ +#pragma once +#include "types.h" + +static constexpr crepe::vec2 TOP_LEFT = {-530,-230}; +static constexpr const char* HUD_DISTANCE = "hud_distance"; +static constexpr const char* HUD_BEST = "hud_best"; +static constexpr const char* HUD_COINS = "hud_coins"; + +// Distance +static constexpr const char* DISTANCE_PLACEHOLDER = "0000m"; +static constexpr const char* DISTANCE_UNIT = "m"; +static constexpr int DISTANCE_LENGTH = 5; +static constexpr float DISTANCE_CHAR_WIDTH = 12; +static constexpr float STEP_SIZE_DISTANCE = 100; + +// BEST +static constexpr const char* BEST = "BEST:"; +static constexpr int BEST_LENGTH = 5; +static constexpr float BEST_CHAR_WIDTH = 10; +static constexpr crepe::vec2 BEST_OFFSET = {0,25}; + +// COINS +static constexpr const char* COINS = "0000"; +static constexpr int COINS_LENGTH = 4; +static constexpr float COINS_CHAR_WIDTH = 10; +static constexpr crepe::vec2 COINS_OFFSET = {0,50}; +
\ No newline at end of file diff --git a/game/hud/HudScript.cpp b/game/hud/HudScript.cpp new file mode 100644 index 0000000..deeea14 --- /dev/null +++ b/game/hud/HudScript.cpp @@ -0,0 +1,40 @@ +#include "HudScript.h" +#include "api/Text.h" +#include "api/Transform.h" +#include "manager/SaveManager.h" +#include "../Config.h" +#include "HudConfig.h" +#include <climits> + +using namespace crepe; +using namespace std; + +void HudScript::init() { + savemgr = &this->get_save_manager(); + savemgr->set(TOTAL_COINS_RUN,0); + Text & txt = this->get_components_by_name<Text>(HUD_BEST).front(); + string record = BEST+to_string(savemgr->get<int>(DISTANCE_GAME,0).get())+DISTANCE_UNIT; + txt.text = record; + txt.dimensions = {BEST_CHAR_WIDTH*record.size(),(BEST_CHAR_WIDTH)*2}; + txt.offset = TOP_LEFT+FONTOFFSET+BEST_OFFSET + vec2{record.size() * BEST_CHAR_WIDTH/2,0}; +} + +void HudScript::frame_update(crepe::duration_t dt) { + + // string number = std::to_string(savemgr->get<int>(DISTANCE_RUN,0).get()); + + // Distance + 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; + 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}; + + // Coins + Text & txt_co = this->get_components_by_name<Text>(HUD_COINS).front(); + string amount_of_coins = to_string(savemgr->get<int>(TOTAL_COINS_RUN,0).get()); + 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}; +} diff --git a/game/hud/HudScript.h b/game/hud/HudScript.h new file mode 100644 index 0000000..aa92582 --- /dev/null +++ b/game/hud/HudScript.h @@ -0,0 +1,12 @@ +#pragma once + +#include "api/Script.h" +#include "manager/SaveManager.h" + +class HudScript : public crepe::Script { +public: + void init() override; + void frame_update(crepe::duration_t dt) override; +private: + crepe::SaveManager* savemgr; +}; diff --git a/game/hud/HudSubScene.cpp b/game/hud/HudSubScene.cpp new file mode 100644 index 0000000..126f933 --- /dev/null +++ b/game/hud/HudSubScene.cpp @@ -0,0 +1,36 @@ +#include "HudSubScene.h" +#include "api/GameObject.h" +#include "api/Text.h" +#include "../Config.h" +#include "HudConfig.h" + +using namespace crepe; +using namespace std; + +void HudSubScene::create(Scene & scn){ + + // Distance + GameObject hud_dis = scn.new_object(HUD_DISTANCE); + + crepe::vec2 size_distance = {DISTANCE_CHAR_WIDTH*DISTANCE_LENGTH,(DISTANCE_CHAR_WIDTH)*2}; + hud_dis.add_component<Text>(size_distance, FONT,Text::Data{ + .world_space = false, + .text_color = Color::WHITE, + }, TOP_LEFT+FONTOFFSET + vec2{DISTANCE_LENGTH * DISTANCE_CHAR_WIDTH/2,0}, DISTANCE_PLACEHOLDER); + + // Best + GameObject hud_best = scn.new_object(HUD_BEST); + crepe::vec2 size_best = {BEST_CHAR_WIDTH*BEST_LENGTH,(BEST_CHAR_WIDTH)*2}; + hud_best.add_component<Text>(size_best, FONT,Text::Data{ + .world_space = false, + .text_color = Color::GREY, + }, TOP_LEFT+FONTOFFSET+BEST_OFFSET + vec2{BEST_LENGTH * BEST_CHAR_WIDTH/2,0}, BEST); + + // Coins + GameObject hud_coin = scn.new_object(HUD_COINS); + crepe::vec2 size = {COINS_CHAR_WIDTH*COINS_LENGTH,(COINS_CHAR_WIDTH)*2}; + hud_coin.add_component<Text>(size, FONT,Text::Data{ + .world_space = false, + .text_color = Color::YELLOW, + }, TOP_LEFT+FONTOFFSET+COINS_OFFSET + vec2{COINS_LENGTH * COINS_CHAR_WIDTH/2,0}, COINS); +} diff --git a/game/hud/HudSubScene.h b/game/hud/HudSubScene.h new file mode 100644 index 0000000..711a34d --- /dev/null +++ b/game/hud/HudSubScene.h @@ -0,0 +1,8 @@ +#pragma once + +#include "api/Scene.h" +class HudSubScene +{ +public: + void create(crepe::Scene & scn); +}; diff --git a/game/hud/SpeedScript.cpp b/game/hud/SpeedScript.cpp new file mode 100644 index 0000000..69534d9 --- /dev/null +++ b/game/hud/SpeedScript.cpp @@ -0,0 +1,35 @@ +#include "SpeedScript.h" +#include "api/Event.h" +#include "api/KeyCodes.h" +#include "manager/LoopTimerManager.h" + +using namespace crepe; +using namespace std; + +void SpeedScript::init() { + this->subscribe<KeyPressEvent>([this](const KeyPressEvent & ev) -> bool { + if(ev.key != Keycode::HOME) return false; + LoopTimerManager & lp = this->get_loop_timer(); + this->toggle = !this->toggle; + if(this->toggle) + { + this->timescale = lp.get_time_scale(); + lp.set_time_scale(0); + } + else { + lp.set_time_scale(this->timescale); + } + + return true; + }); +} + +void SpeedScript::fixed_update(crepe::duration_t dt){ + LoopTimerManager & lp = this->get_loop_timer(); + if(this->get_key_state(Keycode::PAGE_UP)){ + lp.set_time_scale(lp.get_time_scale()+0.1); + } + if(this->get_key_state(Keycode::PAGE_DOWN)){ + lp.set_time_scale(lp.get_time_scale()-0.1); + } +} diff --git a/game/hud/SpeedScript.h b/game/hud/SpeedScript.h new file mode 100644 index 0000000..8bd7271 --- /dev/null +++ b/game/hud/SpeedScript.h @@ -0,0 +1,14 @@ +#pragma once + +#include "api/Script.h" +#include "manager/SaveManager.h" + +class SpeedScript : public crepe::Script { +public: + void init() override; + void fixed_update(crepe::duration_t dt) override; +private: + crepe::SaveManager* savemgr; + bool toggle = true; + float timescale = 1; +}; |