al eixir del editor, recarrega la habitació nova

This commit is contained in:
2026-04-02 11:21:08 +02:00
parent a6fae7b001
commit 20bac58814
8 changed files with 76 additions and 15 deletions

View File

@@ -4,14 +4,14 @@ enemies:
boundaries: boundaries:
position1: position1:
x: 5 x: 5
y: 10 y: 13
position2: position2:
x: 14 x: 16
y: 10 y: 13
color: yellow color: yellow
position: position:
x: 12 x: 11
y: 10 y: 13
velocity: velocity:
x: 24.0 x: 24.0
y: 0 y: 0

View File

@@ -4,6 +4,7 @@
#include <algorithm> // Para find_if #include <algorithm> // Para find_if
#include <cstdlib> // Para exit, size_t #include <cstdlib> // Para exit, size_t
#include <fstream> // Para ifstream, istreambuf_iterator
#include <iostream> // Para basic_ostream, operator<<, endl, cout #include <iostream> // Para basic_ostream, operator<<, endl, cout
#include <stdexcept> // Para runtime_error #include <stdexcept> // Para runtime_error
#include <utility> #include <utility>
@@ -15,6 +16,7 @@
#include "core/resources/resource_list.hpp" // Para List, List::Type #include "core/resources/resource_list.hpp" // Para List, List::Type
#include "game/defaults.hpp" // Para Defaults namespace #include "game/defaults.hpp" // Para Defaults namespace
#include "game/gameplay/room.hpp" // Para RoomData, loadRoomFile, loadRoomTileFile #include "game/gameplay/room.hpp" // Para RoomData, loadRoomFile, loadRoomTileFile
#include "game/gameplay/room_loader.hpp" // Para RoomLoader::loadFromString
#include "game/options.hpp" // Para Options, OptionsGame, options #include "game/options.hpp" // Para Options, OptionsGame, options
#include "utils/defines.hpp" // Para WINDOW_CAPTION #include "utils/defines.hpp" // Para WINDOW_CAPTION
#include "utils/utils.hpp" // Para getFileName, printWithDots, PaletteColor #include "utils/utils.hpp" // Para getFileName, printWithDots, PaletteColor
@@ -173,6 +175,34 @@ namespace Resource {
throw std::runtime_error("Habitación no encontrada: " + name); throw std::runtime_error("Habitación no encontrada: " + name);
} }
#ifdef _DEBUG
// Recarga una habitación desde disco (para el editor de mapas)
// Lee directamente del filesystem (no del resource pack) para obtener los cambios del editor
void Cache::reloadRoom(const std::string& name) {
auto file_path = List::get()->get(name);
if (file_path.empty()) {
std::cerr << "reloadRoom: Cannot resolve path for " << name << '\n';
return;
}
// Leer directamente del filesystem (evita el resource pack que tiene datos antiguos)
std::ifstream file(file_path);
if (!file.is_open()) {
std::cerr << "reloadRoom: Cannot open " << file_path << '\n';
return;
}
std::string content((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
file.close();
// Parsear y actualizar el cache
auto it = std::ranges::find_if(rooms_, [&name](const auto& r) -> bool { return r.name == name; });
if (it != rooms_.end()) {
*(it->room) = RoomLoader::loadFromString(content, name);
std::cout << "reloadRoom: " << name << " reloaded from filesystem\n";
}
}
#endif
// Obtiene todas las habitaciones // Obtiene todas las habitaciones
auto Cache::getRooms() -> std::vector<RoomResource>& { auto Cache::getRooms() -> std::vector<RoomResource>& {
return rooms_; return rooms_;

View File

@@ -26,6 +26,9 @@ namespace Resource {
auto getRooms() -> std::vector<RoomResource>&; auto getRooms() -> std::vector<RoomResource>&;
void reload(); // Recarga todos los recursos void reload(); // Recarga todos los recursos
#ifdef _DEBUG
void reloadRoom(const std::string& name); // Recarga una habitación desde disco
#endif
private: private:
// Estructura para llevar la cuenta de los recursos cargados // Estructura para llevar la cuenta de los recursos cargados

View File

@@ -3,21 +3,21 @@
#include "game/editor/room_saver.hpp" #include "game/editor/room_saver.hpp"
#include <cmath> // Para std::round #include <cmath> // Para std::round
#include <fstream> // Para ofstream #include <fstream> // Para ifstream, ofstream, istreambuf_iterator
#include <iostream> // Para cout, cerr #include <iostream> // Para cout, cerr
#include "core/resources/resource_helper.hpp" // Para Resource::Helper
#include "utils/defines.hpp" // Para Tile::SIZE #include "utils/defines.hpp" // Para Tile::SIZE
// Carga el YAML original desde disco // Carga el YAML original directamente del filesystem (no del resource pack)
auto RoomSaver::loadYAML(const std::string& file_path) -> fkyaml::node { auto RoomSaver::loadYAML(const std::string& file_path) -> fkyaml::node {
auto file_data = Resource::Helper::loadFile(file_path); std::ifstream file(file_path);
if (file_data.empty()) { if (!file.is_open()) {
std::cerr << "RoomSaver: Cannot load " << file_path << "\n"; std::cerr << "RoomSaver: Cannot open " << file_path << "\n";
return {}; return {};
} }
std::string content(file_data.begin(), file_data.end()); std::string content((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
file.close();
return fkyaml::node::deserialize(content); return fkyaml::node::deserialize(content);
} }

View File

@@ -27,5 +27,6 @@ namespace GameControl {
inline std::function<void()> enter_editor; inline std::function<void()> enter_editor;
inline std::function<void()> exit_editor; inline std::function<void()> exit_editor;
inline std::function<std::string()> revert_editor; inline std::function<std::string()> revert_editor;
inline std::function<void()> reload_current_room; // Recarga la habitación actual desde disco
} // namespace GameControl } // namespace GameControl
#endif #endif

View File

@@ -316,6 +316,25 @@ void RoomLoader::parseItems(const fkyaml::node& yaml, Room::Data& room, bool ver
} }
} }
#ifdef _DEBUG
// Carga una habitación desde un string YAML (para el editor de mapas, evita el resource pack)
auto RoomLoader::loadFromString(const std::string& yaml_content, const std::string& file_name) -> Room::Data {
Room::Data room;
try {
auto yaml = fkyaml::node::deserialize(yaml_content);
parseRoomConfig(yaml, room, file_name);
parseTilemap(yaml, room, file_name, false);
parseEnemies(yaml, room, false);
parseItems(yaml, room, false);
} catch (const fkyaml::exception& e) {
std::cerr << "YAML parsing error in " << file_name << ": " << e.what() << '\n';
} catch (const std::exception& e) {
std::cerr << "Error loading room " << file_name << ": " << e.what() << '\n';
}
return room;
}
#endif
// Carga un archivo de room en formato YAML // Carga un archivo de room en formato YAML
auto RoomLoader::loadYAML(const std::string& file_path, bool verbose) -> Room::Data { // NOLINT(readability-convert-member-functions-to-static) auto RoomLoader::loadYAML(const std::string& file_path, bool verbose) -> Room::Data { // NOLINT(readability-convert-member-functions-to-static)
Room::Data room; Room::Data room;

View File

@@ -46,6 +46,9 @@ class RoomLoader {
* - items: lista de items (opcional) * - items: lista de items (opcional)
*/ */
static auto loadYAML(const std::string& file_path, bool verbose = false) -> Room::Data; static auto loadYAML(const std::string& file_path, bool verbose = false) -> Room::Data;
#ifdef _DEBUG
static auto loadFromString(const std::string& yaml_content, const std::string& file_name) -> Room::Data;
#endif
private: private:
/** /**

View File

@@ -123,8 +123,12 @@ Game::Game(Mode mode)
GameControl::enter_editor = [this]() -> void { GameControl::enter_editor = [this]() -> void {
MapEditor::get()->enter(room_, player_, current_room_, scoreboard_data_); MapEditor::get()->enter(room_, player_, current_room_, scoreboard_data_);
}; };
GameControl::exit_editor = []() -> void { GameControl::exit_editor = [this]() -> void {
MapEditor::get()->exit(); MapEditor::get()->exit();
// Recargar la habitación desde disco (con los cambios del editor)
Resource::Cache::get()->reloadRoom(current_room_);
changeRoom(current_room_);
player_->setRoom(room_);
}; };
GameControl::revert_editor = []() -> std::string { GameControl::revert_editor = []() -> std::string {
return MapEditor::get()->revert(); return MapEditor::get()->revert();
@@ -153,6 +157,7 @@ Game::~Game() {
GameControl::enter_editor = nullptr; GameControl::enter_editor = nullptr;
GameControl::exit_editor = nullptr; GameControl::exit_editor = nullptr;
GameControl::revert_editor = nullptr; GameControl::revert_editor = nullptr;
GameControl::reload_current_room = nullptr;
#endif #endif
} }