forked from jaildesigner-jailgames/jaildoctors_dilemma
style: organitzada la capçalera de Room
This commit is contained in:
@@ -19,275 +19,6 @@
|
||||
#include "utils/defines.hpp" // Para BLOCK, PLAY_AREA_HEIGHT, PLAY_AREA_WIDTH
|
||||
#include "utils/utils.hpp" // Para LineHorizontal, LineDiagonal, LineVertical
|
||||
|
||||
// Carga las variables y texturas desde un fichero de mapa de tiles
|
||||
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);
|
||||
|
||||
// El fichero se puede abrir
|
||||
if (file.good()) {
|
||||
std::string line;
|
||||
// Procesa el fichero linea a linea
|
||||
while (std::getline(file, line)) { // Lee el fichero linea a linea
|
||||
if (line.find("data encoding") != std::string::npos) {
|
||||
// Lee la primera linea
|
||||
std::getline(file, line);
|
||||
while (line != "</data>") { // Procesa lineas mientras haya
|
||||
std::stringstream ss(line);
|
||||
std::string tmp;
|
||||
while (getline(ss, tmp, ',')) {
|
||||
tile_map_file.push_back(std::stoi(tmp) - 1);
|
||||
}
|
||||
|
||||
// Lee la siguiente linea
|
||||
std::getline(file, line);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Cierra el fichero
|
||||
if (verbose) {
|
||||
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" << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
return tile_map_file;
|
||||
}
|
||||
|
||||
// Parsea una línea en key y value separados por '='
|
||||
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};
|
||||
}
|
||||
|
||||
// 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() << "\"" << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
// Carga un bloque [enemy]...[/enemy] desde un archivo
|
||||
auto loadEnemyFromFile(std::ifstream& file, const std::string& file_name, bool verbose) -> Enemy::Data {
|
||||
Enemy::Data enemy;
|
||||
enemy.flip = false;
|
||||
enemy.mirror = false;
|
||||
enemy.frame = -1;
|
||||
|
||||
std::string line;
|
||||
do {
|
||||
std::getline(file, line);
|
||||
auto [key, value] = parseKeyValue(line);
|
||||
|
||||
if (!setEnemy(&enemy, key, value)) {
|
||||
logUnknownParameter(file_name, key, verbose);
|
||||
}
|
||||
} while (line != "[/enemy]");
|
||||
|
||||
return enemy;
|
||||
}
|
||||
|
||||
// Carga un bloque [item]...[/item] desde un archivo
|
||||
auto loadItemFromFile(std::ifstream& file, const std::string& file_name, bool verbose) -> Item::Data {
|
||||
Item::Data item;
|
||||
item.counter = 0;
|
||||
item.color1 = stringToColor("yellow");
|
||||
item.color2 = stringToColor("magenta");
|
||||
|
||||
std::string line;
|
||||
do {
|
||||
std::getline(file, line);
|
||||
auto [key, value] = parseKeyValue(line);
|
||||
|
||||
if (!setItem(&item, key, value)) {
|
||||
logUnknownParameter(file_name, key, verbose);
|
||||
}
|
||||
} while (line != "[/item]");
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
// Carga las variables desde un fichero de mapa
|
||||
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('.'));
|
||||
|
||||
std::ifstream file(file_path);
|
||||
|
||||
// El fichero se puede abrir
|
||||
if (file.good()) {
|
||||
std::string line;
|
||||
// Procesa el fichero linea a linea
|
||||
while (std::getline(file, line)) {
|
||||
// Si la linea contiene el texto [enemy] se realiza el proceso de carga de un enemigo
|
||||
if (line == "[enemy]") {
|
||||
room.enemies.push_back(loadEnemyFromFile(file, FILE_NAME, verbose));
|
||||
}
|
||||
// Si la linea contiene el texto [item] se realiza el proceso de carga de un item
|
||||
else if (line == "[item]") {
|
||||
room.items.push_back(loadItemFromFile(file, FILE_NAME, verbose));
|
||||
}
|
||||
// En caso contrario se parsea el fichero para buscar las variables y los valores
|
||||
else {
|
||||
auto [key, value] = parseKeyValue(line);
|
||||
if (!setRoom(&room, key, value)) {
|
||||
logUnknownParameter(FILE_NAME, key, verbose);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Cierra el fichero
|
||||
if (verbose) {
|
||||
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" << '\n';
|
||||
}
|
||||
|
||||
return room;
|
||||
}
|
||||
|
||||
// Asigna variables a una estructura RoomData
|
||||
auto setRoom(RoomData* room, const std::string& key, const std::string& value) -> bool {
|
||||
// Indicador de éxito en la asignación
|
||||
bool success = true;
|
||||
|
||||
try {
|
||||
if (key == "tileMapFile") {
|
||||
room->tile_map_file = value;
|
||||
} else if (key == "name") {
|
||||
room->name = value;
|
||||
} else if (key == "bgColor") {
|
||||
room->bg_color = value;
|
||||
} else if (key == "border") {
|
||||
room->border_color = value;
|
||||
} else if (key == "itemColor1") {
|
||||
room->item_color1 = value;
|
||||
} else if (key == "itemColor2") {
|
||||
room->item_color2 = value;
|
||||
} else if (key == "tileSetFile") {
|
||||
room->tile_set_file = value;
|
||||
} else if (key == "roomUp") {
|
||||
room->upper_room = value;
|
||||
} else if (key == "roomDown") {
|
||||
room->lower_room = value;
|
||||
} else if (key == "roomLeft") {
|
||||
room->left_room = value;
|
||||
} else if (key == "roomRight") {
|
||||
room->right_room = value;
|
||||
} else if (key == "autoSurface") {
|
||||
room->conveyor_belt_direction = (value == "right") ? 1 : -1;
|
||||
} else if (key.empty() || key.substr(0, 1) == "#") {
|
||||
// No se realiza ninguna acción para estas claves
|
||||
} else {
|
||||
success = false;
|
||||
}
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "Error al asignar la clave " << key << " con valor " << value << ": " << e.what() << '\n';
|
||||
success = false;
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
// Asigna variables a una estructura EnemyData
|
||||
auto setEnemy(Enemy::Data* enemy, const std::string& key, const std::string& value) -> bool {
|
||||
// Indicador de éxito en la asignación
|
||||
bool success = true;
|
||||
|
||||
try {
|
||||
if (key == "tileSetFile") {
|
||||
enemy->surface_path = value;
|
||||
} else if (key == "animation") {
|
||||
enemy->animation_path = value;
|
||||
} else if (key == "width") {
|
||||
enemy->w = std::stoi(value);
|
||||
} else if (key == "height") {
|
||||
enemy->h = std::stoi(value);
|
||||
} else if (key == "x") {
|
||||
enemy->x = std::stof(value) * BLOCK;
|
||||
} else if (key == "y") {
|
||||
enemy->y = std::stof(value) * BLOCK;
|
||||
} else if (key == "vx") {
|
||||
enemy->vx = std::stof(value);
|
||||
} else if (key == "vy") {
|
||||
enemy->vy = std::stof(value);
|
||||
} else if (key == "x1") {
|
||||
enemy->x1 = std::stoi(value) * BLOCK;
|
||||
} else if (key == "x2") {
|
||||
enemy->x2 = std::stoi(value) * BLOCK;
|
||||
} else if (key == "y1") {
|
||||
enemy->y1 = std::stoi(value) * BLOCK;
|
||||
} else if (key == "y2") {
|
||||
enemy->y2 = std::stoi(value) * BLOCK;
|
||||
} else if (key == "flip") {
|
||||
enemy->flip = stringToBool(value);
|
||||
} else if (key == "mirror") {
|
||||
enemy->mirror = stringToBool(value);
|
||||
} else if (key == "color") {
|
||||
enemy->color = value;
|
||||
} else if (key == "frame") {
|
||||
enemy->frame = std::stoi(value);
|
||||
} else if (key == "[/enemy]" || key == "tileSetFile" || key.substr(0, 1) == "#") {
|
||||
// No se realiza ninguna acción para estas claves
|
||||
} else {
|
||||
success = false;
|
||||
}
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "Error al asignar la clave " << key << " con valor " << value << ": " << e.what() << '\n';
|
||||
success = false;
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
// Asigna variables a una estructura ItemData
|
||||
auto setItem(Item::Data* item, const std::string& key, const std::string& value) -> bool {
|
||||
// Indicador de éxito en la asignación
|
||||
bool success = true;
|
||||
|
||||
try {
|
||||
if (key == "tileSetFile") {
|
||||
item->tile_set_file = value;
|
||||
} else if (key == "counter") {
|
||||
item->counter = std::stoi(value);
|
||||
} else if (key == "x") {
|
||||
item->x = std::stof(value) * BLOCK;
|
||||
} else if (key == "y") {
|
||||
item->y = std::stof(value) * BLOCK;
|
||||
} else if (key == "tile") {
|
||||
item->tile = std::stof(value);
|
||||
} else if (key == "[/item]") {
|
||||
// No se realiza ninguna acción para esta clave
|
||||
} else {
|
||||
success = false;
|
||||
}
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "Error al asignar la clave " << key << " con valor " << value << ": " << e.what() << '\n';
|
||||
success = false;
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
// Constructor
|
||||
Room::Room(const std::string& room_path, std::shared_ptr<ScoreboardData> data)
|
||||
: data_(std::move(std::move(data))) {
|
||||
@@ -313,7 +44,7 @@ Room::Room(const std::string& room_path, std::shared_ptr<ScoreboardData> data)
|
||||
Screen::get()->setBorderColor(stringToColor(border_color_));
|
||||
}
|
||||
|
||||
void Room::initializeRoom(const RoomData& room) {
|
||||
void Room::initializeRoom(const Data& room) {
|
||||
// Asignar valores a las variables miembro
|
||||
number_ = room.number;
|
||||
name_ = room.name;
|
||||
@@ -495,21 +226,21 @@ void Room::update(float delta_time) {
|
||||
}
|
||||
|
||||
// Devuelve la cadena del fichero de la habitación contigua segun el borde
|
||||
auto Room::getRoom(RoomBorder border) -> std::string {
|
||||
auto Room::getRoom(Border border) -> std::string {
|
||||
switch (border) {
|
||||
case RoomBorder::TOP:
|
||||
case Border::TOP:
|
||||
return upper_room_;
|
||||
break;
|
||||
|
||||
case RoomBorder::BOTTOM:
|
||||
case Border::BOTTOM:
|
||||
return lower_room_;
|
||||
break;
|
||||
|
||||
case RoomBorder::RIGHT:
|
||||
case Border::RIGHT:
|
||||
return right_room_;
|
||||
break;
|
||||
|
||||
case RoomBorder::LEFT:
|
||||
case Border::LEFT:
|
||||
return left_room_;
|
||||
break;
|
||||
|
||||
@@ -520,49 +251,49 @@ auto Room::getRoom(RoomBorder border) -> std::string {
|
||||
}
|
||||
|
||||
// Devuelve el tipo de tile que hay en ese pixel
|
||||
auto Room::getTile(SDL_FPoint point) -> TileType {
|
||||
auto Room::getTile(SDL_FPoint point) -> Tile {
|
||||
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
|
||||
auto Room::getTile(int index) -> TileType {
|
||||
auto Room::getTile(int index) -> Tile {
|
||||
// const bool onRange = (index > -1) && (index < mapWidth * mapHeight);
|
||||
const bool ON_RANGE = (index > -1) && (index < (int)tile_map_.size());
|
||||
|
||||
if (ON_RANGE) {
|
||||
// Las filas 0-8 son de tiles t_wall
|
||||
if ((tile_map_[index] >= 0) && (tile_map_[index] < 9 * tile_set_width_)) {
|
||||
return TileType::WALL;
|
||||
return Tile::WALL;
|
||||
}
|
||||
|
||||
// Las filas 9-17 son de tiles t_passable
|
||||
if ((tile_map_[index] >= 9 * tile_set_width_) && (tile_map_[index] < 18 * tile_set_width_)) {
|
||||
return TileType::PASSABLE;
|
||||
return Tile::PASSABLE;
|
||||
}
|
||||
|
||||
// Las filas 18-20 es de tiles t_animated
|
||||
if ((tile_map_[index] >= 18 * tile_set_width_) && (tile_map_[index] < 21 * tile_set_width_)) {
|
||||
return TileType::ANIMATED;
|
||||
return Tile::ANIMATED;
|
||||
}
|
||||
|
||||
// La fila 21 es de tiles t_slope_r
|
||||
if ((tile_map_[index] >= 21 * tile_set_width_) && (tile_map_[index] < 22 * tile_set_width_)) {
|
||||
return TileType::SLOPE_R;
|
||||
return Tile::SLOPE_R;
|
||||
}
|
||||
|
||||
// La fila 22 es de tiles t_slope_l
|
||||
if ((tile_map_[index] >= 22 * tile_set_width_) && (tile_map_[index] < 23 * tile_set_width_)) {
|
||||
return TileType::SLOPE_L;
|
||||
return Tile::SLOPE_L;
|
||||
}
|
||||
|
||||
// La fila 23 es de tiles t_kill
|
||||
if ((tile_map_[index] >= 23 * tile_set_width_) && (tile_map_[index] < 24 * tile_set_width_)) {
|
||||
return TileType::KILL;
|
||||
return Tile::KILL;
|
||||
}
|
||||
}
|
||||
|
||||
return TileType::EMPTY;
|
||||
return Tile::EMPTY;
|
||||
}
|
||||
|
||||
// Indica si hay colision con un enemigo a partir de un rectangulo
|
||||
@@ -589,7 +320,7 @@ auto Room::itemCollision(SDL_FRect& rect) -> bool {
|
||||
}
|
||||
|
||||
// Obten la coordenada de la cuesta a partir de un punto perteneciente a ese tile
|
||||
auto Room::getSlopeHeight(SDL_FPoint p, TileType slope) -> int {
|
||||
auto Room::getSlopeHeight(SDL_FPoint p, Tile slope) -> int {
|
||||
// Calcula la base del tile
|
||||
int base = ((p.y / TILE_SIZE) * TILE_SIZE) + TILE_SIZE;
|
||||
#ifdef _DEBUG
|
||||
@@ -603,7 +334,7 @@ auto Room::getSlopeHeight(SDL_FPoint p, TileType slope) -> int {
|
||||
#endif
|
||||
|
||||
// Se resta a la base la cantidad de pixeles pos en funcion de la rampa
|
||||
if (slope == TileType::SLOPE_R) {
|
||||
if (slope == Tile::SLOPE_R) {
|
||||
base -= POS + 1;
|
||||
#ifdef _DEBUG
|
||||
Debug::get()->add("BASE_R = " + std::to_string(base));
|
||||
@@ -625,7 +356,7 @@ auto Room::collectBottomTiles() -> std::vector<int> {
|
||||
// Busca todos los tiles de tipo muro que no tengan debajo otro muro
|
||||
// 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) == TileType::WALL && getTile(i + MAP_WIDTH) != TileType::WALL) {
|
||||
if (getTile(i) == Tile::WALL && getTile(i + MAP_WIDTH) != Tile::WALL) {
|
||||
tile.push_back(i);
|
||||
|
||||
// Si llega al final de la fila, introduce un separador
|
||||
@@ -647,7 +378,7 @@ auto Room::collectTopTiles() -> std::vector<int> {
|
||||
// Busca todos los tiles de tipo muro o pasable que no tengan encima un muro
|
||||
// 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) == TileType::WALL || getTile(i) == TileType::PASSABLE) && getTile(i - MAP_WIDTH) != TileType::WALL) {
|
||||
if ((getTile(i) == Tile::WALL || getTile(i) == Tile::PASSABLE) && getTile(i - MAP_WIDTH) != Tile::WALL) {
|
||||
tile.push_back(i);
|
||||
|
||||
// Si llega al final de la fila, introduce un separador
|
||||
@@ -725,7 +456,7 @@ void Room::setLeftSurfaces() {
|
||||
for (int i = 1; i < MAP_WIDTH; ++i) {
|
||||
for (int j = 0; j < MAP_HEIGHT; ++j) {
|
||||
const int POS = ((j * MAP_WIDTH) + i);
|
||||
if (getTile(POS) == TileType::WALL && getTile(POS - 1) != TileType::WALL) {
|
||||
if (getTile(POS) == Tile::WALL && getTile(POS - 1) != Tile::WALL) {
|
||||
tile.push_back(POS);
|
||||
}
|
||||
}
|
||||
@@ -765,7 +496,7 @@ void Room::setRightSurfaces() {
|
||||
for (int i = 0; i < MAP_WIDTH - 1; ++i) {
|
||||
for (int j = 0; j < MAP_HEIGHT; ++j) {
|
||||
const int POS = ((j * MAP_WIDTH) + i);
|
||||
if (getTile(POS) == TileType::WALL && getTile(POS + 1) != TileType::WALL) {
|
||||
if (getTile(POS) == Tile::WALL && getTile(POS + 1) != Tile::WALL) {
|
||||
tile.push_back(POS);
|
||||
}
|
||||
}
|
||||
@@ -801,7 +532,7 @@ void Room::setLeftSlopes() {
|
||||
// Recorre la habitación entera por filas buscando tiles de tipo t_slope_l
|
||||
std::vector<int> found;
|
||||
for (int i = 0; i < (int)tile_map_.size(); ++i) {
|
||||
if (getTile(i) == TileType::SLOPE_L) {
|
||||
if (getTile(i) == Tile::SLOPE_L) {
|
||||
found.push_back(i);
|
||||
}
|
||||
}
|
||||
@@ -836,7 +567,7 @@ void Room::setRightSlopes() {
|
||||
// Recorre la habitación entera por filas buscando tiles de tipo t_slope_r
|
||||
std::vector<int> found;
|
||||
for (int i = 0; i < (int)tile_map_.size(); ++i) {
|
||||
if (getTile(i) == TileType::SLOPE_R) {
|
||||
if (getTile(i) == Tile::SLOPE_R) {
|
||||
found.push_back(i);
|
||||
}
|
||||
}
|
||||
@@ -874,7 +605,7 @@ auto Room::collectAnimatedTiles() -> std::vector<int> {
|
||||
// Busca todos los tiles de tipo animado
|
||||
// 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) == TileType::ANIMATED) {
|
||||
if (getTile(i) == Tile::ANIMATED) {
|
||||
tile.push_back(i);
|
||||
|
||||
// Si llega al final de la fila, introduce un separador
|
||||
@@ -901,7 +632,7 @@ void Room::setAutoSurfaces() {
|
||||
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) == TileType::ANIMATED) {
|
||||
if (getTile(i) == Tile::ANIMATED) {
|
||||
// La i es la ubicación
|
||||
const int X = (i % MAP_WIDTH) * TILE_SIZE;
|
||||
const int Y = (i / MAP_WIDTH) * TILE_SIZE;
|
||||
@@ -1079,4 +810,273 @@ void Room::initRoomSurfaces() {
|
||||
setLeftSlopes();
|
||||
setRightSlopes();
|
||||
setAutoSurfaces();
|
||||
}
|
||||
|
||||
// Asigna variables a una estructura RoomData
|
||||
auto Room::setRoom(Data* room, const std::string& key, const std::string& value) -> bool {
|
||||
// Indicador de éxito en la asignación
|
||||
bool success = true;
|
||||
|
||||
try {
|
||||
if (key == "tileMapFile") {
|
||||
room->tile_map_file = value;
|
||||
} else if (key == "name") {
|
||||
room->name = value;
|
||||
} else if (key == "bgColor") {
|
||||
room->bg_color = value;
|
||||
} else if (key == "border") {
|
||||
room->border_color = value;
|
||||
} else if (key == "itemColor1") {
|
||||
room->item_color1 = value;
|
||||
} else if (key == "itemColor2") {
|
||||
room->item_color2 = value;
|
||||
} else if (key == "tileSetFile") {
|
||||
room->tile_set_file = value;
|
||||
} else if (key == "roomUp") {
|
||||
room->upper_room = value;
|
||||
} else if (key == "roomDown") {
|
||||
room->lower_room = value;
|
||||
} else if (key == "roomLeft") {
|
||||
room->left_room = value;
|
||||
} else if (key == "roomRight") {
|
||||
room->right_room = value;
|
||||
} else if (key == "autoSurface") {
|
||||
room->conveyor_belt_direction = (value == "right") ? 1 : -1;
|
||||
} else if (key.empty() || key.substr(0, 1) == "#") {
|
||||
// No se realiza ninguna acción para estas claves
|
||||
} else {
|
||||
success = false;
|
||||
}
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "Error al asignar la clave " << key << " con valor " << value << ": " << e.what() << '\n';
|
||||
success = false;
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
// Asigna variables a una estructura EnemyData
|
||||
auto Room::setEnemy(Enemy::Data* enemy, const std::string& key, const std::string& value) -> bool {
|
||||
// Indicador de éxito en la asignación
|
||||
bool success = true;
|
||||
|
||||
try {
|
||||
if (key == "tileSetFile") {
|
||||
enemy->surface_path = value;
|
||||
} else if (key == "animation") {
|
||||
enemy->animation_path = value;
|
||||
} else if (key == "width") {
|
||||
enemy->w = std::stoi(value);
|
||||
} else if (key == "height") {
|
||||
enemy->h = std::stoi(value);
|
||||
} else if (key == "x") {
|
||||
enemy->x = std::stof(value) * BLOCK;
|
||||
} else if (key == "y") {
|
||||
enemy->y = std::stof(value) * BLOCK;
|
||||
} else if (key == "vx") {
|
||||
enemy->vx = std::stof(value);
|
||||
} else if (key == "vy") {
|
||||
enemy->vy = std::stof(value);
|
||||
} else if (key == "x1") {
|
||||
enemy->x1 = std::stoi(value) * BLOCK;
|
||||
} else if (key == "x2") {
|
||||
enemy->x2 = std::stoi(value) * BLOCK;
|
||||
} else if (key == "y1") {
|
||||
enemy->y1 = std::stoi(value) * BLOCK;
|
||||
} else if (key == "y2") {
|
||||
enemy->y2 = std::stoi(value) * BLOCK;
|
||||
} else if (key == "flip") {
|
||||
enemy->flip = stringToBool(value);
|
||||
} else if (key == "mirror") {
|
||||
enemy->mirror = stringToBool(value);
|
||||
} else if (key == "color") {
|
||||
enemy->color = value;
|
||||
} else if (key == "frame") {
|
||||
enemy->frame = std::stoi(value);
|
||||
} else if (key == "[/enemy]" || key == "tileSetFile" || key.substr(0, 1) == "#") {
|
||||
// No se realiza ninguna acción para estas claves
|
||||
} else {
|
||||
success = false;
|
||||
}
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "Error al asignar la clave " << key << " con valor " << value << ": " << e.what() << '\n';
|
||||
success = false;
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
// Asigna variables a una estructura ItemData
|
||||
auto Room::setItem(Item::Data* item, const std::string& key, const std::string& value) -> bool {
|
||||
// Indicador de éxito en la asignación
|
||||
bool success = true;
|
||||
|
||||
try {
|
||||
if (key == "tileSetFile") {
|
||||
item->tile_set_file = value;
|
||||
} else if (key == "counter") {
|
||||
item->counter = std::stoi(value);
|
||||
} else if (key == "x") {
|
||||
item->x = std::stof(value) * BLOCK;
|
||||
} else if (key == "y") {
|
||||
item->y = std::stof(value) * BLOCK;
|
||||
} else if (key == "tile") {
|
||||
item->tile = std::stof(value);
|
||||
} else if (key == "[/item]") {
|
||||
// No se realiza ninguna acción para esta clave
|
||||
} else {
|
||||
success = false;
|
||||
}
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "Error al asignar la clave " << key << " con valor " << value << ": " << e.what() << '\n';
|
||||
success = false;
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
// Carga las variables y texturas desde un fichero de mapa de tiles
|
||||
auto Room::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);
|
||||
|
||||
// El fichero se puede abrir
|
||||
if (file.good()) {
|
||||
std::string line;
|
||||
// Procesa el fichero linea a linea
|
||||
while (std::getline(file, line)) { // Lee el fichero linea a linea
|
||||
if (line.find("data encoding") != std::string::npos) {
|
||||
// Lee la primera linea
|
||||
std::getline(file, line);
|
||||
while (line != "</data>") { // Procesa lineas mientras haya
|
||||
std::stringstream ss(line);
|
||||
std::string tmp;
|
||||
while (getline(ss, tmp, ',')) {
|
||||
tile_map_file.push_back(std::stoi(tmp) - 1);
|
||||
}
|
||||
|
||||
// Lee la siguiente linea
|
||||
std::getline(file, line);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Cierra el fichero
|
||||
if (verbose) {
|
||||
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" << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
return tile_map_file;
|
||||
}
|
||||
|
||||
// Carga las variables desde un fichero de mapa
|
||||
auto Room::loadRoomFile(const std::string& file_path, bool verbose) -> Data {
|
||||
Data 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('.'));
|
||||
|
||||
std::ifstream file(file_path);
|
||||
|
||||
// El fichero se puede abrir
|
||||
if (file.good()) {
|
||||
std::string line;
|
||||
// Procesa el fichero linea a linea
|
||||
while (std::getline(file, line)) {
|
||||
// Si la linea contiene el texto [enemy] se realiza el proceso de carga de un enemigo
|
||||
if (line == "[enemy]") {
|
||||
room.enemies.push_back(loadEnemyFromFile(file, FILE_NAME, verbose));
|
||||
}
|
||||
// Si la linea contiene el texto [item] se realiza el proceso de carga de un item
|
||||
else if (line == "[item]") {
|
||||
room.items.push_back(loadItemFromFile(file, FILE_NAME, verbose));
|
||||
}
|
||||
// En caso contrario se parsea el fichero para buscar las variables y los valores
|
||||
else {
|
||||
auto [key, value] = parseKeyValue(line);
|
||||
if (!setRoom(&room, key, value)) {
|
||||
logUnknownParameter(FILE_NAME, key, verbose);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Cierra el fichero
|
||||
if (verbose) {
|
||||
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" << '\n';
|
||||
}
|
||||
|
||||
return room;
|
||||
}
|
||||
|
||||
// Parsea una línea en key y value separados por '='
|
||||
auto Room::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};
|
||||
}
|
||||
|
||||
// Muestra un warning de parámetro desconocido
|
||||
void Room::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() << "\"" << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
// Carga un bloque [enemy]...[/enemy] desde un archivo
|
||||
auto Room::loadEnemyFromFile(std::ifstream& file, const std::string& file_name, bool verbose) -> Enemy::Data {
|
||||
Enemy::Data enemy;
|
||||
enemy.flip = false;
|
||||
enemy.mirror = false;
|
||||
enemy.frame = -1;
|
||||
|
||||
std::string line;
|
||||
do {
|
||||
std::getline(file, line);
|
||||
auto [key, value] = parseKeyValue(line);
|
||||
|
||||
if (!setEnemy(&enemy, key, value)) {
|
||||
logUnknownParameter(file_name, key, verbose);
|
||||
}
|
||||
} while (line != "[/enemy]");
|
||||
|
||||
return enemy;
|
||||
}
|
||||
|
||||
// Carga un bloque [item]...[/item] desde un archivo
|
||||
auto Room::loadItemFromFile(std::ifstream& file, const std::string& file_name, bool verbose) -> Item::Data {
|
||||
Item::Data item;
|
||||
item.counter = 0;
|
||||
item.color1 = stringToColor("yellow");
|
||||
item.color2 = stringToColor("magenta");
|
||||
|
||||
std::string line;
|
||||
do {
|
||||
std::getline(file, line);
|
||||
auto [key, value] = parseKeyValue(line);
|
||||
|
||||
if (!setItem(&item, key, value)) {
|
||||
logUnknownParameter(file_name, key, verbose);
|
||||
}
|
||||
} while (line != "[/item]");
|
||||
|
||||
return item;
|
||||
}
|
||||
Reference in New Issue
Block a user