mirror of
https://github.com/theoleuthardt/DinoGame.git
synced 2026-06-13 09:27:57 +00:00
docs: documentation of all game object classes
This commit is contained in:
parent
d8e55e1370
commit
06d5c33258
3 changed files with 184 additions and 0 deletions
|
|
@ -2,17 +2,55 @@
|
||||||
#define CACTUS_H
|
#define CACTUS_H
|
||||||
#include "raylib.h"
|
#include "raylib.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief The Cactus class represents an obstacle in the game.
|
||||||
|
*/
|
||||||
class Cactus {
|
class Cactus {
|
||||||
public:
|
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);
|
Cactus(Texture2D texture, float x, float y);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Update the cactus position with a constant speed
|
||||||
|
*/
|
||||||
void Update();
|
void Update();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Draw the cactus to the screen
|
||||||
|
*/
|
||||||
void Draw();
|
void Draw();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Check if the cactus is off the screen
|
||||||
|
* @return True if the cactus is off the screen, false otherwise
|
||||||
|
*/
|
||||||
bool IsOffScreen();
|
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();
|
Rectangle GetRect();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
/**
|
||||||
|
* @brief The texture for the cactus
|
||||||
|
*/
|
||||||
Texture2D texture;
|
Texture2D texture;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief The rectangle that represents the cactus hitbox
|
||||||
|
*/
|
||||||
Rectangle rect;
|
Rectangle rect;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief The speed constant to move the cacti to the left
|
||||||
|
*/
|
||||||
static constexpr float SPEED = 5;
|
static constexpr float SPEED = 5;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
67
src/Dino.hpp
67
src/Dino.hpp
|
|
@ -2,25 +2,92 @@
|
||||||
#define DINO_HPP
|
#define DINO_HPP
|
||||||
#include "raylib.h"
|
#include "raylib.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief The Dino class represents the player character in the game.
|
||||||
|
*/
|
||||||
class Dino {
|
class Dino {
|
||||||
public:
|
public:
|
||||||
|
/**
|
||||||
|
* @brief Construct a new Dino object.
|
||||||
|
* @param texture The texture for the dino
|
||||||
|
*/
|
||||||
explicit Dino(Texture2D texture);
|
explicit Dino(Texture2D texture);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Make the dino jump if it is not already jumping.
|
||||||
|
*/
|
||||||
void Jump();
|
void Jump();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Let the dino fall if it is not on the ground and update the frame for the walking animation.
|
||||||
|
*/
|
||||||
void Update();
|
void Update();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Draw the dino to the screen.
|
||||||
|
*/
|
||||||
void Draw();
|
void Draw();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Get the rectangle that represents the dinos position and size (hitbox).
|
||||||
|
* @return The rectangle that represents the dinos hitbox
|
||||||
|
*/
|
||||||
Rectangle GetRect();
|
Rectangle GetRect();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
/**
|
||||||
|
* @brief The texture for the dino (3 frames for walking animation)
|
||||||
|
*/
|
||||||
Texture2D texture;
|
Texture2D texture;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief The rectangle that represents the dinos hitbox
|
||||||
|
*/
|
||||||
Rectangle rect;
|
Rectangle rect;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief The vertical velocity of the dino (positive is down)
|
||||||
|
*/
|
||||||
float velocityY;
|
float velocityY;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief A flag to indicate if the dino is currently jumping
|
||||||
|
*/
|
||||||
bool isJumping;
|
bool isJumping;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Gravity constant to let the dino fall
|
||||||
|
*/
|
||||||
static constexpr float GRAVITY = 0.4;
|
static constexpr float GRAVITY = 0.4;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief The dino's strength of the jump against gravity
|
||||||
|
*/
|
||||||
static constexpr float JUMP_STRENGTH = -10;
|
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;
|
int groundLevel = 320;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief The number of frames for the walking animation
|
||||||
|
*/
|
||||||
int frameCount;
|
int frameCount;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief The current frame for the walking animation
|
||||||
|
*/
|
||||||
int currentFrame;
|
int currentFrame;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief The time for each frame of the walking animation
|
||||||
|
*/
|
||||||
float frameTime;
|
float frameTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief The timer to switch to the next frame of the walking animation
|
||||||
|
*/
|
||||||
float frameTimer;
|
float frameTimer;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
79
src/Game.hpp
79
src/Game.hpp
|
|
@ -7,27 +7,106 @@
|
||||||
|
|
||||||
using namespace std;
|
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 {
|
class Game {
|
||||||
public:
|
public:
|
||||||
|
/**
|
||||||
|
* @brief Construct a new Game object to initialize the game window and load the textures.
|
||||||
|
*/
|
||||||
Game();
|
Game();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Destroy the Game object if the game window is closed.
|
||||||
|
*/
|
||||||
~Game();
|
~Game();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Run the game loop to update and draw the game objects (loop until the window is closed).
|
||||||
|
*/
|
||||||
void Run();
|
void Run();
|
||||||
|
|
||||||
private:
|
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();
|
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();
|
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();
|
void Reset();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Saves the high score to a dat-file. If the file does not exist, it will be created.
|
||||||
|
*/
|
||||||
void Save();
|
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();
|
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);
|
bool CheckCollision(Dino &dino, Cactus &cactus);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief The texture for the dino (3 frames)
|
||||||
|
*/
|
||||||
Texture2D dinoTexture;
|
Texture2D dinoTexture;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief The texture for the cactus
|
||||||
|
*/
|
||||||
Texture2D cactusTexture;
|
Texture2D cactusTexture;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief The texture for the ground
|
||||||
|
*/
|
||||||
Texture2D groundTexture;
|
Texture2D groundTexture;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief The dino object
|
||||||
|
*/
|
||||||
Dino *dino;
|
Dino *dino;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief The vector of cactus objects to save all the cacti
|
||||||
|
*/
|
||||||
vector<Cactus> cacti;
|
vector<Cactus> cacti;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief The displayed score of the player
|
||||||
|
*/
|
||||||
int score;
|
int score;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief The highscore of the player which will be saved
|
||||||
|
*/
|
||||||
int highscore;
|
int highscore;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief The state if the game is over
|
||||||
|
*/
|
||||||
bool gameOver;
|
bool gameOver;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue