mirror of
https://github.com/theoleuthardt/DinoGame.git
synced 2026-06-13 09:27:57 +00:00
feat: dino walking animation
This commit is contained in:
parent
8dd09555f2
commit
d8e55e1370
3 changed files with 24 additions and 4 deletions
19
src/Dino.cpp
19
src/Dino.cpp
|
|
@ -1,8 +1,10 @@
|
||||||
#include "Dino.hpp"
|
#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),
|
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() {
|
void Dino::Jump() {
|
||||||
|
|
@ -21,10 +23,21 @@ void Dino::Update() {
|
||||||
velocityY = 0;
|
velocityY = 0;
|
||||||
isJumping = false;
|
isJumping = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!isJumping) {
|
||||||
|
frameTimer += GetFrameTime();
|
||||||
|
if (frameTimer >= frameTime) {
|
||||||
|
frameTimer = 0.0f;
|
||||||
|
currentFrame = (currentFrame + 1) % frameCount;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Dino::Draw() {
|
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() {
|
Rectangle Dino::GetRect() {
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,10 @@ private:
|
||||||
static constexpr float GRAVITY = 0.4;
|
static constexpr float GRAVITY = 0.4;
|
||||||
static constexpr float JUMP_STRENGTH = -10;
|
static constexpr float JUMP_STRENGTH = -10;
|
||||||
int groundLevel = 320;
|
int groundLevel = 320;
|
||||||
|
int frameCount;
|
||||||
|
int currentFrame;
|
||||||
|
float frameTime;
|
||||||
|
float frameTimer;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //DINO_HPP
|
#endif //DINO_HPP
|
||||||
|
|
@ -110,7 +110,10 @@ void Game::Draw() {
|
||||||
}
|
}
|
||||||
|
|
||||||
int exitTextWidth = MeasureText("Press ESC to close the game!", 20);
|
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();
|
Save();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue