Files
jaildoctors_dilemma/source/game/editor/room_saver.cpp

119 lines
4.0 KiB
C++

#ifdef _DEBUG
#include "game/editor/room_saver.hpp"
#include <cmath> // Para std::round
#include <fstream> // Para ofstream
#include <iostream> // Para cout, cerr
#include "core/resources/resource_helper.hpp" // Para Resource::Helper
#include "utils/defines.hpp" // Para Tile::SIZE
// Carga el YAML original desde disco
auto RoomSaver::loadYAML(const std::string& file_path) -> fkyaml::node {
auto file_data = Resource::Helper::loadFile(file_path);
if (file_data.empty()) {
std::cerr << "RoomSaver: Cannot load " << file_path << "\n";
return {};
}
std::string content(file_data.begin(), file_data.end());
return fkyaml::node::deserialize(content);
}
// Guarda el nodo YAML completo a disco
auto RoomSaver::saveYAML(const std::string& file_path, const fkyaml::node& yaml) -> std::string {
std::string content = fkyaml::node::serialize(yaml);
std::ofstream file(file_path);
if (!file.is_open()) {
std::cerr << "RoomSaver: Cannot write to " << file_path << "\n";
return "Error: Cannot write to " + file_path;
}
file << content;
file.close();
const std::string FILE_NAME = file_path.substr(file_path.find_last_of("\\/") + 1);
std::cout << "RoomSaver: Saved " << FILE_NAME << "\n";
return "Saved " + FILE_NAME;
}
// Actualiza la posición inicial de un enemigo (pixels → tiles)
void RoomSaver::updateEnemyPosition(fkyaml::node& yaml, int index, float x, float y) {
if (!yaml.contains("enemies")) { return; }
auto& enemies = yaml["enemies"];
if (index < 0 || index >= static_cast<int>(enemies.size())) { return; }
auto& enemy = enemies[index];
int tile_x = static_cast<int>(std::round(x / Tile::SIZE));
int tile_y = static_cast<int>(std::round(y / Tile::SIZE));
if (!enemy.contains("position")) {
enemy["position"] = fkyaml::node::mapping();
}
enemy["position"]["x"] = tile_x;
enemy["position"]["y"] = tile_y;
}
// Actualiza boundary1 de un enemigo (pixels → tiles)
void RoomSaver::updateEnemyBound1(fkyaml::node& yaml, int index, int x, int y) {
if (!yaml.contains("enemies")) { return; }
auto& enemies = yaml["enemies"];
if (index < 0 || index >= static_cast<int>(enemies.size())) { return; }
auto& enemy = enemies[index];
int tile_x = x / Tile::SIZE;
int tile_y = y / Tile::SIZE;
if (!enemy.contains("boundaries")) {
enemy["boundaries"] = fkyaml::node::mapping();
}
auto& bounds = enemy["boundaries"];
if (!bounds.contains("position1")) {
bounds["position1"] = fkyaml::node::mapping();
}
bounds["position1"]["x"] = tile_x;
bounds["position1"]["y"] = tile_y;
}
// Actualiza boundary2 de un enemigo (pixels → tiles)
void RoomSaver::updateEnemyBound2(fkyaml::node& yaml, int index, int x, int y) {
if (!yaml.contains("enemies")) { return; }
auto& enemies = yaml["enemies"];
if (index < 0 || index >= static_cast<int>(enemies.size())) { return; }
auto& enemy = enemies[index];
int tile_x = x / Tile::SIZE;
int tile_y = y / Tile::SIZE;
if (!enemy.contains("boundaries")) {
enemy["boundaries"] = fkyaml::node::mapping();
}
auto& bounds = enemy["boundaries"];
if (!bounds.contains("position2")) {
bounds["position2"] = fkyaml::node::mapping();
}
bounds["position2"]["x"] = tile_x;
bounds["position2"]["y"] = tile_y;
}
// Actualiza la posición de un item (pixels → tiles)
void RoomSaver::updateItemPosition(fkyaml::node& yaml, int index, float x, float y) {
if (!yaml.contains("items")) { return; }
auto& items = yaml["items"];
if (index < 0 || index >= static_cast<int>(items.size())) { return; }
auto& item = items[index];
int tile_x = static_cast<int>(std::round(x / Tile::SIZE));
int tile_y = static_cast<int>(std::round(y / Tile::SIZE));
if (!item.contains("position")) {
item["position"] = fkyaml::node::mapping();
}
item["position"]["x"] = tile_x;
item["position"]["y"] = tile_y;
}
#endif // _DEBUG