148 lines
4.3 KiB
C++
148 lines
4.3 KiB
C++
#ifdef _DEBUG
|
|
|
|
#include "game/editor/map_editor.hpp"
|
|
|
|
#include <SDL3/SDL.h>
|
|
|
|
#include <iostream> // Para cout
|
|
|
|
#include "core/input/mouse.hpp" // Para Mouse
|
|
#include "core/rendering/screen.hpp" // Para Screen
|
|
#include "game/editor/editor_statusbar.hpp" // Para EditorStatusBar
|
|
#include "game/entities/player.hpp" // Para Player
|
|
#include "game/gameplay/room.hpp" // Para Room
|
|
#include "game/options.hpp" // Para Options
|
|
#include "utils/defines.hpp" // Para Tile::SIZE, PlayArea
|
|
|
|
// 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);
|
|
|
|
// Guardar estado de invencibilidad y forzarla
|
|
invincible_before_editor_ = Options::cheats.invincible;
|
|
Options::cheats.invincible = Options::Cheat::State::ENABLED;
|
|
player_->setColor();
|
|
|
|
// Crear la barra de estado
|
|
statusbar_ = std::make_unique<EditorStatusBar>(room_->getNumber(), room_->getName());
|
|
|
|
// Pausar enemigos e items
|
|
room_->setPaused(true);
|
|
|
|
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();
|
|
|
|
// Despausar enemigos e items
|
|
room_->setPaused(false);
|
|
|
|
// Liberar recursos
|
|
statusbar_.reset();
|
|
room_.reset();
|
|
player_.reset();
|
|
scoreboard_data_.reset();
|
|
|
|
std::cout << "MapEditor: OFF\n";
|
|
}
|
|
|
|
// Actualiza el editor
|
|
void MapEditor::update([[maybe_unused]] float delta_time) {
|
|
// Mantener el ratón siempre visible
|
|
SDL_ShowCursor();
|
|
Mouse::last_mouse_move_time = SDL_GetTicks();
|
|
|
|
// Actualizar posición del ratón
|
|
updateMousePosition();
|
|
|
|
// Actualizar la barra de estado con las coordenadas del ratón
|
|
if (statusbar_) {
|
|
statusbar_->setMouseTile(mouse_tile_x_, mouse_tile_y_);
|
|
statusbar_->update(delta_time);
|
|
}
|
|
}
|
|
|
|
// Renderiza el editor
|
|
void MapEditor::render() {
|
|
// El tilemap ya ha sido renderizado por Game::renderPlaying() antes de llamar aquí
|
|
// Renderizar entidades (por ahora: enemigos, items, jugador normales)
|
|
room_->renderEnemies();
|
|
room_->renderItems();
|
|
player_->render();
|
|
|
|
// Renderizar barra de estado del editor (reemplaza al scoreboard)
|
|
if (statusbar_) {
|
|
statusbar_->render();
|
|
}
|
|
}
|
|
|
|
// Maneja eventos del editor
|
|
void MapEditor::handleEvent([[maybe_unused]] const SDL_Event& event) {
|
|
// Por ahora no procesamos eventos específicos del editor
|
|
// En fases posteriores: drag & drop
|
|
}
|
|
|
|
// 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
|