524 lines
18 KiB
C++
524 lines
18 KiB
C++
#ifdef _DEBUG
|
|
|
|
#include "game/editor/map_editor.hpp"
|
|
|
|
#include <SDL3/SDL.h>
|
|
|
|
#include <cmath> // Para std::round
|
|
#include <iostream> // Para cout
|
|
|
|
#include "core/input/mouse.hpp" // Para Mouse
|
|
#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
|
|
#include "game/gameplay/room.hpp" // Para Room
|
|
#include "game/options.hpp" // Para Options
|
|
#include "utils/defines.hpp" // Para Tile::SIZE, PlayArea
|
|
#include "utils/utils.hpp" // Para stringToColor
|
|
|
|
// Singleton
|
|
MapEditor* MapEditor::instance_ = nullptr;
|
|
|
|
void MapEditor::init() {
|
|
instance_ = new MapEditor();
|
|
}
|
|
|
|
void MapEditor::destroy() {
|
|
delete instance_;
|
|
instance_ = nullptr;
|
|
}
|
|
|
|
auto MapEditor::get() -> MapEditor* {
|
|
return instance_;
|
|
}
|
|
|
|
// Constructor
|
|
MapEditor::MapEditor() = default;
|
|
|
|
// Destructor
|
|
MapEditor::~MapEditor() = default;
|
|
|
|
// Entra en modo editor
|
|
void MapEditor::enter(std::shared_ptr<Room> room, std::shared_ptr<Player> player, const std::string& room_path, std::shared_ptr<Scoreboard::Data> scoreboard_data) {
|
|
if (active_) { return; }
|
|
|
|
room_ = std::move(room);
|
|
player_ = std::move(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)
|
|
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;
|
|
player_->setColor();
|
|
|
|
// Resetear enemigos a su posición inicial (pueden haberse movido durante el gameplay)
|
|
room_->resetEnemyPositions(room_data_.enemies);
|
|
|
|
// Crear la barra de estado
|
|
statusbar_ = std::make_unique<EditorStatusBar>(room_->getNumber(), room_->getName());
|
|
|
|
// Resetear estado de drag
|
|
drag_ = {};
|
|
|
|
active_ = true;
|
|
std::cout << "MapEditor: ON (room " << room_path_ << ")\n";
|
|
}
|
|
|
|
// Sale del modo editor
|
|
void MapEditor::exit() {
|
|
if (!active_) { return; }
|
|
|
|
active_ = false;
|
|
|
|
// Restaurar invencibilidad
|
|
Options::cheats.invincible = invincible_before_editor_;
|
|
player_->setColor();
|
|
|
|
// Liberar recursos
|
|
drag_ = {};
|
|
statusbar_.reset();
|
|
room_.reset();
|
|
player_.reset();
|
|
scoreboard_data_.reset();
|
|
|
|
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
|
|
SDL_ShowCursor();
|
|
Mouse::last_mouse_move_time = SDL_GetTicks();
|
|
|
|
// Actualizar animaciones de enemigos e items (sin mover enemigos)
|
|
room_->updateEditorMode(delta_time);
|
|
|
|
// Actualizar posición del ratón
|
|
updateMousePosition();
|
|
|
|
// Si estamos arrastrando, actualizar la posición snapped
|
|
if (drag_.target != DragTarget::NONE) {
|
|
updateDrag();
|
|
}
|
|
|
|
// Actualizar la barra de estado con las coordenadas del ratón y la selección
|
|
if (statusbar_) {
|
|
statusbar_->setMouseTile(mouse_tile_x_, mouse_tile_y_);
|
|
|
|
// Construir info de selección
|
|
std::string sel_info;
|
|
if (drag_.target != DragTarget::NONE) {
|
|
switch (drag_.target) {
|
|
case DragTarget::PLAYER:
|
|
sel_info = "PLAYER";
|
|
break;
|
|
case DragTarget::ENEMY_INITIAL:
|
|
sel_info = "ENEMY " + std::to_string(drag_.index);
|
|
break;
|
|
case DragTarget::ENEMY_BOUND1:
|
|
sel_info = "E" + std::to_string(drag_.index) + " BOUND1";
|
|
break;
|
|
case DragTarget::ENEMY_BOUND2:
|
|
sel_info = "E" + std::to_string(drag_.index) + " BOUND2";
|
|
break;
|
|
case DragTarget::ITEM:
|
|
sel_info = "ITEM " + std::to_string(drag_.index);
|
|
break;
|
|
case DragTarget::NONE:
|
|
break;
|
|
}
|
|
}
|
|
statusbar_->setSelectionInfo(sel_info);
|
|
statusbar_->update(delta_time);
|
|
}
|
|
}
|
|
|
|
// Renderiza el editor
|
|
void MapEditor::render() {
|
|
// El tilemap ya ha sido renderizado por Game::renderPlaying() antes de llamar aquí
|
|
|
|
// Renderizar los marcadores de boundaries y líneas de ruta (debajo de los sprites)
|
|
renderEnemyBoundaries();
|
|
|
|
// Renderizar entidades normales: enemigos (animados en posición inicial), items, jugador
|
|
room_->renderEnemies();
|
|
room_->renderItems();
|
|
player_->render();
|
|
|
|
// Renderizar highlight de selección (encima de los sprites)
|
|
renderSelectionHighlight();
|
|
|
|
// Renderizar barra de estado del editor (reemplaza al scoreboard)
|
|
if (statusbar_) {
|
|
statusbar_->render();
|
|
}
|
|
}
|
|
|
|
// Maneja eventos del editor
|
|
void MapEditor::handleEvent(const SDL_Event& event) {
|
|
if (event.type == SDL_EVENT_MOUSE_BUTTON_DOWN && event.button.button == SDL_BUTTON_LEFT) {
|
|
handleMouseDown(mouse_game_x_, mouse_game_y_);
|
|
} else if (event.type == SDL_EVENT_MOUSE_BUTTON_UP && event.button.button == SDL_BUTTON_LEFT) {
|
|
handleMouseUp();
|
|
}
|
|
}
|
|
|
|
// Procesa click del ratón: hit test + inicio de drag
|
|
void MapEditor::handleMouseDown(float game_x, float game_y) {
|
|
// Prioridad de hit test: jugador → enemigos (initial) → enemigos (boundaries) → items
|
|
|
|
// Helper para iniciar drag
|
|
auto startDrag = [&](DragTarget target, int index, float entity_x, float entity_y) {
|
|
drag_.target = target;
|
|
drag_.index = index;
|
|
drag_.offset_x = game_x - entity_x;
|
|
drag_.offset_y = game_y - entity_y;
|
|
drag_.snap_x = entity_x;
|
|
drag_.snap_y = entity_y;
|
|
};
|
|
|
|
// 1. Hit test sobre el jugador (8x16)
|
|
SDL_FRect player_rect = player_->getRect();
|
|
if (pointInRect(game_x, game_y, player_rect)) {
|
|
startDrag(DragTarget::PLAYER, -1, player_rect.x, player_rect.y);
|
|
return;
|
|
}
|
|
|
|
// 2. Hit test sobre enemigos: posición inicial (usan el rect del sprite vivo)
|
|
auto* enemy_mgr = room_->getEnemyManager();
|
|
for (int i = 0; i < enemy_mgr->getCount(); ++i) {
|
|
SDL_FRect enemy_rect = enemy_mgr->getEnemy(i)->getRect();
|
|
if (pointInRect(game_x, game_y, enemy_rect)) {
|
|
startDrag(DragTarget::ENEMY_INITIAL, i, enemy_rect.x, enemy_rect.y);
|
|
return;
|
|
}
|
|
}
|
|
|
|
// 3. Hit test sobre boundaries de enemigos (rectángulos 8x8 en las posiciones de room_data_)
|
|
for (int i = 0; i < static_cast<int>(room_data_.enemies.size()); ++i) {
|
|
const auto& ed = room_data_.enemies[i];
|
|
constexpr float SZ = static_cast<float>(Tile::SIZE);
|
|
|
|
SDL_FRect b1_rect = {.x = static_cast<float>(ed.x1), .y = static_cast<float>(ed.y1), .w = SZ, .h = SZ};
|
|
if (pointInRect(game_x, game_y, b1_rect)) {
|
|
startDrag(DragTarget::ENEMY_BOUND1, i, b1_rect.x, b1_rect.y);
|
|
return;
|
|
}
|
|
|
|
SDL_FRect b2_rect = {.x = static_cast<float>(ed.x2), .y = static_cast<float>(ed.y2), .w = SZ, .h = SZ};
|
|
if (pointInRect(game_x, game_y, b2_rect)) {
|
|
startDrag(DragTarget::ENEMY_BOUND2, i, b2_rect.x, b2_rect.y);
|
|
return;
|
|
}
|
|
}
|
|
|
|
// 4. Hit test sobre items (8x8)
|
|
auto* item_mgr = room_->getItemManager();
|
|
for (int i = 0; i < item_mgr->getCount(); ++i) {
|
|
SDL_FRect item_rect = item_mgr->getItem(i)->getCollider();
|
|
if (pointInRect(game_x, game_y, item_rect)) {
|
|
startDrag(DragTarget::ITEM, i, item_rect.x, item_rect.y);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Procesa soltar el ratón: commit del drag
|
|
void MapEditor::handleMouseUp() {
|
|
if (drag_.target == DragTarget::NONE) { return; }
|
|
|
|
const int IDX = drag_.index;
|
|
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:
|
|
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;
|
|
|
|
case DragTarget::ENEMY_BOUND1:
|
|
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;
|
|
|
|
case DragTarget::ENEMY_BOUND2:
|
|
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;
|
|
|
|
case DragTarget::NONE:
|
|
break;
|
|
}
|
|
|
|
// Auto-guardar si hubo cambio
|
|
if (changed) {
|
|
autosave();
|
|
}
|
|
|
|
// Resetear estado de drag
|
|
drag_ = {};
|
|
}
|
|
|
|
// Actualiza la posición snapped durante el drag
|
|
void MapEditor::updateDrag() {
|
|
float raw_x = mouse_game_x_ - drag_.offset_x;
|
|
float raw_y = mouse_game_y_ - drag_.offset_y;
|
|
|
|
drag_.snap_x = snapToGrid(raw_x);
|
|
drag_.snap_y = snapToGrid(raw_y);
|
|
|
|
// Mientras arrastramos, mover la entidad visualmente a la posición snapped
|
|
switch (drag_.target) {
|
|
case DragTarget::PLAYER:
|
|
player_->setDebugPosition(drag_.snap_x, drag_.snap_y);
|
|
break;
|
|
|
|
case DragTarget::ENEMY_INITIAL:
|
|
if (drag_.index >= 0 && drag_.index < room_->getEnemyManager()->getCount()) {
|
|
// Mover el sprite vivo del enemigo durante el arrastre
|
|
auto& enemy = room_->getEnemyManager()->getEnemy(drag_.index);
|
|
Enemy::Data temp_data = room_data_.enemies[drag_.index];
|
|
temp_data.x = drag_.snap_x;
|
|
temp_data.y = drag_.snap_y;
|
|
enemy->resetToInitialPosition(temp_data);
|
|
}
|
|
break;
|
|
|
|
case DragTarget::ENEMY_BOUND1:
|
|
// Los boundaries se actualizan visualmente en renderEnemyBoundaries() via drag_.snap
|
|
break;
|
|
|
|
case DragTarget::ENEMY_BOUND2:
|
|
break;
|
|
|
|
case DragTarget::ITEM:
|
|
if (drag_.index >= 0 && drag_.index < room_->getItemManager()->getCount()) {
|
|
room_->getItemManager()->getItem(drag_.index)->setPosition(drag_.snap_x, drag_.snap_y);
|
|
}
|
|
break;
|
|
|
|
case DragTarget::NONE:
|
|
break;
|
|
}
|
|
}
|
|
|
|
// Dibuja highlight del elemento seleccionado/arrastrado
|
|
void MapEditor::renderSelectionHighlight() {
|
|
if (drag_.target == DragTarget::NONE) { return; }
|
|
|
|
auto game_surface = Screen::get()->getRendererSurface();
|
|
if (!game_surface) { return; }
|
|
|
|
const Uint8 HIGHLIGHT_COLOR = stringToColor("bright_white");
|
|
constexpr float SZ = static_cast<float>(Tile::SIZE);
|
|
|
|
SDL_FRect highlight_rect{};
|
|
switch (drag_.target) {
|
|
case DragTarget::PLAYER:
|
|
highlight_rect = player_->getRect();
|
|
break;
|
|
|
|
case DragTarget::ENEMY_INITIAL:
|
|
if (drag_.index >= 0 && drag_.index < room_->getEnemyManager()->getCount()) {
|
|
highlight_rect = room_->getEnemyManager()->getEnemy(drag_.index)->getRect();
|
|
}
|
|
break;
|
|
|
|
case DragTarget::ENEMY_BOUND1:
|
|
case DragTarget::ENEMY_BOUND2:
|
|
highlight_rect = {.x = drag_.snap_x, .y = drag_.snap_y, .w = SZ, .h = SZ};
|
|
break;
|
|
|
|
case DragTarget::ITEM:
|
|
if (drag_.index >= 0 && drag_.index < room_->getItemManager()->getCount()) {
|
|
highlight_rect = room_->getItemManager()->getItem(drag_.index)->getCollider();
|
|
}
|
|
break;
|
|
|
|
case DragTarget::NONE:
|
|
return;
|
|
}
|
|
|
|
// Dibujar rectángulo de highlight alrededor de la entidad
|
|
SDL_FRect border = {
|
|
.x = highlight_rect.x - 1,
|
|
.y = highlight_rect.y - 1,
|
|
.w = highlight_rect.w + 2,
|
|
.h = highlight_rect.h + 2};
|
|
game_surface->drawRectBorder(&border, HIGHLIGHT_COLOR);
|
|
}
|
|
|
|
// Alinea un valor a la cuadrícula de 8x8
|
|
auto MapEditor::snapToGrid(float value) -> float {
|
|
return std::round(value / static_cast<float>(Tile::SIZE)) * static_cast<float>(Tile::SIZE);
|
|
}
|
|
|
|
// Hit test: punto dentro de rectángulo
|
|
auto MapEditor::pointInRect(float px, float py, const SDL_FRect& rect) -> bool {
|
|
return px >= rect.x && px < rect.x + rect.w && py >= rect.y && py < rect.y + rect.h;
|
|
}
|
|
|
|
// Dibuja marcadores de boundaries y líneas de ruta para los enemigos
|
|
void MapEditor::renderEnemyBoundaries() {
|
|
auto game_surface = Screen::get()->getRendererSurface();
|
|
if (!game_surface) { return; }
|
|
|
|
const Uint8 COLOR_BOUND1 = stringToColor("bright_cyan");
|
|
const Uint8 COLOR_BOUND2 = stringToColor("bright_yellow");
|
|
const Uint8 COLOR_ROUTE = stringToColor("bright_white");
|
|
|
|
for (int i = 0; i < static_cast<int>(room_data_.enemies.size()); ++i) {
|
|
const auto& enemy = room_data_.enemies[i];
|
|
constexpr float HALF = Tile::SIZE / 2.0F;
|
|
|
|
// Posiciones base (pueden estar siendo arrastradas)
|
|
float init_x = enemy.x;
|
|
float init_y = enemy.y;
|
|
float b1_x = static_cast<float>(enemy.x1);
|
|
float b1_y = static_cast<float>(enemy.y1);
|
|
float b2_x = static_cast<float>(enemy.x2);
|
|
float b2_y = static_cast<float>(enemy.y2);
|
|
|
|
// Si estamos arrastrando una boundary de este enemigo, usar la posición snapped
|
|
if (drag_.index == i) {
|
|
if (drag_.target == DragTarget::ENEMY_BOUND1) {
|
|
b1_x = drag_.snap_x;
|
|
b1_y = drag_.snap_y;
|
|
} else if (drag_.target == DragTarget::ENEMY_BOUND2) {
|
|
b2_x = drag_.snap_x;
|
|
b2_y = drag_.snap_y;
|
|
} else if (drag_.target == DragTarget::ENEMY_INITIAL) {
|
|
init_x = drag_.snap_x;
|
|
init_y = drag_.snap_y;
|
|
}
|
|
}
|
|
|
|
// Dibujar líneas de ruta
|
|
game_surface->drawLine(b1_x + HALF, b1_y + HALF, init_x + HALF, init_y + HALF, COLOR_ROUTE);
|
|
game_surface->drawLine(init_x + HALF, init_y + HALF, b2_x + HALF, b2_y + HALF, COLOR_ROUTE);
|
|
|
|
// Marcadores en las boundaries
|
|
renderBoundaryMarker(b1_x, b1_y, COLOR_BOUND1);
|
|
renderBoundaryMarker(b2_x, b2_y, COLOR_BOUND2);
|
|
}
|
|
}
|
|
|
|
// Dibuja un marcador de boundary (rectángulo pequeño) en una posición
|
|
void MapEditor::renderBoundaryMarker(float x, float y, Uint8 color) {
|
|
auto game_surface = Screen::get()->getRendererSurface();
|
|
if (!game_surface) { return; }
|
|
|
|
SDL_FRect marker = {.x = x, .y = y, .w = static_cast<float>(Tile::SIZE), .h = static_cast<float>(Tile::SIZE)};
|
|
game_surface->drawRectBorder(&marker, color);
|
|
}
|
|
|
|
// Convierte coordenadas de ventana a coordenadas de juego y tile
|
|
void MapEditor::updateMousePosition() {
|
|
float mouse_x = 0.0F;
|
|
float mouse_y = 0.0F;
|
|
SDL_GetMouseState(&mouse_x, &mouse_y);
|
|
|
|
float render_x = 0.0F;
|
|
float render_y = 0.0F;
|
|
SDL_RenderCoordinatesFromWindow(Screen::get()->getRenderer(), mouse_x, mouse_y, &render_x, &render_y);
|
|
|
|
SDL_FRect dst_rect = Screen::get()->getGameSurfaceDstRect();
|
|
mouse_game_x_ = render_x - dst_rect.x;
|
|
mouse_game_y_ = render_y - dst_rect.y;
|
|
|
|
// Convertir a coordenadas de tile (clampeadas al área de juego)
|
|
mouse_tile_x_ = static_cast<int>(mouse_game_x_) / Tile::SIZE;
|
|
mouse_tile_y_ = static_cast<int>(mouse_game_y_) / Tile::SIZE;
|
|
|
|
// Clampear a los límites del mapa
|
|
if (mouse_tile_x_ < 0) { mouse_tile_x_ = 0; }
|
|
if (mouse_tile_x_ >= PlayArea::WIDTH / Tile::SIZE) { mouse_tile_x_ = PlayArea::WIDTH / Tile::SIZE - 1; }
|
|
if (mouse_tile_y_ < 0) { mouse_tile_y_ = 0; }
|
|
if (mouse_tile_y_ >= PlayArea::HEIGHT / Tile::SIZE) { mouse_tile_y_ = PlayArea::HEIGHT / Tile::SIZE - 1; }
|
|
}
|
|
|
|
#endif // _DEBUG
|