afegit RoomFormat per a centralitzar la creació i edició de fitxers d'habitacions

This commit is contained in:
2026-04-10 17:58:25 +02:00
parent faae650a53
commit 077b86ea4a
19 changed files with 924 additions and 1090 deletions

View File

@@ -11,6 +11,7 @@
#include <iostream> // Para cout
#include <set> // Para set
#include "external/fkyaml_node.hpp" // Para fkyaml::node (loadSettings)
#include "core/input/mouse.hpp" // Para Mouse
#include "core/rendering/render_info.hpp" // Para RenderInfo
#include "core/rendering/screen.hpp" // Para Screen
@@ -19,7 +20,7 @@
#include "core/resources/resource_list.hpp" // Para Resource::List
#include "core/resources/resource_types.hpp" // Para RoomResource
#include "game/editor/editor_statusbar.hpp" // Para EditorStatusBar
#include "game/editor/room_saver.hpp" // Para RoomSaver
#include "game/gameplay/room_format.hpp" // Para RoomFormat
#include "game/entities/player.hpp" // Para Player
#include "game/game_control.hpp" // Para GameControl
#include "game/gameplay/enemy_manager.hpp" // Para EnemyManager
@@ -191,12 +192,8 @@ void MapEditor::enter(std::shared_ptr<Room> room, std::shared_ptr<Player> player
room_data_ = *room_data_ptr;
}
// Obtener la ruta completa y cargar el YAML original (para edición parcial y backup)
// Obtener la ruta completa al fichero del editor (para autosave en disco)
file_path_ = Resource::List::get()->get(room_path_);
if (!file_path_.empty()) {
yaml_ = RoomSaver::loadYAML(file_path_);
yaml_backup_ = yaml_; // Copia profunda para revert
}
bool is_reenter = reenter_;
if (!reenter_) {
@@ -278,13 +275,12 @@ auto MapEditor::revert() -> std::string {
if (!active_) { return "Editor not active"; }
if (file_path_.empty()) { return "Error: No file path"; }
// Restaurar el YAML original y reescribir el fichero
yaml_ = yaml_backup_;
// Restaurar room_data_ desde el cache (que mantiene la versión original) y persistir
auto room_data_ptr = Resource::Cache::get()->getRoom(room_path_);
if (room_data_ptr) {
room_data_ = *room_data_ptr;
}
RoomSaver::saveYAML(file_path_, yaml_, room_data_);
RoomFormat::saveYAML(file_path_, room_data_);
// Rebuild all entities from room_data_
auto* enemy_mgr = room_->getEnemyManager();
@@ -334,7 +330,7 @@ void MapEditor::autosave() {
// Platforms are already synced via resetToInitialPosition during drag commit
RoomSaver::saveYAML(file_path_, yaml_, room_data_);
RoomFormat::saveYAML(file_path_, room_data_);
}
// Actualiza el editor
@@ -1573,8 +1569,7 @@ auto MapEditor::setRoomProperty(const std::string& property, const std::string&
// Guardar la otra room
std::string other_path = Resource::List::get()->get(*our_field);
if (!other_path.empty()) {
auto other_yaml = RoomSaver::loadYAML(other_path);
RoomSaver::saveYAML(other_path, other_yaml, *old_other);
RoomFormat::saveYAML(other_path, *old_other);
}
}
}
@@ -1610,8 +1605,7 @@ auto MapEditor::setRoomProperty(const std::string& property, const std::string&
}
std::string other_path = Resource::List::get()->get(connection);
if (!other_path.empty()) {
auto other_yaml = RoomSaver::loadYAML(other_path);
RoomSaver::saveYAML(other_path, other_yaml, *other);
RoomFormat::saveYAML(other_path, *other);
}
}
}
@@ -1669,19 +1663,9 @@ auto MapEditor::createNewRoom(const std::string& direction) -> std::string { //
std::string room_dir = ref_path.substr(0, ref_path.find_last_of("\\/") + 1);
std::string new_path = room_dir + new_name;
// Crear Room::Data por defecto con conexión recíproca
Room::Data new_room;
// Construir Room::Data por defecto desde la autoridad del formato
Room::Data new_room = RoomFormat::createDefault();
new_room.number = std::string(name_buf).substr(0, std::string(name_buf).find('.'));
new_room.tile_set_file = "standard.gif";
new_room.item_color1 = 11;
new_room.item_color2 = 12;
new_room.upper_room = "0";
new_room.lower_room = "0";
new_room.left_room = "0";
new_room.right_room = "0";
new_room.conveyor_belt_direction = 0;
new_room.tile_map.resize(Map::WIDTH * Map::HEIGHT, -1);
new_room.collision_tile_map.resize(Map::WIDTH * Map::HEIGHT, 0);
// Conexión recíproca: la nueva room conecta de vuelta a la actual
if (direction == "UP") {
@@ -1694,40 +1678,9 @@ auto MapEditor::createNewRoom(const std::string& direction) -> std::string { //
new_room.left_room = room_path_;
}
// Conexiones del YAML
auto conn_str = [](const std::string& c) -> std::string { return (c == "0") ? "null" : c; };
// Crear el YAML
std::ofstream file(new_path);
if (!file.is_open()) { return "Error: cannot create " + new_path; }
file << "# NO_NAME\n";
file << "room:\n";
file << " name_en: \"NO_NAME\"\n";
file << " name_ca: \"NO_NAME\"\n";
file << " tileSetFile: standard.gif\n";
file << "\n";
file << " connections:\n";
file << " up: " << conn_str(new_room.upper_room) << "\n";
file << " down: " << conn_str(new_room.lower_room) << "\n";
file << " left: " << conn_str(new_room.left_room) << "\n";
file << " right: " << conn_str(new_room.right_room) << "\n";
file << "\n";
file << " itemColor1: bright_cyan\n";
file << " itemColor2: yellow\n";
file << "\n";
file << " conveyorBelt: none\n";
file << "\n";
file << "tilemap:\n";
for (int row = 0; row < Map::HEIGHT; ++row) {
file << " - [";
for (int col = 0; col < Map::WIDTH; ++col) {
file << "-1";
if (col < Map::WIDTH - 1) { file << ", "; }
}
file << "]\n";
}
file.close();
// Persistir vía la autoridad del formato (no más std::ofstream a pelo)
auto save_result = RoomFormat::saveYAML(new_path, new_room);
if (save_result.find("Error") == 0) { return save_result; }
// Registrar en Resource::List (mapa + assets.yaml) y cache
Resource::List::get()->addAsset(new_path, Resource::List::Type::ROOM);
@@ -1803,8 +1756,7 @@ auto MapEditor::deleteRoom() -> std::string { // NOLINT(readability-function-co
// Guardar la otra room
std::string other_path = Resource::List::get()->get(neighbor);
if (!other_path.empty()) {
auto other_yaml = RoomSaver::loadYAML(other_path);
RoomSaver::saveYAML(other_path, other_yaml, *other);
RoomFormat::saveYAML(other_path, *other);
}
};