guardat dels canvis en la habitacio
This commit is contained in:
@@ -11,7 +11,9 @@
|
||||
#include "core/rendering/screen.hpp" // Para Screen
|
||||
#include "core/rendering/surface.hpp" // Para Surface
|
||||
#include "core/resources/resource_cache.hpp" // Para Resource::Cache
|
||||
#include "core/resources/resource_list.hpp" // Para Resource::List
|
||||
#include "game/editor/editor_statusbar.hpp" // Para EditorStatusBar
|
||||
#include "game/editor/room_saver.hpp" // Para RoomSaver
|
||||
#include "game/entities/player.hpp" // Para Player
|
||||
#include "game/gameplay/enemy_manager.hpp" // Para EnemyManager
|
||||
#include "game/gameplay/item_manager.hpp" // Para ItemManager
|
||||
@@ -51,12 +53,19 @@ void MapEditor::enter(std::shared_ptr<Room> room, std::shared_ptr<Player> player
|
||||
room_path_ = room_path;
|
||||
scoreboard_data_ = std::move(scoreboard_data);
|
||||
|
||||
// Cargar una copia de los datos de la habitación (para boundaries y edición futura)
|
||||
// Cargar una copia de los datos de la habitación (para boundaries y edición)
|
||||
auto room_data_ptr = Resource::Cache::get()->getRoom(room_path);
|
||||
if (room_data_ptr) {
|
||||
room_data_ = *room_data_ptr;
|
||||
}
|
||||
|
||||
// Obtener la ruta completa y cargar el YAML original (para edición parcial y backup)
|
||||
file_path_ = Resource::List::get()->get(room_path_);
|
||||
if (!file_path_.empty()) {
|
||||
yaml_ = RoomSaver::loadYAML(file_path_);
|
||||
yaml_backup_ = yaml_; // Copia profunda para revert
|
||||
}
|
||||
|
||||
// Guardar estado de invencibilidad y forzarla
|
||||
invincible_before_editor_ = Options::cheats.invincible;
|
||||
Options::cheats.invincible = Options::Cheat::State::ENABLED;
|
||||
@@ -95,6 +104,39 @@ void MapEditor::exit() {
|
||||
std::cout << "MapEditor: OFF\n";
|
||||
}
|
||||
|
||||
// Revierte todos los cambios al estado original
|
||||
auto MapEditor::revert() -> std::string {
|
||||
if (!active_) { return "Editor not active"; }
|
||||
if (file_path_.empty()) { return "Error: No file path"; }
|
||||
|
||||
// Restaurar el YAML al backup original
|
||||
yaml_ = yaml_backup_;
|
||||
RoomSaver::saveYAML(file_path_, yaml_);
|
||||
|
||||
// Recargar room_data_ desde el backup
|
||||
auto room_data_ptr = Resource::Cache::get()->getRoom(room_path_);
|
||||
if (room_data_ptr) {
|
||||
room_data_ = *room_data_ptr;
|
||||
}
|
||||
|
||||
// Resetear los sprites vivos a las posiciones originales
|
||||
room_->resetEnemyPositions(room_data_.enemies);
|
||||
|
||||
// Resetear items (recargar posiciones)
|
||||
auto* item_mgr = room_->getItemManager();
|
||||
for (int i = 0; i < item_mgr->getCount() && i < static_cast<int>(room_data_.items.size()); ++i) {
|
||||
item_mgr->getItem(i)->setPosition(room_data_.items[i].x, room_data_.items[i].y);
|
||||
}
|
||||
|
||||
return "Reverted to original";
|
||||
}
|
||||
|
||||
// Auto-guarda los cambios puntuales al YAML tras soltar una entidad
|
||||
void MapEditor::autosave() {
|
||||
if (file_path_.empty()) { return; }
|
||||
RoomSaver::saveYAML(file_path_, yaml_);
|
||||
}
|
||||
|
||||
// Actualiza el editor
|
||||
void MapEditor::update(float delta_time) {
|
||||
// Mantener el ratón siempre visible
|
||||
@@ -242,18 +284,22 @@ void MapEditor::handleMouseUp() {
|
||||
const int SNAP_X = static_cast<int>(drag_.snap_x);
|
||||
const int SNAP_Y = static_cast<int>(drag_.snap_y);
|
||||
|
||||
bool changed = false;
|
||||
|
||||
switch (drag_.target) {
|
||||
case DragTarget::PLAYER:
|
||||
player_->setDebugPosition(drag_.snap_x, drag_.snap_y);
|
||||
player_->finalizeDebugTeleport();
|
||||
// El jugador no se guarda en el YAML de la habitación (es dato de spawn global)
|
||||
break;
|
||||
|
||||
case DragTarget::ENEMY_INITIAL:
|
||||
// Actualizar datos mutables y posición del sprite vivo
|
||||
if (IDX >= 0 && IDX < static_cast<int>(room_data_.enemies.size())) {
|
||||
room_data_.enemies[IDX].x = drag_.snap_x;
|
||||
room_data_.enemies[IDX].y = drag_.snap_y;
|
||||
room_->getEnemyManager()->getEnemy(IDX)->resetToInitialPosition(room_data_.enemies[IDX]);
|
||||
RoomSaver::updateEnemyPosition(yaml_, IDX, drag_.snap_x, drag_.snap_y);
|
||||
changed = true;
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -261,6 +307,8 @@ void MapEditor::handleMouseUp() {
|
||||
if (IDX >= 0 && IDX < static_cast<int>(room_data_.enemies.size())) {
|
||||
room_data_.enemies[IDX].x1 = SNAP_X;
|
||||
room_data_.enemies[IDX].y1 = SNAP_Y;
|
||||
RoomSaver::updateEnemyBound1(yaml_, IDX, SNAP_X, SNAP_Y);
|
||||
changed = true;
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -268,12 +316,16 @@ void MapEditor::handleMouseUp() {
|
||||
if (IDX >= 0 && IDX < static_cast<int>(room_data_.enemies.size())) {
|
||||
room_data_.enemies[IDX].x2 = SNAP_X;
|
||||
room_data_.enemies[IDX].y2 = SNAP_Y;
|
||||
RoomSaver::updateEnemyBound2(yaml_, IDX, SNAP_X, SNAP_Y);
|
||||
changed = true;
|
||||
}
|
||||
break;
|
||||
|
||||
case DragTarget::ITEM:
|
||||
if (IDX >= 0 && IDX < room_->getItemManager()->getCount()) {
|
||||
room_->getItemManager()->getItem(IDX)->setPosition(drag_.snap_x, drag_.snap_y);
|
||||
RoomSaver::updateItemPosition(yaml_, IDX, drag_.snap_x, drag_.snap_y);
|
||||
changed = true;
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -281,6 +333,11 @@ void MapEditor::handleMouseUp() {
|
||||
break;
|
||||
}
|
||||
|
||||
// Auto-guardar si hubo cambio
|
||||
if (changed) {
|
||||
autosave();
|
||||
}
|
||||
|
||||
// Resetear estado de drag
|
||||
drag_ = {};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user