From ba170d00586ab261e015cc2febbb43f9aa7ae43e Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Sun, 5 Jan 2025 14:26:19 +0100 Subject: added hud --- game/hud/HudConfig.h | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 game/hud/HudConfig.h (limited to 'game/hud/HudConfig.h') diff --git a/game/hud/HudConfig.h b/game/hud/HudConfig.h new file mode 100644 index 0000000..91e3bfa --- /dev/null +++ b/game/hud/HudConfig.h @@ -0,0 +1,28 @@ +#pragma once +#include "types.h" + +static constexpr crepe::vec2 TOP_LEFT = {-500,-380}; +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_WIDTH = 60; +static constexpr float STEP_SIZE_DISTANCE = 100; + +// BEST +static constexpr const char* BEST = "BEST:"; +static constexpr int BEST_LENGTH = 5; +static constexpr float BEST_WIDTH = 40; +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_WIDTH = 40; +static constexpr crepe::vec2 COINS_OFFSET = {0,50}; + + \ No newline at end of file -- cgit v1.2.3 From 2726785d64aa117aff0791d4591046c442709aac Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Sun, 5 Jan 2025 16:36:38 +0100 Subject: improved hud --- game/CMakeLists.txt | 1 + game/GameScene.cpp | 3 +++ game/hud/HudConfig.h | 8 ++++---- game/hud/HudScript.cpp | 7 +++++++ game/hud/HudSubScene.cpp | 17 +++++++++-------- game/hud/SpeedScript.cpp | 28 ++++++++++++++++++++++++++++ game/hud/SpeedScript.h | 13 +++++++++++++ 7 files changed, 65 insertions(+), 12 deletions(-) create mode 100644 game/hud/SpeedScript.cpp create mode 100644 game/hud/SpeedScript.h (limited to 'game/hud/HudConfig.h') diff --git a/game/CMakeLists.txt b/game/CMakeLists.txt index 50c9b4d..e14e132 100644 --- a/game/CMakeLists.txt +++ b/game/CMakeLists.txt @@ -37,6 +37,7 @@ add_executable(main coins/CoinScript.cpp hud/HudSubScene.cpp hud/HudScript.cpp + hud/SpeedScript.cpp ) target_link_libraries(main PUBLIC crepe) diff --git a/game/GameScene.cpp b/game/GameScene.cpp index 2073a1e..57cb8c4 100644 --- a/game/GameScene.cpp +++ b/game/GameScene.cpp @@ -10,6 +10,7 @@ #include "background/BackgroundSubScene.h" #include "hud/HudScript.h" #include "hud/HudSubScene.h" +#include "hud/SpeedScript.h" #include #include @@ -43,6 +44,8 @@ void GameScene::load_scene() { camera.add_component().set_script(); camera.add_component().set_script(); camera.add_component().set_script(); + camera.add_component().set_script(); + camera.add_component(Rigidbody::Data{}); PlayerSubScene player(*this); diff --git a/game/hud/HudConfig.h b/game/hud/HudConfig.h index 91e3bfa..983a557 100644 --- a/game/hud/HudConfig.h +++ b/game/hud/HudConfig.h @@ -1,7 +1,7 @@ #pragma once #include "types.h" -static constexpr crepe::vec2 TOP_LEFT = {-500,-380}; +static constexpr crepe::vec2 TOP_LEFT = {-530,-380}; static constexpr const char* HUD_DISTANCE = "hud_distance"; static constexpr const char* HUD_BEST = "hud_best"; static constexpr const char* HUD_COINS = "hud_coins"; @@ -10,19 +10,19 @@ static constexpr const char* HUD_COINS = "hud_coins"; static constexpr const char* DISTANCE_PLACEHOLDER = "0000m"; static constexpr const char* DISTANCE_UNIT = "m"; static constexpr int DISTANCE_LENGTH = 5; -static constexpr float DISTANCE_WIDTH = 60; +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_WIDTH = 40; +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_WIDTH = 40; +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 index 42ff118..deeea14 100644 --- a/game/hud/HudScript.cpp +++ b/game/hud/HudScript.cpp @@ -4,6 +4,7 @@ #include "manager/SaveManager.h" #include "../Config.h" #include "HudConfig.h" +#include using namespace crepe; using namespace std; @@ -14,6 +15,8 @@ void HudScript::init() { Text & txt = this->get_components_by_name(HUD_BEST).front(); string record = BEST+to_string(savemgr->get(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) { @@ -25,9 +28,13 @@ void HudScript::frame_update(crepe::duration_t dt) { Transform & tf = this->get_components_by_name(PLAYER_NAME).front(); string distance = to_string(static_cast(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(HUD_COINS).front(); string amount_of_coins = to_string(savemgr->get(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/HudSubScene.cpp b/game/hud/HudSubScene.cpp index e76623d..126f933 100644 --- a/game/hud/HudSubScene.cpp +++ b/game/hud/HudSubScene.cpp @@ -11,25 +11,26 @@ void HudSubScene::create(Scene & scn){ // Distance GameObject hud_dis = scn.new_object(HUD_DISTANCE); - crepe::vec2 size_distance = {DISTANCE_WIDTH,(DISTANCE_WIDTH/DISTANCE_LENGTH)*2}; + + crepe::vec2 size_distance = {DISTANCE_CHAR_WIDTH*DISTANCE_LENGTH,(DISTANCE_CHAR_WIDTH)*2}; hud_dis.add_component(size_distance, FONT,Text::Data{ .world_space = false, .text_color = Color::WHITE, - }, TOP_LEFT+FONTOFFSET, DISTANCE_PLACEHOLDER); + }, 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_WIDTH,(BEST_WIDTH/BEST_LENGTH)*2}; + crepe::vec2 size_best = {BEST_CHAR_WIDTH*BEST_LENGTH,(BEST_CHAR_WIDTH)*2}; hud_best.add_component(size_best, FONT,Text::Data{ .world_space = false, - .text_color = Color::WHITE, - }, TOP_LEFT+FONTOFFSET+BEST_OFFSET, BEST); + .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_WIDTH,(COINS_WIDTH/COINS_LENGTH)*2}; + crepe::vec2 size = {COINS_CHAR_WIDTH*COINS_LENGTH,(COINS_CHAR_WIDTH)*2}; hud_coin.add_component(size, FONT,Text::Data{ .world_space = false, - .text_color = Color::WHITE, - }, TOP_LEFT+FONTOFFSET+COINS_OFFSET, COINS); + .text_color = Color::YELLOW, + }, TOP_LEFT+FONTOFFSET+COINS_OFFSET + vec2{COINS_LENGTH * COINS_CHAR_WIDTH/2,0}, COINS); } diff --git a/game/hud/SpeedScript.cpp b/game/hud/SpeedScript.cpp new file mode 100644 index 0000000..c0d927c --- /dev/null +++ b/game/hud/SpeedScript.cpp @@ -0,0 +1,28 @@ +#include "SpeedScript.h" +#include "api/KeyCodes.h" +#include "manager/LoopTimerManager.h" + +using namespace crepe; +using namespace std; + +void SpeedScript::fixed_update(crepe::duration_t dt){ + LoopTimerManager & lp = this->get_loop_timer(); + if(this->get_key_state(Keycode::HOME)){ + if(toggle) + { + this->timescale = lp.get_time_scale(); + lp.set_time_scale(0); + toggle = false; + } + else { + lp.set_time_scale(this->timescale); + toggle = true; + } + } + 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..1cc4368 --- /dev/null +++ b/game/hud/SpeedScript.h @@ -0,0 +1,13 @@ +#pragma once + +#include "api/Script.h" +#include "manager/SaveManager.h" + +class SpeedScript : public crepe::Script { +public: + void fixed_update(crepe::duration_t dt) override; +private: + crepe::SaveManager* savemgr; + bool toggle = true; + float timescale = 1; +}; -- cgit v1.2.3 From 04a37c297f9f4e22adcee1f4a3cc73cde4c3c7b0 Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Mon, 6 Jan 2025 10:42:44 +0100 Subject: changed game camera --- game/hud/HudConfig.h | 2 +- src/crepe/api/Config.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'game/hud/HudConfig.h') diff --git a/game/hud/HudConfig.h b/game/hud/HudConfig.h index 983a557..1d2ed89 100644 --- a/game/hud/HudConfig.h +++ b/game/hud/HudConfig.h @@ -1,7 +1,7 @@ #pragma once #include "types.h" -static constexpr crepe::vec2 TOP_LEFT = {-530,-380}; +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"; diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h index 3e9c9ef..9c226a3 100644 --- a/src/crepe/api/Config.h +++ b/src/crepe/api/Config.h @@ -85,7 +85,7 @@ struct Config final { * This config option is the font size at which all fonts will be loaded initially. * */ - unsigned int size = 40; + unsigned int size = 500; } font; //! Configuration for click tolerance. struct { -- cgit v1.2.3 From a216cfc457cdde754c5673e9423d00147a2f1574 Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Mon, 6 Jan 2025 13:34:27 +0100 Subject: fps in hud --- game/Config.h | 7 +++---- game/coins/CoinSubScene.cpp | 6 +++--- game/hud/HudConfig.h | 7 +++++++ game/hud/HudScript.cpp | 13 +++++++++++-- game/hud/HudSubScene.cpp | 12 ++++++++++-- 5 files changed, 34 insertions(+), 11 deletions(-) (limited to 'game/hud/HudConfig.h') diff --git a/game/Config.h b/game/Config.h index 81dd7ea..c09a965 100644 --- a/game/Config.h +++ b/game/Config.h @@ -5,6 +5,7 @@ static constexpr int SORT_IN_LAY_BACK_BACKGROUND = 3; // For all scenes static constexpr int SORT_IN_LAY_BACKGROUND = 4; // For all scenes static constexpr int SORT_IN_LAY_FORE_BACKGROUND = 5; // For all scenes static constexpr int SORT_IN_LAY_PARTICLES_BACKGROUND = 6; // For all scenes +static constexpr int SORT_IN_LAY_COINS = 7; // Only for GameScene static constexpr int SORT_IN_LAY_OBSTACLES = 8; // Only for GameScene static constexpr int SORT_IN_LAY_PLAYER = 10; // Only for GameScene static constexpr int SORT_IN_LAY_PARTICLES_FOREGROUND = 15; // Only for GameScene @@ -22,14 +23,12 @@ static constexpr int GAME_HEIGHT = 800; // In game units static constexpr int VIEWPORT_X = 1100; // In game units // 'GAME_HEIGHT' (below) should be replaced by '500' when game development is finished -static constexpr int VIEWPORT_Y = GAME_HEIGHT; // In game units +static constexpr int VIEWPORT_Y = 500; // In game units // Font settings static constexpr const char* FONT = "Jetpackia"; static constexpr crepe::vec2 FONTOFFSET = {0,0}; -// Save data - // Amount of coins in game static constexpr const char* TOTAL_COINS_GAME = "total_coins_game"; @@ -40,7 +39,7 @@ static constexpr const char* TOTAL_COINS_RUN = "total_coins_run"; static constexpr const char* DISTANCE_GAME = "distance_game"; static constexpr const char* DISTANCE_RUN = "distance_run"; -// Global tags and names +// Player config static constexpr const char* PLAYER_NAME = "player"; static constexpr int PLAYER_SPEED = 7500; // In game units static constexpr int PLAYER_GRAVITY_SCALE = 60; // In game units diff --git a/game/coins/CoinSubScene.cpp b/game/coins/CoinSubScene.cpp index 58f74ef..604af78 100644 --- a/game/coins/CoinSubScene.cpp +++ b/game/coins/CoinSubScene.cpp @@ -26,10 +26,10 @@ int CoinSubScene::create(Scene & scn){ .kinematic_collision = false, .collision_layers = {COLL_LAY_PLAYER}, }); - coin.add_component(size.x / 2).active = false; + coin.add_component((size.x / 2)-3).active = false; crepe::OptionalRef coin_sprite = coin.add_component(Asset{"asset/coin/coin1_TVOS.png"}, Sprite::Data{ - .sorting_in_layer = 100, - .order_in_layer = 100, + .sorting_in_layer = SORT_IN_LAY_COINS, + .order_in_layer = 0, .size = size, }); coin_sprite->active = false; diff --git a/game/hud/HudConfig.h b/game/hud/HudConfig.h index 5972f0a..e3497fb 100644 --- a/game/hud/HudConfig.h +++ b/game/hud/HudConfig.h @@ -5,6 +5,7 @@ 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"; @@ -24,4 +25,10 @@ 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 index deeea14..71c84a1 100644 --- a/game/hud/HudScript.cpp +++ b/game/hud/HudScript.cpp @@ -21,8 +21,6 @@ void HudScript::init() { void HudScript::frame_update(crepe::duration_t dt) { - // string number = std::to_string(savemgr->get(DISTANCE_RUN,0).get()); - // Distance Text & txt_dt = this->get_components_by_name(HUD_DISTANCE).front(); Transform & tf = this->get_components_by_name(PLAYER_NAME).front(); @@ -37,4 +35,15 @@ void HudScript::frame_update(crepe::duration_t dt) { 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(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; } diff --git a/game/hud/HudSubScene.cpp b/game/hud/HudSubScene.cpp index 126f933..dd36d1c 100644 --- a/game/hud/HudSubScene.cpp +++ b/game/hud/HudSubScene.cpp @@ -28,9 +28,17 @@ void HudSubScene::create(Scene & scn){ // 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(size, FONT,Text::Data{ + crepe::vec2 size_coin = {COINS_CHAR_WIDTH*COINS_LENGTH,(COINS_CHAR_WIDTH)*2}; + hud_coin.add_component(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(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); } -- cgit v1.2.3