aboutsummaryrefslogtreecommitdiff
path: root/game/hud
diff options
context:
space:
mode:
authorMax-001 <maxsmits21@kpnmail.nl>2025-01-08 09:52:00 +0100
committerMax-001 <maxsmits21@kpnmail.nl>2025-01-08 09:52:00 +0100
commitb1824253875901b4fb6f4c5fee72aea6d807d370 (patch)
tree8acc156e68d48022f453dcf06b541f77a5803484 /game/hud
parente9082de7d114ce024a884cac38c545c99de4026a (diff)
parente75ca1c947f3cde19bebf15049732bc069c6e913 (diff)
Merge remote-tracking branch 'origin/master' into max/game2
Diffstat (limited to 'game/hud')
-rw-r--r--game/hud/HudConfig.h33
-rw-r--r--game/hud/HudScript.cpp95
-rw-r--r--game/hud/HudScript.h25
-rw-r--r--game/hud/HudSubScene.cpp68
-rw-r--r--game/hud/HudSubScene.h8
-rw-r--r--game/hud/SpeedScript.cpp34
-rw-r--r--game/hud/SpeedScript.h15
7 files changed, 278 insertions, 0 deletions
diff --git a/game/hud/HudConfig.h b/game/hud/HudConfig.h
new file mode 100644
index 0000000..facc298
--- /dev/null
+++ b/game/hud/HudConfig.h
@@ -0,0 +1,33 @@
+#pragma once
+#include <crepe/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};
diff --git a/game/hud/HudScript.cpp b/game/hud/HudScript.cpp
new file mode 100644
index 0000000..8f77706
--- /dev/null
+++ b/game/hud/HudScript.cpp
@@ -0,0 +1,95 @@
+#include "HudScript.h"
+#include "HudConfig.h"
+
+#include "../Config.h"
+#include "../Events.h"
+
+#include <climits>
+
+#include <crepe/api/Text.h>
+#include <crepe/api/Transform.h>
+#include <crepe/manager/SaveManager.h>
+
+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);
+ });
+ this->subscribe<EndGameEvent>([this](const EndGameEvent e) -> bool {
+ return this->save();
+ });
+}
+
+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;
+ this->distance_st = distance;
+ 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);
+ this->coin_amount_st = amount_of_coins;
+ 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;
+}
+
+bool HudScript::save() {
+ SaveManager & savemgr = this->get_save_manager();
+ savemgr.set(TOTAL_COINS_RUN, this->coin_amount);
+ savemgr.set(DISTANCE_RUN, this->distance_st);
+ return false;
+}
diff --git a/game/hud/HudScript.h b/game/hud/HudScript.h
new file mode 100644
index 0000000..2b789db
--- /dev/null
+++ b/game/hud/HudScript.h
@@ -0,0 +1,25 @@
+#pragma once
+
+#include <crepe/api/Event.h>
+#include <crepe/api/Script.h>
+#include <crepe/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);
+ bool save();
+
+private:
+ crepe::SaveManager * savemgr;
+ bool show_fps = false;
+ int coin_amount = 0;
+ std::string coin_amount_st = "";
+ std::string distance_st = "";
+};
diff --git a/game/hud/HudSubScene.cpp b/game/hud/HudSubScene.cpp
new file mode 100644
index 0000000..ca81614
--- /dev/null
+++ b/game/hud/HudSubScene.cpp
@@ -0,0 +1,68 @@
+#include "HudSubScene.h"
+#include "HudConfig.h"
+
+#include "../Config.h"
+
+#include <crepe/api/GameObject.h>
+#include <crepe/api/Text.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..0cd368e
--- /dev/null
+++ b/game/hud/HudSubScene.h
@@ -0,0 +1,8 @@
+#pragma once
+
+#include <crepe/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..d0a4dfe
--- /dev/null
+++ b/game/hud/SpeedScript.cpp
@@ -0,0 +1,34 @@
+#include "SpeedScript.h"
+
+#include <crepe/api/Event.h>
+#include <crepe/api/KeyCodes.h>
+#include <crepe/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..6c15a89
--- /dev/null
+++ b/game/hud/SpeedScript.h
@@ -0,0 +1,15 @@
+#pragma once
+
+#include <crepe/api/Script.h>
+#include <crepe/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;
+};