This commit is contained in:
brxxh 2025-07-02 11:46:43 +02:00
parent b6805f7164
commit 47bfead825

View file

@ -22,6 +22,11 @@
#include <tuple> #include <tuple>
#include <unordered_map> #include <unordered_map>
// @ToDo: Introduce a struct that automates a sprite's scale. Right now we are
// doing this all by hand by doing IMAGE_SIZE / WINDOW_SIZE.
// @ToDo: Introduce Delta-Time for each update function?
namespace TappyPlane { namespace TappyPlane {
using SpriteName = std::tuple<std::string_view, std::string_view>; using SpriteName = std::tuple<std::string_view, std::string_view>;
@ -66,10 +71,12 @@ struct Background {
sprites = {paradiso::Sprite{ sprites = {paradiso::Sprite{
.bitmap = sprite_map["background"], .bitmap = sprite_map["background"],
.pivot = {paradiso::Vector2<float>::make(0.0f, 0.0f)}, .pivot = {paradiso::Vector2<float>::make(0.0f, 0.0f)},
// The background sprite is as big as the window.
.scale = {paradiso::Vector2<float>::make(1.0f, 1.0f)}}, .scale = {paradiso::Vector2<float>::make(1.0f, 1.0f)}},
paradiso::Sprite{ paradiso::Sprite{
.bitmap = sprite_map["background"], .bitmap = sprite_map["background"],
.pivot = {paradiso::Vector2<float>::make(2.0f, 0.0f)}, .pivot = {paradiso::Vector2<float>::make(2.0f, 0.0f)},
// The background sprite is as big as the window.
.scale = {paradiso::Vector2<float>::make(1.0f, 1.0f)}}}; .scale = {paradiso::Vector2<float>::make(1.0f, 1.0f)}}};
} }
@ -308,6 +315,7 @@ struct Plane {
static constexpr paradiso::Vector2<float> INIT_PIVOT{ static constexpr paradiso::Vector2<float> INIT_PIVOT{
paradiso::Vector2<float>::make(0.0f, INIT_POSITION_Y)}; paradiso::Vector2<float>::make(0.0f, INIT_POSITION_Y)};
// Every 10th frame.
static const unsigned int ANIM_INTERVAL = 10; static const unsigned int ANIM_INTERVAL = 10;
// The image's size is 88x73 px. // The image's size is 88x73 px.
@ -636,7 +644,7 @@ struct App {
}; };
for (const auto& [name, filename] : assets) { for (const auto& [name, filename] : assets) {
std::print("{} : {} -> ", name, filename); std::print("{:<20} : {:<30} -> ", name, filename);
auto bitmap = paradiso::BitmapIO::get().load(filename); auto bitmap = paradiso::BitmapIO::get().load(filename);
app.sprites[name] = bitmap; app.sprites[name] = bitmap;
@ -708,7 +716,8 @@ struct App {
return false; return false;
} }
if (game_state == GameState::Start) { switch (game_state) {
case GameState::Start:
if (window.keyboard_input().size() && if (window.keyboard_input().size() &&
window.keyboard_input().top().key == ACTION_KEY && window.keyboard_input().top().key == ACTION_KEY &&
window.keyboard_input().top().action == 1) { window.keyboard_input().top().action == 1) {
@ -724,7 +733,8 @@ struct App {
plane.update_animation(); // Only the animation. plane.update_animation(); // Only the animation.
ground.update(); ground.update();
start_ui.update(); start_ui.update();
} else if (game_state == GameState::Playing) { break;
case GameState::Playing:
background.update(); background.update();
ground.update(); ground.update();
rocks1.update(); rocks1.update();
@ -740,7 +750,8 @@ struct App {
plane.aabb.is_colliding_with(ground.aabb)) { plane.aabb.is_colliding_with(ground.aabb)) {
game_state = GameState::GameOver; game_state = GameState::GameOver;
} }
} else if (game_state == GameState::GameOver) { break;
case GameState::GameOver:
if (window.keyboard_input().size() && if (window.keyboard_input().size() &&
window.keyboard_input().top().key == ACTION_KEY) { window.keyboard_input().top().key == ACTION_KEY) {
@ -752,6 +763,7 @@ struct App {
plane.reset(); plane.reset();
game_state = GameState::Start; game_state = GameState::Start;
} }
break;
} }
context.set_viewport( context.set_viewport(
@ -773,7 +785,18 @@ struct App {
game_over_ui.draw(shader); game_over_ui.draw(shader);
} }
// Einen kurzen Moment warten, um auf 60 FPS zu kommen. //
// Einen kurzen Moment warten, um FRAME_RATE (siehe oben) zu
// erreichen.
//
// Wichtig: Das beeinflusst auch die allgemeine
// Geschwindigkeit des Spiels, da das Aktualisieren des Game-States
// nicht unabhängig von der Frame-Rate läuft.
//
// Man könnte Delta-Time einführen oder das Aktualisieren des
// Game-States auf eine bestimmte Geschwindigkeit (Tick-Speed)
// bringen und zwischen den Frames interpolieren.
//
auto t2 = std::chrono::high_resolution_clock::now(); auto t2 = std::chrono::high_resolution_clock::now();
auto duration = auto duration =
std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1); std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1);