From 10194544f3429afcdeef8df4c69a331734095c0e Mon Sep 17 00:00:00 2001 From: theoleuthardt Date: Thu, 13 Mar 2025 10:54:02 +0100 Subject: [PATCH] feat: dino class for drawing, updating and managing the player object --- src/Dino.cpp | 31 +++++++++++++++++++++++++++++++ src/Dino.hpp | 22 ++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 src/Dino.cpp create mode 100644 src/Dino.hpp diff --git a/src/Dino.cpp b/src/Dino.cpp new file mode 100644 index 0000000..a033c60 --- /dev/null +++ b/src/Dino.cpp @@ -0,0 +1,31 @@ +#include "Dino.hpp" + +Dino::Dino(Texture2D tex) : texture(tex), velocityY(0), isJumping(false) { + rect = {50.0f, static_cast(300 - texture.height), + static_cast(texture.width), static_cast(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; +} diff --git a/src/Dino.hpp b/src/Dino.hpp new file mode 100644 index 0000000..4a198ab --- /dev/null +++ b/src/Dino.hpp @@ -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 \ No newline at end of file