docs: more comments

This commit is contained in:
Martin Prokoph
2025-02-07 15:54:33 +01:00
parent c813274a6c
commit 813e47a25e
10 changed files with 166 additions and 6 deletions

View File

@@ -1,2 +1 @@
mkdir build g++ -std=c++23 -Wall ./main.cpp -o ./adventura && ./adventura
g++ -std=c++23 -Wall ./main.cpp -o ./build/testCompiled && ./build/testCompiled

BIN
adventura Executable file

Binary file not shown.

View File

@@ -11,7 +11,22 @@ private:
BlockSettings settings; BlockSettings settings;
public: public:
/**
* Constructs a block with the given identifier, encoding and settings.
*
* @param id The (unique) identifier of the block.
* @param encoding The encoding of the block, which is the character used to represent it in the game world.
* @param settings The settings of the block, which define how the block behaves in the game world.
*/
Block(Identifier id, char encoding, BlockSettings settings) : Block(id, encoding, Color::RESET, settings) {}; Block(Identifier id, char encoding, BlockSettings settings) : Block(id, encoding, Color::RESET, settings) {};
/**
* Constructs a block with the given identifier, encoding, color and settings.
*
* @param id The (unique) identifier of the block.
* @param encoding The encoding of the block, which is the character used to represent it in the game world.
* @param color The color of the block.
* @param settings The settings of the block, which define how the block behaves in the game world.
*/
Block(Identifier id, char encoding, Color color, BlockSettings settings) { Block(Identifier id, char encoding, Color color, BlockSettings settings) {
this->id = id; this->id = id;
this->encoding = encoding; this->encoding = encoding;
@@ -19,19 +34,55 @@ public:
this->settings = settings; this->settings = settings;
}; };
/**
* Returns the settings associated with the block.
*
* @return The settings of the block, including solidity, pushability, and more.
*/
BlockSettings getSettings() { BlockSettings getSettings() {
return settings; return settings;
} }
/**
* Gets the identifier of the block.
*
* This identifier is used by the game to uniquely identify the block.
*
* @return The identifier of the block.
*/
Identifier getId() { Identifier getId() {
return id; return id;
} }
/**
* Returns the color of the block.
*
* The color is used when drawing the block in the world.
*
* @return The color of the block.
*/
Color getColor() { Color getColor() {
return color; return color;
} }
/**
* Returns the character encoding of the block.
*
* This character is used in the text file as well as the terminal to represent the block.
*
* @return The character encoding of the block.
*/
char getEncoding() { char getEncoding() {
return encoding; return encoding;
} }
/**
* Sets the character encoding for the block.
*
* This encoding is used to represent the block in text files and the terminal.
*
* @param encoding The character encoding to set for the block.
*/
void setEncoding(char encoding) { void setEncoding(char encoding) {
this->encoding = encoding; this->encoding = encoding;
} }

View File

@@ -3,28 +3,67 @@ class BlockPos {
int x; int x;
int y; int y;
public: public:
/**
* Define an in-world position.
*
* @param x The x-coordinate of the BlockPos.
* @param y The y-coordinate of the BlockPos.
*/
BlockPos(int x, int y) { BlockPos(int x, int y) {
this->x = x; this->x = x;
this->y = y; this->y = y;
} }
/**
* @return The x-coordinate of the BlockPos.
*/
int getX() { int getX() {
return x; return x;
} }
/**
* @return The y-coordinate of the BlockPos.
*/
int getY() { int getY() {
return y; return y;
} }
/**
* @return The x-coordinate of the BlockPos as an unsigned integer.
* @pre x >= 0
*/
unsigned int getUnsignedX() { unsigned int getUnsignedX() {
return static_cast<unsigned int>(x); return static_cast<unsigned int>(x);
} }
/**
* @return The y-coordinate of the BlockPos as an unsigned integer.
* @pre y >= 0
*/
unsigned int getUnsignedY() { unsigned int getUnsignedY() {
return static_cast<unsigned int>(y); return static_cast<unsigned int>(y);
} }
/**
* @return True if the BlockPos is negative, false otherwise.
*
* A BlockPos is considered negative if either the x-coordinate or the y-coordinate is negative.
*/
bool isNegative() { bool isNegative() {
return x < 0 || y < 0; return x < 0 || y < 0;
} }
/**
* Add the given coordinates to the BlockPos.
*
* @param x The x-coordinate to add.
* @param y The y-coordinate to add.
* @return The BlockPos with the given coordinates added.
*/
BlockPos add(int x, int y) { BlockPos add(int x, int y) {
return BlockPos(this->x + x, this->y + y); return BlockPos(this->x + x, this->y + y);
} }
BlockPos operator+(BlockPos offset) { BlockPos operator+(BlockPos offset) {
return BlockPos(this->getX() + offset.getX(), this->getY() + offset.getY()); return BlockPos(this->getX() + offset.getX(), this->getY() + offset.getY());
} }

View File

@@ -19,6 +19,9 @@ public:
Block BOX = Block(Identifier("adventura", "box"), 'x', Color::BRIGHT_CYAN, BlockSettingsBuilder().pushable().collidable().gravity().build()); Block BOX = Block(Identifier("adventura", "box"), 'x', Color::BRIGHT_CYAN, BlockSettingsBuilder().pushable().collidable().gravity().build());
Block SAND = Block(Identifier("adventura", "sand"), '*', Color::BRIGHT_YELLOW, BlockSettingsBuilder().brittle().gravity().build()); Block SAND = Block(Identifier("adventura", "sand"), '*', Color::BRIGHT_YELLOW, BlockSettingsBuilder().brittle().gravity().build());
/**
* Constructor for BlockRegistry. Registers all built-in blocks.
*/
BlockRegistry() { BlockRegistry() {
registerBlock(AIR); registerBlock(AIR);
registerBlock(WATER); registerBlock(WATER);
@@ -48,9 +51,13 @@ public:
} }
private: private:
Block registerBlock(Block& block) { /**
* Registers a block in the registry.
*
* @param block The block to register.
*/
void registerBlock(Block& block) {
registeredBlocks.push_back(block); registeredBlocks.push_back(block);
return block;
} }
vector<Block> registeredBlocks; vector<Block> registeredBlocks;
}; };

