- es pot canviar d'habitacio amb el editor obert

- nous comandos per a navegar per les habitacions (left, right, up i down)
This commit is contained in:
2026-04-02 11:38:46 +02:00
parent eca5a55d3c
commit 0e61b94848
4 changed files with 69 additions and 11 deletions

View File

@@ -201,9 +201,9 @@ categories:
- keyword: ROOM
handler: cmd_room
description: "Change to room number (GAME only)"
usage: "ROOM <1-60>|NEXT|PREV"
usage: "ROOM <1-60>|NEXT|PREV|LEFT|RIGHT|UP|DOWN"
completions:
ROOM: [NEXT, PREV]
ROOM: [NEXT, PREV, LEFT, RIGHT, UP, DOWN]
- keyword: SCENE
handler: cmd_scene

View File

@@ -27,6 +27,7 @@ namespace GameControl {
inline std::function<void()> enter_editor;
inline std::function<void()> exit_editor;
inline std::function<std::string()> revert_editor;
inline std::function<void()> reload_current_room; // Recarga la habitación actual desde disco
inline std::function<void()> reload_current_room; // Recarga la habitación actual desde disco
inline std::function<std::string(const std::string&)> get_adjacent_room; // Obtiene la room adyacente (UP/DOWN/LEFT/RIGHT)
} // namespace GameControl
#endif

View File

@@ -133,6 +133,13 @@ Game::Game(Mode mode)
GameControl::revert_editor = []() -> std::string {
return MapEditor::get()->revert();
};
GameControl::get_adjacent_room = [this](const std::string& direction) -> std::string {
if (direction == "UP") { return room_->getRoom(Room::Border::TOP); }
if (direction == "DOWN") { return room_->getRoom(Room::Border::BOTTOM); }
if (direction == "LEFT") { return room_->getRoom(Room::Border::LEFT); }
if (direction == "RIGHT") { return room_->getRoom(Room::Border::RIGHT); }
return "0";
};
#endif
SceneManager::current = (mode_ == Mode::GAME) ? SceneManager::Scene::GAME : SceneManager::Scene::DEMO;
@@ -158,6 +165,7 @@ Game::~Game() {
GameControl::exit_editor = nullptr;
GameControl::revert_editor = nullptr;
GameControl::reload_current_room = nullptr;
GameControl::get_adjacent_room = nullptr;
#endif
}

View File

@@ -22,7 +22,8 @@
#include "utils/utils.hpp" // Para toUpper, prettyName
#ifdef _DEBUG
#include "core/system/debug.hpp" // Para Debug
#include "core/system/debug.hpp" // Para Debug
#include "game/editor/map_editor.hpp" // Para MapEditor
#endif
// ── Helpers ──────────────────────────────────────────────────────────────────
@@ -560,9 +561,48 @@ static auto cmd_debug(const std::vector<std::string>& args) -> std::string {
}
// ROOM <num>|NEXT|PREV
// Helper: cambia de room teniendo en cuenta el editor (exit → change → re-enter)
static auto changeRoomWithEditor(const std::string& room_file) -> std::string {
if (!GameControl::change_room) { return "Game not initialized"; }
const bool EDITOR_WAS_ACTIVE = MapEditor::get() && MapEditor::get()->isActive();
// Si el editor está activo, salir primero (guarda y recarga la room actual)
if (EDITOR_WAS_ACTIVE && GameControl::exit_editor) {
GameControl::exit_editor();
}
// Cambiar de habitación
if (!GameControl::change_room(room_file)) {
// Si falla, re-entrar al editor en la room original
if (EDITOR_WAS_ACTIVE && GameControl::enter_editor) {
GameControl::enter_editor();
}
return std::string("Room not found: ") + room_file;
}
// Si el editor estaba activo, re-entrar en la nueva room
if (EDITOR_WAS_ACTIVE && GameControl::enter_editor) {
GameControl::enter_editor();
}
return std::string("Room: ") + room_file;
}
static auto cmd_room(const std::vector<std::string>& args) -> std::string {
if (SceneManager::current != SceneManager::Scene::GAME) { return "Only available in GAME scene"; }
if (args.empty()) { return "usage: room <1-60>|next|prev"; }
if (args.empty()) { return "usage: room <1-60>|next|prev|left|right|up|down"; }
// Direcciones: LEFT, RIGHT, UP, DOWN
if (args[0] == "LEFT" || args[0] == "RIGHT" || args[0] == "UP" || args[0] == "DOWN") {
if (!GameControl::get_adjacent_room) { return "Game not initialized"; }
const std::string ADJACENT = GameControl::get_adjacent_room(args[0]);
if (ADJACENT == "0" || ADJACENT.empty()) {
return "No room " + toLower(args[0]);
}
return changeRoomWithEditor(ADJACENT);
}
int num = 0;
if (args[0] == "NEXT" || args[0] == "PREV") {
if (!GameControl::get_current_room) { return "Game not initialized"; }
@@ -574,15 +614,12 @@ static auto cmd_room(const std::vector<std::string>& args) -> std::string {
} else {
try {
num = std::stoi(args[0]);
} catch (...) { return "usage: room <1-60>|next|prev"; }
} catch (...) { return "usage: room <1-60>|next|prev|left|right|up|down"; }
}
if (num < 1 || num > 60) { return "Room must be between 1 and 60"; }
char buf[16];
std::snprintf(buf, sizeof(buf), "%02d.yaml", num);
if (GameControl::change_room && GameControl::change_room(buf)) {
return std::string("Room: ") + buf;
}
return std::string("Room not found: ") + buf;
return changeRoomWithEditor(buf);
}
// ITEMS <0-200>
@@ -632,7 +669,19 @@ static auto cmd_scene(const std::vector<std::string>& args) -> std::string {
// EDIT [ON|OFF|REVERT]
static auto cmd_edit(const std::vector<std::string>& args) -> std::string {
if (args.empty() || args[0] == "ON") {
if (args.empty()) {
// Toggle: si está activo → off, si no → on
if (MapEditor::get() && MapEditor::get()->isActive()) {
if (GameControl::exit_editor) { GameControl::exit_editor(); }
return "Editor OFF";
}
if (GameControl::enter_editor) {
GameControl::enter_editor();
return "Editor ON";
}
return "Not in game";
}
if (args[0] == "ON") {
if (GameControl::enter_editor) {
GameControl::enter_editor();
return "Editor ON";