diff options
| author | JAROWMR <jarorutjes07@gmail.com> | 2025-01-05 16:36:38 +0100 | 
|---|---|---|
| committer | JAROWMR <jarorutjes07@gmail.com> | 2025-01-05 16:36:38 +0100 | 
| commit | 2726785d64aa117aff0791d4591046c442709aac (patch) | |
| tree | ce5b7d2fb240e87597d70e1f1be544e9672212e3 | |
| parent | ba170d00586ab261e015cc2febbb43f9aa7ae43e (diff) | |
improved hud
| -rw-r--r-- | game/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | game/GameScene.cpp | 3 | ||||
| -rw-r--r-- | game/hud/HudConfig.h | 8 | ||||
| -rw-r--r-- | game/hud/HudScript.cpp | 7 | ||||
| -rw-r--r-- | game/hud/HudSubScene.cpp | 17 | ||||
| -rw-r--r-- | game/hud/SpeedScript.cpp | 28 | ||||
| -rw-r--r-- | game/hud/SpeedScript.h | 13 | 
7 files changed, 65 insertions, 12 deletions
| 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 <cmath>  #include <crepe/api/Animator.h> @@ -43,6 +44,8 @@ void GameScene::load_scene() {  	camera.add_component<BehaviorScript>().set_script<MoveCameraManualyScript>();  	camera.add_component<BehaviorScript>().set_script<CoinSystemScript>();  	camera.add_component<BehaviorScript>().set_script<HudScript>(); +	camera.add_component<BehaviorScript>().set_script<SpeedScript>(); +	  	camera.add_component<Rigidbody>(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 <climits>  using namespace crepe;  using namespace std; @@ -14,6 +15,8 @@ void HudScript::init() {  	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) { @@ -25,9 +28,13 @@ void HudScript::frame_update(crepe::duration_t dt) {  	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/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<Text>(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<Text>(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<Text>(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; +}; |