Arreglos en la estructura i format del codi

This commit is contained in:
2025-03-02 09:32:25 +01:00
parent 193dac708f
commit b1ba5e67dc
41 changed files with 611 additions and 739 deletions

View File

@@ -1,22 +1,23 @@
#include "room.h"
#include <SDL2/SDL_blendmode.h> // Para SDL_BLENDMODE_BLEND
#include <SDL2/SDL_error.h> // Para SDL_GetError
#include <SDL2/SDL_pixels.h> // Para SDL_PIXELFORMAT_RGBA8888
#include <stdlib.h> // Para rand
#include <fstream> // Para basic_ostream, operator<<, basic_ist...
#include <iostream> // Para cout
#include <sstream> // Para basic_stringstream
#include "asset.h" // Para Asset
#include "defines.h" // Para BLOCK, PLAY_AREA_HEIGHT, PLAY_AREA_W...
#include "debug.h" // Para Debug
#include "item_tracker.h" // Para ItemTracker
#include "jail_audio.h" // Para JA_DeleteSound, JA_LoadSound, JA_Pla...
#include "screen.h" // Para Screen
#include "sprite.h" // Para Sprite
#include "texture.h" // Para Texture
#include "options.h"
#include "utils.h" // Para stringToBool, stringToColor
#include "resource.h"
#include <SDL2/SDL_blendmode.h> // for SDL_BLENDMODE_BLEND
#include <SDL2/SDL_error.h> // for SDL_GetError
#include <SDL2/SDL_pixels.h> // for SDL_PIXELFORMAT_RGBA8888
#include <stdlib.h> // for rand
#include <exception> // for exception
#include <fstream> // for basic_ostream, operator<<, basic_ist...
#include <iostream> // for cout, cerr
#include <sstream> // for basic_stringstream
#include "debug.h" // for Debug
#include "defines.h" // for BLOCK, PLAY_AREA_HEIGHT, PLAY_AREA_W...
#include "item_tracker.h" // for ItemTracker
#include "jail_audio.h" // for JA_PlaySound
#include "options.h" // for Options, options, OptionsVideo, Opti...
#include "resource.h" // for Resource
#include "scoreboard.h" // for ScoreboardData
#include "screen.h" // for Screen
#include "sprite.h" // for Sprite
#include "texture.h" // for Texture
#include "utils.h" // for LineHorizontal, LineDiagonal, LineVe...
// Carga las variables y texturas desde un fichero de mapa de tiles
std::vector<int> loadRoomTileFile(const std::string &file_path, bool verbose)
@@ -402,60 +403,13 @@ Room::Room(const std::string &room_path, std::shared_ptr<ScoreboardData> data)
: data_(data)
{
auto room = Resource::get()->getRoom(room_path);
number_ = room->number;
name_ = room->name;
bg_color_ = room->bg_color;
border_color_ = room->border_color;
item_color1_ = room->item_color1 == "" ? "yellow" : room->item_color1;
item_color2_ = room->item_color2 == "" ? "magenta" : room->item_color2;
room_top_ = room->room_top;
room_bottom_ = room->room_bottom;
room_left_ = room->room_left;
room_right_ = room->room_right;
tile_set_file_ = room->tile_set_file;
tile_map_file_ = room->tile_map_file;
auto_surface_direction_ = room->auto_surface_direction;
tile_map_ = Resource::get()->getTileMap(room->tile_map_file);
texture_ = Resource::get()->getTexture(room->tile_set_file);
initializeRoom(*room);
// Inicializa variables
tile_set_width_ = texture_->getWidth() / TILE_SIZE_;
is_paused_ = false;
counter_ = 0;
// Abre la Jail si se da el caso
openTheJail();
// Crea los enemigos
for (auto &enemy_data : room->enemies)
{
enemies_.emplace_back(std::make_shared<Enemy>(enemy_data));
}
// Crea los items
for (auto &item : room->items)
{
const SDL_Point itemPos = {item.x, item.y};
if (!ItemTracker::get()->hasBeenPicked(room->name, itemPos))
{
item.color1 = stringToColor(options.video.palette, item_color1_);
item.color2 = stringToColor(options.video.palette, item_color2_);
items_.emplace_back(std::make_shared<Item>(item));
}
}
// Abre la jail para poder entrar
if (data_->jail_is_open)
{
openTheJail();
}
// Calcula las superficies
setBottomSurfaces();
setTopSurfaces();
setLeftSurfaces();
setRightSurfaces();
setLeftSlopes();
setRightSlopes();
setAutoSurfaces();
// Inicializa las superficies de colision
initRoomSurfaces();
// Busca los tiles animados
setAnimatedTiles();
@@ -475,16 +429,14 @@ Room::Room(const std::string &room_path, std::shared_ptr<ScoreboardData> data)
fillMapTexture();
// Establece el color del borde
Screen::get()->setBorderColor(stringToColor(options.video.palette, room->border_color));
Screen::get()->setBorderColor(stringToColor(options.video.palette, border_color_));
}
// Destructor
Room::~Room()
{
SDL_DestroyTexture(map_texture_);
}
Room::~Room() { SDL_DestroyTexture(map_texture_); }
void Room::initializeRoom(const RoomData& room) {
void Room::initializeRoom(const RoomData &room)
{
// Asignar valores a las variables miembro
number_ = room.number;
name_ = room.name;
@@ -506,40 +458,29 @@ void Room::initializeRoom(const RoomData& room) {
counter_ = 0;
// Crear los enemigos
for (auto &enemy_data : room.enemies) {
for (auto &enemy_data : room.enemies)
{
enemies_.emplace_back(std::make_shared<Enemy>(enemy_data));
}
// Crear los items
for (auto &item : room.items) {
for (const auto &item : room.items)
{
const SDL_Point itemPos = {item.x, item.y};
if (!ItemTracker::get()->hasBeenPicked(room.name, itemPos)) {
item.color1 = stringToColor(options.video.palette, item_color1_);
item.color2 = stringToColor(options.video.palette, item_color2_);
items_.emplace_back(std::make_shared<Item>(item));
if (!ItemTracker::get()->hasBeenPicked(room.name, itemPos))
{
// Crear una copia local de los datos del item
ItemData itemCopy = item;
itemCopy.color1 = stringToColor(options.video.palette, item_color1_);
itemCopy.color2 = stringToColor(options.video.palette, item_color2_);
// Crear el objeto Item usando la copia modificada
items_.emplace_back(std::make_shared<Item>(itemCopy));
}
}
}
// Devuelve el nombre de la habitación
std::string Room::getName()
{
return name_;
}
// Devuelve el color de la habitación
Color Room::getBGColor()
{
return stringToColor(options.video.palette, bg_color_);
}
// Devuelve el color del borde
Color Room::getBorderColor()
{
return stringToColor(options.video.palette, border_color_);
}
// Crea la textura con el mapeado de la habitación
void Room::fillMapTexture()
{
@@ -687,7 +628,7 @@ void Room::renderMap()
// Dibuja los enemigos en pantalla
void Room::renderEnemies()
{
for (auto enemy : enemies_)
for (const auto &enemy : enemies_)
{
enemy->render();
}
@@ -696,7 +637,7 @@ void Room::renderEnemies()
// Dibuja los objetos en pantalla
void Room::renderItems()
{
for (auto item : items_)
for (const auto &item : items_)
{
item->render();
}
@@ -706,7 +647,8 @@ void Room::renderItems()
void Room::update()
{
if (is_paused_)
{ // Si está en modo pausa no se actualiza nada
{
// Si está en modo pausa no se actualiza nada
return;
}
@@ -717,12 +659,14 @@ void Room::update()
updateAnimatedTiles();
for (auto enemy : enemies_)
{ // Actualiza los enemigos
{
// Actualiza los enemigos
enemy->update();
}
for (auto item : items_)
{ // Actualiza los items
{
// Actualiza los items
item->update();
}
}
@@ -755,14 +699,14 @@ std::string Room::getRoom(int border)
}
// Devuelve el tipo de tile que hay en ese pixel
tile_e Room::getTile(SDL_Point point)
TileType Room::getTile(SDL_Point point)
{
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
tile_e Room::getTile(int index)
TileType Room::getTile(int index)
{
// const bool onRange = (index > -1) && (index < mapWidth * mapHeight);
const bool onRange = (index > -1) && (index < (int)tile_map_.size());
@@ -772,64 +716,64 @@ tile_e Room::getTile(int index)
// Las filas 0-8 son de tiles t_wall
if ((tile_map_[index] >= 0) && (tile_map_[index] < 9 * tile_set_width_))
{
return t_wall;
return TileType::WALL;
}
// Las filas 9-17 son de tiles t_passable
else if ((tile_map_[index] >= 9 * tile_set_width_) && (tile_map_[index] < 18 * tile_set_width_))
{
return t_passable;
return TileType::PASSABLE;
}
// Las filas 18-20 es de tiles t_animated
else if ((tile_map_[index] >= 18 * tile_set_width_) && (tile_map_[index] < 21 * tile_set_width_))
{
return t_animated;
return TileType::ANIMATED;
}
// La fila 21 es de tiles t_slope_r
else if ((tile_map_[index] >= 21 * tile_set_width_) && (tile_map_[index] < 22 * tile_set_width_))
{
return t_slope_r;
return TileType::SLOPE_R;
}
// La fila 22 es de tiles t_slope_l
else if ((tile_map_[index] >= 22 * tile_set_width_) && (tile_map_[index] < 23 * tile_set_width_))
{
return t_slope_l;
return TileType::SLOPE_L;
}
// La fila 23 es de tiles t_kill
else if ((tile_map_[index] >= 23 * tile_set_width_) && (tile_map_[index] < 24 * tile_set_width_))
{
return t_kill;
return TileType::KILL;
}
}
return t_empty;
return TileType::EMPTY;
}
// Indica si hay colision con un enemigo a partir de un rectangulo
bool Room::enemyCollision(SDL_Rect &rect)
{
bool collision = false;
for (auto enemy : enemies_)
for (const auto &enemy : enemies_)
{
collision |= checkCollision(rect, enemy->getCollider());
if (checkCollision(rect, enemy->getCollider()))
{
return true;
}
}
return collision;
return false;
}
// Indica si hay colision con un objeto a partir de un rectangulo
bool Room::itemCollision(SDL_Rect &rect)
{
for (int i = 0; i < (int)items_.size(); ++i)
for (int i = 0; i < static_cast<int>(items_.size()); ++i)
{
if (checkCollision(rect, items_[i]->getCollider()))
if (checkCollision(rect, items_.at(i)->getCollider()))
{
ItemTracker::get()->addItem(name_, items_[i]->getPos());
ItemTracker::get()->addItem(name_, items_.at(i)->getPos());
items_.erase(items_.begin() + i);
JA_PlaySound(Resource::get()->getSound("item.wav"));
data_->items++;
@@ -842,7 +786,7 @@ bool Room::itemCollision(SDL_Rect &rect)
}
// Obten la coordenada de la cuesta a partir de un punto perteneciente a ese tile
int Room::getSlopeHeight(SDL_Point p, tile_e slope)
int Room::getSlopeHeight(SDL_Point p, TileType slope)
{
// Calcula la base del tile
int base = ((p.y / TILE_SIZE_) * TILE_SIZE_) + TILE_SIZE_;
@@ -857,7 +801,7 @@ int Room::getSlopeHeight(SDL_Point p, tile_e slope)
#endif
// Se resta a la base la cantidad de pixeles pos en funcion de la rampa
if (slope == t_slope_r)
if (slope == TileType::SLOPE_R)
{
base -= pos + 1;
#ifdef DEBUG
@@ -884,7 +828,7 @@ void Room::setBottomSurfaces()
// Hay que recorrer la habitación por filas (excepto los de la última fila)
for (int i = 0; i < (int)tile_map_.size() - MAP_WIDTH_; ++i)
{
if (getTile(i) == t_wall && getTile(i + MAP_WIDTH_) != t_wall)
if (getTile(i) == TileType::WALL && getTile(i + MAP_WIDTH_) != TileType::WALL)
{
tile.push_back(i);
@@ -905,7 +849,7 @@ void Room::setBottomSurfaces()
int i = 0;
do
{
h_line_t line;
LineHorizontal line;
line.x1 = (tile[i] % MAP_WIDTH_) * TILE_SIZE_;
line.y = ((tile[i] / MAP_WIDTH_) * TILE_SIZE_) + TILE_SIZE_ - 1;
int last_one = i;
@@ -946,7 +890,7 @@ void Room::setTopSurfaces()
// Hay que recorrer la habitación por filas (excepto los de la primera fila)
for (int i = MAP_WIDTH_; i < (int)tile_map_.size(); ++i)
{
if ((getTile(i) == t_wall || getTile(i) == t_passable) && getTile(i - MAP_WIDTH_) != t_wall)
if ((getTile(i) == TileType::WALL || getTile(i) == TileType::PASSABLE) && getTile(i - MAP_WIDTH_) != TileType::WALL)
{
tile.push_back(i);
@@ -967,7 +911,7 @@ void Room::setTopSurfaces()
int i = 0;
do
{
h_line_t line;
LineHorizontal line;
line.x1 = (tile[i] % MAP_WIDTH_) * TILE_SIZE_;
line.y = (tile[i] / MAP_WIDTH_) * TILE_SIZE_;
int last_one = i;
@@ -1011,7 +955,7 @@ void Room::setLeftSurfaces()
for (int j = 0; j < MAP_HEIGHT_; ++j)
{
const int pos = (j * MAP_WIDTH_ + i);
if (getTile(pos) == t_wall && getTile(pos - 1) != t_wall)
if (getTile(pos) == TileType::WALL && getTile(pos - 1) != TileType::WALL)
{
tile.push_back(pos);
}
@@ -1029,7 +973,7 @@ void Room::setLeftSurfaces()
int i = 0;
do
{
v_line_t line;
LineVertical line;
line.x = (tile[i] % MAP_WIDTH_) * TILE_SIZE_;
line.y1 = ((tile[i] / MAP_WIDTH_) * TILE_SIZE_);
while (tile[i] + MAP_WIDTH_ == tile[i + 1])
@@ -1059,7 +1003,7 @@ void Room::setRightSurfaces()
for (int j = 0; j < MAP_HEIGHT_; ++j)
{
const int pos = (j * MAP_WIDTH_ + i);
if (getTile(pos) == t_wall && getTile(pos + 1) != t_wall)
if (getTile(pos) == TileType::WALL && getTile(pos + 1) != TileType::WALL)
{
tile.push_back(pos);
}
@@ -1077,7 +1021,7 @@ void Room::setRightSurfaces()
int i = 0;
do
{
v_line_t line;
LineVertical line;
line.x = ((tile[i] % MAP_WIDTH_) * TILE_SIZE_) + TILE_SIZE_ - 1;
line.y1 = ((tile[i] / MAP_WIDTH_) * TILE_SIZE_);
while (tile[i] + MAP_WIDTH_ == tile[i + 1])
@@ -1102,7 +1046,7 @@ void Room::setLeftSlopes()
std::vector<int> found;
for (int i = 0; i < (int)tile_map_.size(); ++i)
{
if (getTile(i) == t_slope_l)
if (getTile(i) == TileType::SLOPE_L)
{
found.push_back(i);
}
@@ -1114,7 +1058,7 @@ void Room::setLeftSlopes()
while (found.size() > 0)
{
d_line_t line;
LineDiagonal line;
line.x1 = (found[0] % MAP_WIDTH_) * TILE_SIZE_;
line.y1 = (found[0] / MAP_WIDTH_) * TILE_SIZE_;
int lookingFor = found[0] + MAP_WIDTH_ + 1;
@@ -1143,7 +1087,7 @@ void Room::setRightSlopes()
std::vector<int> found;
for (int i = 0; i < (int)tile_map_.size(); ++i)
{
if (getTile(i) == t_slope_r)
if (getTile(i) == TileType::SLOPE_R)
{
found.push_back(i);
}
@@ -1155,7 +1099,7 @@ void Room::setRightSlopes()
while (found.size() > 0)
{
d_line_t line;
LineDiagonal line;
line.x1 = ((found[0] % MAP_WIDTH_) * TILE_SIZE_) + TILE_SIZE_ - 1;
line.y1 = (found[0] / MAP_WIDTH_) * TILE_SIZE_;
int lookingFor = found[0] + MAP_WIDTH_ - 1;
@@ -1186,7 +1130,7 @@ void Room::setAutoSurfaces()
// Hay que recorrer la habitación por filas (excepto los de la primera fila)
for (int i = MAP_WIDTH_; i < (int)tile_map_.size(); ++i)
{
if (getTile(i) == t_animated)
if (getTile(i) == TileType::ANIMATED)
{
tile.push_back(i);
@@ -1204,7 +1148,7 @@ void Room::setAutoSurfaces()
int i = 0;
do
{
h_line_t line;
LineHorizontal line;
line.x1 = (tile[i] % MAP_WIDTH_) * TILE_SIZE_;
line.y = (tile[i] / MAP_WIDTH_) * TILE_SIZE_;
int last_one = i;
@@ -1242,7 +1186,7 @@ void Room::setAnimatedTiles()
// Recorre la habitación entera por filas buscando tiles de tipo t_animated
for (int i = 0; i < (int)tile_map_.size(); ++i)
{
if (getTile(i) == t_animated)
if (getTile(i) == TileType::ANIMATED)
{
// La i es la ubicación
const int x = (i % MAP_WIDTH_) * TILE_SIZE_;
@@ -1286,7 +1230,7 @@ void Room::updateAnimatedTiles()
// Pinta los tiles animados en pantalla
void Room::renderAnimatedTiles()
{
for (auto a : animated_tiles_)
for (const auto &a : animated_tiles_)
{
a.sprite->render();
}
@@ -1295,7 +1239,7 @@ void Room::renderAnimatedTiles()
// Comprueba las colisiones
int Room::checkRightSurfaces(SDL_Rect *rect)
{
for (auto s : right_surfaces_)
for (const auto &s : right_surfaces_)
{
if (checkCollision(s, *rect))
{
@@ -1309,7 +1253,7 @@ int Room::checkRightSurfaces(SDL_Rect *rect)
// Comprueba las colisiones
int Room::checkLeftSurfaces(SDL_Rect *rect)
{
for (auto s : left_surfaces_)
for (const auto &s : left_surfaces_)
{
if (checkCollision(s, *rect))
{
@@ -1323,7 +1267,7 @@ int Room::checkLeftSurfaces(SDL_Rect *rect)
// Comprueba las colisiones
int Room::checkTopSurfaces(SDL_Rect *rect)
{
for (auto s : top_surfaces_)
for (const auto &s : top_surfaces_)
{
if (checkCollision(s, *rect))
{
@@ -1337,7 +1281,7 @@ int Room::checkTopSurfaces(SDL_Rect *rect)
// Comprueba las colisiones
int Room::checkBottomSurfaces(SDL_Rect *rect)
{
for (auto s : bottom_surfaces_)
for (const auto &s : bottom_surfaces_)
{
if (checkCollision(s, *rect))
{
@@ -1351,7 +1295,7 @@ int Room::checkBottomSurfaces(SDL_Rect *rect)
// Comprueba las colisiones
int Room::checkAutoSurfaces(SDL_Rect *rect)
{
for (auto s : auto_surfaces_)
for (const auto &s : auto_surfaces_)
{
if (checkCollision(s, *rect))
{
@@ -1365,7 +1309,7 @@ int Room::checkAutoSurfaces(SDL_Rect *rect)
// Comprueba las colisiones
bool Room::checkTopSurfaces(SDL_Point *p)
{
for (auto s : top_surfaces_)
for (const auto &s : top_surfaces_)
{
if (checkCollision(s, *p))
{
@@ -1379,7 +1323,7 @@ bool Room::checkTopSurfaces(SDL_Point *p)
// Comprueba las colisiones
bool Room::checkAutoSurfaces(SDL_Point *p)
{
for (auto s : auto_surfaces_)
for (const auto &s : auto_surfaces_)
{
if (checkCollision(s, *p))
{
@@ -1391,9 +1335,9 @@ bool Room::checkAutoSurfaces(SDL_Point *p)
}
// Comprueba las colisiones
int Room::checkLeftSlopes(const v_line_t *line)
int Room::checkLeftSlopes(const LineVertical *line)
{
for (auto slope : left_slopes_)
for (const auto &slope : left_slopes_)
{
const SDL_Point p = checkCollision(slope, *line);
if (p.x != -1)
@@ -1408,7 +1352,7 @@ int Room::checkLeftSlopes(const v_line_t *line)
// Comprueba las colisiones
bool Room::checkLeftSlopes(SDL_Point *p)
{
for (auto slope : left_slopes_)
for (const auto &slope : left_slopes_)
{
if (checkCollision(*p, slope))
{
@@ -1420,9 +1364,9 @@ bool Room::checkLeftSlopes(SDL_Point *p)
}
// Comprueba las colisiones
int Room::checkRightSlopes(const v_line_t *line)
int Room::checkRightSlopes(const LineVertical *line)
{
for (auto slope : right_slopes_)
for (const auto &slope : right_slopes_)
{
const SDL_Point p = checkCollision(slope, *line);
if (p.x != -1)
@@ -1437,7 +1381,7 @@ int Room::checkRightSlopes(const v_line_t *line)
// Comprueba las colisiones
bool Room::checkRightSlopes(SDL_Point *p)
{
for (auto slope : right_slopes_)
for (const auto &slope : right_slopes_)
{
if (checkCollision(*p, slope))
{
@@ -1448,24 +1392,39 @@ bool Room::checkRightSlopes(SDL_Point *p)
return false;
}
// Obten la direccion de las superficies automaticas
int Room::getAutoSurfaceDirection()
{
return auto_surface_direction_;
}
// Abre la jail para poder entrar
// Abre la Jail si se da el caso
void Room::openTheJail()
{
if (name_ == "THE JAIL")
if (data_->jail_is_open && name_ == "THE JAIL")
{
// Elimina el último enemigo (Bry debe ser el ultimo enemigo definido en el fichero)
enemies_.pop_back();
// Elimina el último enemigo (Bry debe ser el último enemigo definido en el fichero)
if (!enemies_.empty())
{
enemies_.pop_back();
}
// Abre las puertas
const int tileA = 16 + (13 * 32);
const int tileB = 16 + (14 * 32);
tile_map_[tileA] = -1;
tile_map_[tileB] = -1;
constexpr int TILE_A = 16 + (13 * 32);
constexpr int TILE_B = 16 + (14 * 32);
if (TILE_A < tile_map_.size())
{
tile_map_[TILE_A] = -1;
}
if (TILE_B < tile_map_.size())
{
tile_map_[TILE_B] = -1;
}
}
}
// Inicializa las superficies de colision
void Room::initRoomSurfaces()
{
setBottomSurfaces();
setTopSurfaces();
setLeftSurfaces();
setRightSurfaces();
setLeftSlopes();
setRightSlopes();
setAutoSurfaces();
}