This commit is contained in:
2025-10-27 18:35:53 +01:00
parent b1dca32a5b
commit 3179a08dac
63 changed files with 686 additions and 693 deletions

View File

@@ -5,6 +5,7 @@
#include <fstream> // Para basic_ostream, operator<<, basic_istream
#include <iostream> // Para cout, cerr
#include <sstream> // Para basic_stringstream
#include <utility>
#include "core/rendering/screen.hpp" // Para Screen
#include "core/rendering/surface.hpp" // Para Surface
@@ -19,7 +20,7 @@
#include "utils/utils.hpp" // Para LineHorizontal, LineDiagonal, LineVertical
// Carga las variables y texturas desde un fichero de mapa de tiles
std::vector<int> loadRoomTileFile(const std::string& file_path, bool verbose) {
auto loadRoomTileFile(const std::string& file_path, bool verbose) -> std::vector<int> {
std::vector<int> tile_map_file;
const std::string FILENAME = file_path.substr(file_path.find_last_of("\\/") + 1);
std::ifstream file(file_path);
@@ -47,14 +48,14 @@ std::vector<int> loadRoomTileFile(const std::string& file_path, bool verbose) {
// Cierra el fichero
if (verbose) {
std::cout << "TileMap loaded: " << FILENAME.c_str() << std::endl;
std::cout << "TileMap loaded: " << FILENAME.c_str() << '\n';
}
file.close();
}
else { // El fichero no se puede abrir
if (verbose) {
std::cout << "Warning: Unable to open " << FILENAME.c_str() << " file" << std::endl;
std::cout << "Warning: Unable to open " << FILENAME.c_str() << " file" << '\n';
}
}
@@ -62,8 +63,8 @@ std::vector<int> loadRoomTileFile(const std::string& file_path, bool verbose) {
}
// Parsea una línea en key y value separados por '='
std::pair<std::string, std::string> parseKeyValue(const std::string& line) {
int pos = line.find("=");
auto parseKeyValue(const std::string& line) -> std::pair<std::string, std::string> {
int pos = line.find('=');
std::string key = line.substr(0, pos);
std::string value = line.substr(pos + 1, line.length());
return {key, value};
@@ -72,12 +73,12 @@ std::pair<std::string, std::string> parseKeyValue(const std::string& line) {
// Muestra un warning de parámetro desconocido
void logUnknownParameter(const std::string& file_name, const std::string& key, bool verbose) {
if (verbose) {
std::cout << "Warning: file " << file_name.c_str() << "\n, unknown parameter \"" << key.c_str() << "\"" << std::endl;
std::cout << "Warning: file " << file_name.c_str() << "\n, unknown parameter \"" << key.c_str() << "\"" << '\n';
}
}
// Carga un bloque [enemy]...[/enemy] desde un archivo
EnemyData loadEnemyFromFile(std::ifstream& file, const std::string& file_name, bool verbose) {
auto loadEnemyFromFile(std::ifstream& file, const std::string& file_name, bool verbose) -> EnemyData {
EnemyData enemy;
enemy.flip = false;
enemy.mirror = false;
@@ -97,7 +98,7 @@ EnemyData loadEnemyFromFile(std::ifstream& file, const std::string& file_name, b
}
// Carga un bloque [item]...[/item] desde un archivo
ItemData loadItemFromFile(std::ifstream& file, const std::string& file_name, bool verbose) {
auto loadItemFromFile(std::ifstream& file, const std::string& file_name, bool verbose) -> ItemData {
ItemData item;
item.counter = 0;
item.color1 = stringToColor("yellow");
@@ -117,14 +118,14 @@ ItemData loadItemFromFile(std::ifstream& file, const std::string& file_name, boo
}
// Carga las variables desde un fichero de mapa
RoomData loadRoomFile(const std::string& file_path, bool verbose) {
auto loadRoomFile(const std::string& file_path, bool verbose) -> RoomData {
RoomData room;
room.item_color1 = "yellow";
room.item_color2 = "magenta";
room.conveyor_belt_direction = 1;
const std::string FILE_NAME = file_path.substr(file_path.find_last_of("\\/") + 1);
room.number = FILE_NAME.substr(0, FILE_NAME.find_last_of("."));
room.number = FILE_NAME.substr(0, FILE_NAME.find_last_of('.'));
std::ifstream file(file_path);
@@ -152,20 +153,20 @@ RoomData loadRoomFile(const std::string& file_path, bool verbose) {
// Cierra el fichero
if (verbose) {
std::cout << "Room loaded: " << FILE_NAME.c_str() << std::endl;
std::cout << "Room loaded: " << FILE_NAME.c_str() << '\n';
}
file.close();
}
// El fichero no se puede abrir
else {
std::cout << "Warning: Unable to open " << FILE_NAME.c_str() << " file" << std::endl;
std::cout << "Warning: Unable to open " << FILE_NAME.c_str() << " file" << '\n';
}
return room;
}
// Asigna variables a una estructura RoomData
bool setRoom(RoomData* room, const std::string& key, const std::string& value) {
auto setRoom(RoomData* room, const std::string& key, const std::string& value) -> bool {
// Indicador de éxito en la asignación
bool success = true;
@@ -200,7 +201,7 @@ bool setRoom(RoomData* room, const std::string& key, const std::string& value) {
success = false;
}
} catch (const std::exception& e) {
std::cerr << "Error al asignar la clave " << key << " con valor " << value << ": " << e.what() << std::endl;
std::cerr << "Error al asignar la clave " << key << " con valor " << value << ": " << e.what() << '\n';
success = false;
}
@@ -208,7 +209,7 @@ bool setRoom(RoomData* room, const std::string& key, const std::string& value) {
}
// Asigna variables a una estructura EnemyData
bool setEnemy(EnemyData* enemy, const std::string& key, const std::string& value) {
auto setEnemy(EnemyData* enemy, const std::string& key, const std::string& value) -> bool {
// Indicador de éxito en la asignación
bool success = true;
@@ -251,7 +252,7 @@ bool setEnemy(EnemyData* enemy, const std::string& key, const std::string& value
success = false;
}
} catch (const std::exception& e) {
std::cerr << "Error al asignar la clave " << key << " con valor " << value << ": " << e.what() << std::endl;
std::cerr << "Error al asignar la clave " << key << " con valor " << value << ": " << e.what() << '\n';
success = false;
}
@@ -259,7 +260,7 @@ bool setEnemy(EnemyData* enemy, const std::string& key, const std::string& value
}
// Asigna variables a una estructura ItemData
bool setItem(ItemData* item, const std::string& key, const std::string& value) {
auto setItem(ItemData* item, const std::string& key, const std::string& value) -> bool {
// Indicador de éxito en la asignación
bool success = true;
@@ -280,7 +281,7 @@ bool setItem(ItemData* item, const std::string& key, const std::string& value) {
success = false;
}
} catch (const std::exception& e) {
std::cerr << "Error al asignar la clave " << key << " con valor " << value << ": " << e.what() << std::endl;
std::cerr << "Error al asignar la clave " << key << " con valor " << value << ": " << e.what() << '\n';
success = false;
}
@@ -289,7 +290,7 @@ bool setItem(ItemData* item, const std::string& key, const std::string& value) {
// Constructor
Room::Room(const std::string& room_path, std::shared_ptr<ScoreboardData> data)
: data_(data) {
: data_(std::move(std::move(data))) {
auto room = Resource::get()->getRoom(room_path);
initializeRoom(*room);
@@ -482,19 +483,19 @@ void Room::update() {
// Actualiza los tiles animados
updateAnimatedTiles();
for (auto enemy : enemies_) {
for (const auto& enemy : enemies_) {
// Actualiza los enemigos
enemy->update();
}
for (auto item : items_) {
for (const auto& item : items_) {
// Actualiza los items
item->update();
}
}
// Devuelve la cadena del fichero de la habitación contigua segun el borde
std::string Room::getRoom(RoomBorder border) {
auto Room::getRoom(RoomBorder border) -> std::string {
switch (border) {
case RoomBorder::TOP:
return upper_room_;
@@ -519,13 +520,13 @@ std::string Room::getRoom(RoomBorder border) {
}
// Devuelve el tipo de tile que hay en ese pixel
TileType Room::getTile(SDL_FPoint point) {
auto Room::getTile(SDL_FPoint point) -> TileType {
const int POS = ((point.y / TILE_SIZE) * MAP_WIDTH) + (point.x / TILE_SIZE);
return getTile(POS);
}
// Devuelve el tipo de tile que hay en ese indice
TileType Room::getTile(int index) {
auto Room::getTile(int index) -> TileType {
// const bool onRange = (index > -1) && (index < mapWidth * mapHeight);
const bool ON_RANGE = (index > -1) && (index < (int)tile_map_.size());
@@ -565,14 +566,14 @@ TileType Room::getTile(int index) {
}
// Indica si hay colision con un enemigo a partir de un rectangulo
bool Room::enemyCollision(SDL_FRect& rect) {
auto Room::enemyCollision(SDL_FRect& rect) -> bool {
return std::ranges::any_of(enemies_, [&rect](const auto& enemy) {
return checkCollision(rect, enemy->getCollider());
});
}
// Indica si hay colision con un objeto a partir de un rectangulo
bool Room::itemCollision(SDL_FRect& rect) {
auto Room::itemCollision(SDL_FRect& rect) -> bool {
for (int i = 0; i < static_cast<int>(items_.size()); ++i) {
if (checkCollision(rect, items_.at(i)->getCollider())) {
ItemTracker::get()->addItem(name_, items_.at(i)->getPos());
@@ -588,7 +589,7 @@ bool Room::itemCollision(SDL_FRect& rect) {
}
// Obten la coordenada de la cuesta a partir de un punto perteneciente a ese tile
int Room::getSlopeHeight(SDL_FPoint p, TileType slope) {
auto Room::getSlopeHeight(SDL_FPoint p, TileType slope) -> int {
// Calcula la base del tile
int base = ((p.y / TILE_SIZE) * TILE_SIZE) + TILE_SIZE;
#ifdef _DEBUG
@@ -618,7 +619,7 @@ int Room::getSlopeHeight(SDL_FPoint p, TileType slope) {
}
// Helper: recopila tiles inferiores (muros sin muro debajo)
std::vector<int> Room::collectBottomTiles() {
auto Room::collectBottomTiles() -> std::vector<int> {
std::vector<int> tile;
// Busca todos los tiles de tipo muro que no tengan debajo otro muro
@@ -640,7 +641,7 @@ std::vector<int> Room::collectBottomTiles() {
}
// Helper: recopila tiles superiores (muros o pasables sin muro encima)
std::vector<int> Room::collectTopTiles() {
auto Room::collectTopTiles() -> std::vector<int> {
std::vector<int> tile;
// Busca todos los tiles de tipo muro o pasable que no tengan encima un muro
@@ -867,7 +868,7 @@ void Room::setRightSlopes() {
// Calcula las superficies automaticas
// Helper: recopila tiles animados (para superficies automaticas/conveyor belts)
std::vector<int> Room::collectAnimatedTiles() {
auto Room::collectAnimatedTiles() -> std::vector<int> {
std::vector<int> tile;
// Busca todos los tiles de tipo animado
@@ -943,7 +944,7 @@ void Room::renderAnimatedTiles() {
}
// Comprueba las colisiones
int Room::checkRightSurfaces(SDL_FRect* rect) {
auto Room::checkRightSurfaces(SDL_FRect* rect) -> int {
for (const auto& s : right_walls_) {
if (checkCollision(s, *rect)) {
return s.x;
@@ -954,7 +955,7 @@ int Room::checkRightSurfaces(SDL_FRect* rect) {
}
// Comprueba las colisiones
int Room::checkLeftSurfaces(SDL_FRect* rect) {
auto Room::checkLeftSurfaces(SDL_FRect* rect) -> int {
for (const auto& s : left_walls_) {
if (checkCollision(s, *rect)) {
return s.x;
@@ -965,7 +966,7 @@ int Room::checkLeftSurfaces(SDL_FRect* rect) {
}
// Comprueba las colisiones
int Room::checkTopSurfaces(SDL_FRect* rect) {
auto Room::checkTopSurfaces(SDL_FRect* rect) -> int {
for (const auto& s : top_floors_) {
if (checkCollision(s, *rect)) {
return s.y;
@@ -976,7 +977,7 @@ int Room::checkTopSurfaces(SDL_FRect* rect) {
}
// Comprueba las colisiones
int Room::checkBottomSurfaces(SDL_FRect* rect) {
auto Room::checkBottomSurfaces(SDL_FRect* rect) -> int {
for (const auto& s : bottom_floors_) {
if (checkCollision(s, *rect)) {
return s.y;
@@ -987,7 +988,7 @@ int Room::checkBottomSurfaces(SDL_FRect* rect) {
}
// Comprueba las colisiones
int Room::checkAutoSurfaces(SDL_FRect* rect) {
auto Room::checkAutoSurfaces(SDL_FRect* rect) -> int {
for (const auto& s : conveyor_belt_floors_) {
if (checkCollision(s, *rect)) {
return s.y;
@@ -998,21 +999,21 @@ int Room::checkAutoSurfaces(SDL_FRect* rect) {
}
// Comprueba las colisiones
bool Room::checkTopSurfaces(SDL_FPoint* p) {
auto Room::checkTopSurfaces(SDL_FPoint* p) -> bool {
return std::ranges::any_of(top_floors_, [&](const auto& s) {
return checkCollision(s, *p);
});
}
// Comprueba las colisiones
bool Room::checkAutoSurfaces(SDL_FPoint* p) {
auto Room::checkAutoSurfaces(SDL_FPoint* p) -> bool {
return std::ranges::any_of(conveyor_belt_floors_, [&](const auto& s) {
return checkCollision(s, *p);
});
}
// Comprueba las colisiones
int Room::checkLeftSlopes(const LineVertical* line) {
auto Room::checkLeftSlopes(const LineVertical* line) -> int {
for (const auto& slope : left_slopes_) {
const auto P = checkCollision(slope, *line);
if (P.x != -1) {
@@ -1024,14 +1025,14 @@ int Room::checkLeftSlopes(const LineVertical* line) {
}
// Comprueba las colisiones
bool Room::checkLeftSlopes(SDL_FPoint* p) {
auto Room::checkLeftSlopes(SDL_FPoint* p) -> bool {
return std::ranges::any_of(left_slopes_, [&](const auto& slope) {
return checkCollision(*p, slope);
});
}
// Comprueba las colisiones
int Room::checkRightSlopes(const LineVertical* line) {
auto Room::checkRightSlopes(const LineVertical* line) -> int {
for (const auto& slope : right_slopes_) {
const auto P = checkCollision(slope, *line);
if (P.x != -1) {
@@ -1043,7 +1044,7 @@ int Room::checkRightSlopes(const LineVertical* line) {
}
// Comprueba las colisiones
bool Room::checkRightSlopes(SDL_FPoint* p) {
auto Room::checkRightSlopes(SDL_FPoint* p) -> bool {
return std::ranges::any_of(right_slopes_, [&](const auto& slope) {
return checkCollision(*p, slope);
});