diff options
Diffstat (limited to 'game/hud')
-rw-r--r-- | game/hud/HudConfig.h | 34 | ||||
-rw-r--r-- | game/hud/HudScript.cpp | 73 | ||||
-rw-r--r-- | game/hud/HudScript.h | 21 | ||||
-rw-r--r-- | game/hud/HudSubScene.cpp | 44 | ||||
-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, 229 insertions, 0 deletions
diff --git a/game/hud/HudConfig.h b/game/hud/HudConfig.h new file mode 100644 index 0000000..e3497fb --- /dev/null +++ b/game/hud/HudConfig.h @@ -0,0 +1,34 @@ +#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"; +static constexpr const char* HUD_FPS = "hud_fps"; + +// 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}; + +// FPS +static constexpr const char* FPS = "00"; +static constexpr int FPS_LENGTH = 2; +static constexpr float FPS_CHAR_WIDTH = 10; +static constexpr crepe::vec2 FPS_OFFSET = {1030,0}; +
\ No newline at end of file diff --git a/game/hud/HudScript.cpp b/game/hud/HudScript.cpp new file mode 100644 index 0000000..30a5c15 --- /dev/null +++ b/game/hud/HudScript.cpp @@ -0,0 +1,73 @@ +#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}; + + 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);}); +} + +bool HudScript::toggle_fps(crepe::KeyPressEvent ev){ + if(ev.key != Keycode::END) return false; + Text & txt_fps = this->get_components_by_name<Text>(HUD_FPS).front(); + this->show_fps = !this->show_fps; + if(this->show_fps) + { + txt_fps.active = true; + } + else { + txt_fps.active = false; + } + return true; +} + +void HudScript::frame_update(crepe::duration_t dt) { + + // 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(this->coin_amount); + 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}; + + // FPS + Text & txt_fps = this->get_components_by_name<Text>(HUD_FPS).front(); + float fps = this->get_loop_timer().get_fps(); + string fps_amount = to_string(this->get_loop_timer().get_fps()); + txt_fps.text = fps_amount; + txt_fps.dimensions = {FPS_CHAR_WIDTH*fps_amount.size(),(FPS_CHAR_WIDTH)*2}; + txt_fps.offset = TOP_LEFT+FONTOFFSET+FPS_OFFSET + vec2{fps_amount.size() * FPS_CHAR_WIDTH/2,0}; + if(fps >= 30) txt_fps.data.text_color = Color::YELLOW; + if(fps >= 50) txt_fps.data.text_color = Color::GREEN; + if(fps < 30) txt_fps.data.text_color = Color::RED; +} + + +bool HudScript::get_coin(const GetCoinEvent e){ + this->coin_amount = e.amount_of_coins; + return true; +} + diff --git a/game/hud/HudScript.h b/game/hud/HudScript.h new file mode 100644 index 0000000..d780d4b --- /dev/null +++ b/game/hud/HudScript.h @@ -0,0 +1,21 @@ +#pragma once + +#include "api/Event.h" +#include "api/Script.h" +#include "manager/SaveManager.h" + +struct GetCoinEvent : public crepe::Event { + int amount_of_coins; +}; + +class HudScript : public crepe::Script { +public: + void init() override; + void frame_update(crepe::duration_t dt) override; + bool get_coin(const GetCoinEvent e); + bool toggle_fps(crepe::KeyPressEvent ev); +private: + crepe::SaveManager* savemgr; + bool show_fps = false; + int coin_amount = 0; +}; diff --git a/game/hud/HudSubScene.cpp b/game/hud/HudSubScene.cpp new file mode 100644 index 0000000..4995624 --- /dev/null +++ b/game/hud/HudSubScene.cpp @@ -0,0 +1,44 @@ +#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_coin = {COINS_CHAR_WIDTH*COINS_LENGTH,(COINS_CHAR_WIDTH)*2}; + hud_coin.add_component<Text>(size_coin, FONT,Text::Data{ + .world_space = false, + .text_color = Color::YELLOW, + }, TOP_LEFT+FONTOFFSET+COINS_OFFSET + vec2{COINS_LENGTH * COINS_CHAR_WIDTH/2,0}, COINS); + + // Fps + GameObject hud_fps = scn.new_object(HUD_FPS); + crepe::vec2 size_fps = {FPS_CHAR_WIDTH*FPS_LENGTH,(FPS_CHAR_WIDTH)*2}; + hud_fps.add_component<Text>(size_fps, FONT,Text::Data{ + .world_space = false, + .text_color = Color::GREEN, + }, TOP_LEFT+FONTOFFSET+FPS_OFFSET + vec2{FPS_LENGTH * FPS_CHAR_WIDTH/2,0}, FPS).active = false; +} 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; +}; |