diff --git a/src/Cactus.hpp b/src/Cactus.hpp index 7b55577..253e26f 100644 --- a/src/Cactus.hpp +++ b/src/Cactus.hpp @@ -2,17 +2,55 @@ #define CACTUS_H #include "raylib.h" +/** + * @brief The Cactus class represents an obstacle in the game. + */ class Cactus { public: + /** + * @brief Construct a new Cactus object + * @param texture The texture for the cactus + * @param x The x-coordinate of the cactus + * @param y The y-coordinate of the cactus + */ Cactus(Texture2D texture, float x, float y); + + /** + * @brief Update the cactus position with a constant speed + */ void Update(); + + /** + * @brief Draw the cactus to the screen + */ void Draw(); + + /** + * @brief Check if the cactus is off the screen + * @return True if the cactus is off the screen, false otherwise + */ bool IsOffScreen(); + + /** + * @brief Get the rectangle that represents the cactus position and size (hitbox) for collision checking + * @return The rectangle that represents the cactus hitbox + */ Rectangle GetRect(); private: + /** + * @brief The texture for the cactus + */ Texture2D texture; + + /** + * @brief The rectangle that represents the cactus hitbox + */ Rectangle rect; + + /** + * @brief The speed constant to move the cacti to the left + */ static constexpr float SPEED = 5; }; diff --git a/src/Dino.hpp b/src/Dino.hpp index de7c0c0..be56aa9 100644 --- a/src/Dino.hpp +++ b/src/Dino.hpp @@ -2,25 +2,92 @@ #define DINO_HPP #include "raylib.h" +/** + * @brief The Dino class represents the player character in the game. + */ class Dino { public: + /** + * @brief Construct a new Dino object. + * @param texture The texture for the dino + */ explicit Dino(Texture2D texture); + + /** + * @brief Make the dino jump if it is not already jumping. + */ void Jump(); + + /** + * @brief Let the dino fall if it is not on the ground and update the frame for the walking animation. + */ void Update(); + + /** + * @brief Draw the dino to the screen. + */ void Draw(); + + /** + * @brief Get the rectangle that represents the dinos position and size (hitbox). + * @return The rectangle that represents the dinos hitbox + */ Rectangle GetRect(); private: + /** + * @brief The texture for the dino (3 frames for walking animation) + */ Texture2D texture; + + /** + * @brief The rectangle that represents the dinos hitbox + */ Rectangle rect; + + /** + * @brief The vertical velocity of the dino (positive is down) + */ float velocityY; + + /** + * @brief A flag to indicate if the dino is currently jumping + */ bool isJumping; + + /** + * @brief Gravity constant to let the dino fall + */ static constexpr float GRAVITY = 0.4; + + /** + * @brief The dino's strength of the jump against gravity + */ static constexpr float JUMP_STRENGTH = -10; + + /** + * @brief The y-coordinate of the ground level to let the dino stand on the ground + */ int groundLevel = 320; + + /** + * @brief The number of frames for the walking animation + */ int frameCount; + + /** + * @brief The current frame for the walking animation + */ int currentFrame; + + /** + * @brief The time for each frame of the walking animation + */ float frameTime; + + /** + * @brief The timer to switch to the next frame of the walking animation + */ float frameTimer; }; diff --git a/src/Game.hpp b/src/Game.hpp index f01df67..55730eb 100644 --- a/src/Game.hpp +++ b/src/Game.hpp @@ -7,27 +7,106 @@ using namespace std; +/** + * @brief The Game class is responsible for running the game loop, + * updating the game state, and drawing the game objects. + */ class Game { public: + /** + * @brief Construct a new Game object to initialize the game window and load the textures. + */ Game(); + + /** + * @brief Destroy the Game object if the game window is closed. + */ ~Game(); + + /** + * @brief Run the game loop to update and draw the game objects (loop until the window is closed). + */ void Run(); private: + /** + * @brief Update the game state for the current frame. If the game is not over yet, the dino can jump and the cacti + * are moving towards the dino. On a random interval, a new cactus is spawned. If the dino collides with a cactus, + * the game is over and can be restarted! The score is increased every frame. + */ void Update(); + + /** + * @brief Draw the game objects to the screen. If the game is over, the game over screen is displayed with the + * hints to restart the game or close the window. The highscore is saved if the player wants to play again. + * + */ void Draw(); + + /** + * @brief Reset the game state if the player wants to play again. It deletes the dino object and creates a new one, + * deletes all cacti and creates a new one at the beginning of the game. Also the score is reset to 0 and the game's + * highscore is set to the maximum of the current highscore and the current score. + */ void Reset(); + + /** + * @brief Saves the high score to a dat-file. If the file does not exist, it will be created. + */ void Save(); + + /** + * @brief Loads the high score from a dat-file. If the file does not exist, the high score will be 0. + */ void Load(); + + /** + * @brief Checks for collision between the dino and a cactus. It uses the rectangle collision detection from raylib. + * @param dino The dino object + * @param cactus The cactus object + * @return true if the dino and cactus are colliding + * @return false if the dino and cactus are not colliding + */ bool CheckCollision(Dino &dino, Cactus &cactus); + /** + * @brief The texture for the dino (3 frames) + */ Texture2D dinoTexture; + + /** + * @brief The texture for the cactus + */ Texture2D cactusTexture; + + /** + * @brief The texture for the ground + */ Texture2D groundTexture; + + /** + * @brief The dino object + */ Dino *dino; + + /** + * @brief The vector of cactus objects to save all the cacti + */ vector cacti; + + /** + * @brief The displayed score of the player + */ int score; + + /** + * @brief The highscore of the player which will be saved + */ int highscore; + + /** + * @brief The state if the game is over + */ bool gameOver; };