View File

@@ -12,6 +12,7 @@ enum class Color {
BRIGHT_CYAN= 96, BRIGHT_CYAN= 96,
BRIGHT_WHITE= 97 BRIGHT_WHITE= 97
}; };
std::ostream& operator<<(std::ostream& os, Color color) { std::ostream& operator<<(std::ostream& os, Color color) {
return os << "\033[" << static_cast<int>(color) << "m"; return os << "\033[" << static_cast<int>(color) << "m";
} }

View File

@@ -8,7 +8,17 @@ public:
std::string nameSpace; std::string nameSpace;
std::string path; std::string path;
Identifier(std::string nameSpace, std::string path) : nameSpace(nameSpace), path(path) {}
/**
* Construct a new Identifier.
* Identifiers are used to uniquely identify blocks, regardless of their encoding.
*
* @param nameSpace The namespace of the Identifier.
* @param path The path of the Identifier.
*/
Identifier(std::string nameSpace, std::string path) : nameSpace(nameSpace), path(path) {
}
std::ostream& operator<<(std::ostream& out) { std::ostream& operator<<(std::ostream& out) {
out << nameSpace << ":" << path; out << nameSpace << ":" << path;

View File

@@ -8,21 +8,52 @@
class Player { class Player {
public: public:
/**
* Initializes a new Player at the specified starting position in the provided world.
*
* @param pos The initial position of the player within the world.
* @param world A reference to the World object representing the game world.
*/
Player(BlockPos pos, World& world) : world(world) { Player(BlockPos pos, World& world) : world(world) {
this->pos = pos; this->pos = pos;
this->world = world; this->world = world;
playerTexture = REGULAR_PLAYER_TEXTURE; playerTexture = REGULAR_PLAYER_TEXTURE;
} }
/**
* Retrieves the current position of the player in the world.
*
* @return The current BlockPos representing the player's position.
*/
BlockPos getPos() { BlockPos getPos() {
return pos; return pos;
} }
/**
* Move the player by the specified x and y offsets.
*
* @param x The x offset to move by.
* @param y The y offset to move by.
*/
void move(int x, int y) { void move(int x, int y) {
move(BlockPos(x, y)); move(BlockPos(x, y));
} }
/**
* Move the player by the specified BlockPos offset.
*
* @param offset The BlockPos representing the offset to move the player by.
*/
void move(BlockPos offset) { void move(BlockPos offset) {
setPos(pos + offset); setPos(pos + offset);
} }
/**
* Updates the player's position and checks for any conditions that would update the state of the player.
*
* @param pos The position to move the player to.
*/
void setPos(BlockPos pos) { void setPos(BlockPos pos) {
if (!world.containsPos(pos)) { if (!world.containsPos(pos)) {
alive = false; alive = false;
@@ -50,17 +81,39 @@ public:
if (world.getBlockAt(pos.add(0, 2)).getSettings().isLethal()) unalive(); if (world.getBlockAt(pos.add(0, 2)).getSettings().isLethal()) unalive();
} }
/**
* Handle the unfortunate case of a player dying :(
*/
void unalive() { void unalive() {
playerTexture = DEAD_PLAYER_TEXTURE; playerTexture = DEAD_PLAYER_TEXTURE;
redraw(world, this->mapToWorldspace()); redraw(world, this->mapToWorldspace());
alive = false; alive = false;
} }
/**
* Checks if the player is still alive.
*
* @return true if the player is alive, false otherwise.
*/
bool isAlive() { bool isAlive() {
return alive; return alive;
} }
/**
* Checks if the player has reached the goal in the current world.
*
* @return true if the player has reached the goal, false otherwise.
*/
bool hasReachedGoal() { bool hasReachedGoal() {
return reachedGoal; return reachedGoal;
} }
/**
* Maps the player texture to the player's position in the current game world.
*
* @return A 2D vector of characters representing the player's position in the world.
*/
vector<vector<char>> mapToWorldspace() { vector<vector<char>> mapToWorldspace() {
vector<vector<char>> map; vector<vector<char>> map;
for (unsigned int y = 0; y <= world.getMaxY(); y++) { for (unsigned int y = 0; y <= world.getMaxY(); y++) {

View File

@@ -1,7 +1,7 @@
#pragma once #pragma once
#include <vector> #include <vector>
#include <array> #include <array>
#include "fileutils.hpp" #include "fileUtils.hpp"
#include "block.hpp" #include "block.hpp"
#include "blockRegistry.hpp" #include "blockRegistry.hpp"
#include "blockPos.hpp" #include "blockPos.hpp"