From d8e55e1370cc23ad7966a55ee5bf3f8861085b87 Mon Sep 17 00:00:00 2001 From: theoleuthardt Date: Thu, 13 Mar 2025 14:07:06 +0100 Subject: [PATCH] feat: dino walking animation --- src/Dino.cpp | 19 ++++++++++++++++--- src/Dino.hpp | 4 ++++ src/Game.cpp | 5 ++++- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/Dino.cpp b/src/Dino.cpp index 0ea0a78..022f523 100644 --- a/src/Dino.cpp +++ b/src/Dino.cpp @@ -1,8 +1,10 @@ #include "Dino.hpp" -Dino::Dino(Texture2D texture) : texture(texture), velocityY(0), isJumping(false) { +Dino::Dino(Texture2D texture) + : texture(texture), velocityY(0), isJumping(false), + frameCount(3), currentFrame(0), frameTime(0.1f), frameTimer(0.0f) { rect = {50.0f, static_cast(320 - texture.height), - static_cast(texture.width), static_cast(texture.height)}; + static_cast(texture.width / frameCount), static_cast(texture.height)}; } void Dino::Jump() { @@ -21,10 +23,21 @@ void Dino::Update() { velocityY = 0; isJumping = false; } + + if (!isJumping) { + frameTimer += GetFrameTime(); + if (frameTimer >= frameTime) { + frameTimer = 0.0f; + currentFrame = (currentFrame + 1) % frameCount; + } + } } void Dino::Draw() { - DrawTexture(texture, rect.x, rect.y, WHITE); + Rectangle source = {static_cast(currentFrame * (texture.width / frameCount)), 0, + static_cast(texture.width / frameCount), static_cast(texture.height)}; + + DrawTextureRec(texture, source, {rect.x, rect.y}, WHITE); } Rectangle Dino::GetRect() { diff --git a/src/Dino.hpp b/src/Dino.hpp index 7c945ae..de7c0c0 100644 --- a/src/Dino.hpp +++ b/src/Dino.hpp @@ -18,6 +18,10 @@ private: static constexpr float GRAVITY = 0.4; static constexpr float JUMP_STRENGTH = -10; int groundLevel = 320; + int frameCount; + int currentFrame; + float frameTime; + float frameTimer; }; #endif //DINO_HPP \ No newline at end of file diff --git a/src/Game.cpp b/src/Game.cpp index 7a8ed9c..c00e566 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -110,7 +110,10 @@ void Game::Draw() { } int exitTextWidth = MeasureText("Press ESC to close the game!", 20); - DrawText("Press ESC to close the game!", GetScreenWidth()/2 - exitTextWidth/2, 210, 20, BLACK); + DrawText("Press ESC to close the game!", GetScreenWidth()/2 - exitTextWidth/2, 220, 20, BLACK); + + int saveTextWidth = MeasureText("(Your highscore will be saved!)", 20); + DrawText("(Your highscore will be saved!)", GetScreenWidth()/2 - saveTextWidth/2, 240, 20, BLACK); Save(); }