feat: dino class for drawing, updating and managing the player object

This commit is contained in:
theoleuthardt 2025-03-13 10:54:02 +01:00
parent da4c292fbd
commit 10194544f3
2 changed files with 53 additions and 0 deletions

31
src/Dino.cpp Normal file
View file

@ -0,0 +1,31 @@
#include "Dino.hpp"
Dino::Dino(Texture2D tex) : texture(tex), velocityY(0), isJumping(false) {
rect = {50.0f, static_cast<float>(300 - texture.height),
static_cast<float>(texture.width), static_cast<float>(texture.height)};
}
void Dino::Jump() {
if (!isJumping) {
velocityY = JUMP_STRENGTH;
isJumping = true;
}
}
void Dino::Update() {
velocityY += GRAVITY;
rect.y += velocityY;
if (rect.y >= 300 - rect.height) {
rect.y = 300 - rect.height;
isJumping = false;
}
}
void Dino::Draw() {
DrawTexture(texture, rect.x, rect.y, WHITE);
}
Rectangle Dino::GetRect() {
return rect;
}

22
src/Dino.hpp Normal file
View file

@ -0,0 +1,22 @@
#ifndef DINO_HPP
#define DINO_HPP
#include "raylib.h"
class Dino {
public:
explicit Dino(Texture2D texture);
void Jump();
void Update();
void Draw();
Rectangle GetRect();
private:
Texture2D texture;
Rectangle rect;
float velocityY;
bool isJumping;
static constexpr float GRAVITY = 0.4;
static constexpr float JUMP_STRENGTH = -10;
};
#endif //DINO_HPP