feat: dino walking animation

This commit is contained in:
theoleuthardt 2025-03-13 14:07:06 +01:00
parent 8dd09555f2
commit d8e55e1370
3 changed files with 24 additions and 4 deletions

View file

@ -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<float>(320 - texture.height),
static_cast<float>(texture.width), static_cast<float>(texture.height)};
static_cast<float>(texture.width / frameCount), static_cast<float>(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<float>(currentFrame * (texture.width / frameCount)), 0,
static_cast<float>(texture.width / frameCount), static_cast<float>(texture.height)};
DrawTextureRec(texture, source, {rect.x, rect.y}, WHITE);
}
Rectangle Dino::GetRect() {

View file

@ -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

View file

@ -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();
}