eliminat sistema de colisió antic
This commit is contained in:
@@ -4,48 +4,34 @@
|
||||
#include "core/rendering/sprite/sprite.hpp"
|
||||
#include "core/rendering/surface.hpp"
|
||||
#ifdef _DEBUG
|
||||
#include "core/resources/resource_cache.hpp" // Para Resource::Cache (collision.gif)
|
||||
#include "core/system/debug.hpp"
|
||||
#endif
|
||||
#include "game/gameplay/collision_map.hpp"
|
||||
#include "utils/utils.hpp"
|
||||
|
||||
// Constructor
|
||||
TilemapRenderer::TilemapRenderer(std::vector<int> tile_map, int tile_set_width, std::shared_ptr<Surface> tileset_surface, Uint8 bg_color, int conveyor_belt_direction)
|
||||
: tile_map_(std::move(tile_map)),
|
||||
tile_set_width_(tile_set_width),
|
||||
tileset_surface_(std::move(tileset_surface)),
|
||||
bg_color_(bg_color),
|
||||
conveyor_belt_direction_(conveyor_belt_direction) {
|
||||
// Crear la surface del mapa
|
||||
map_surface_ = std::make_shared<Surface>(PlayArea::WIDTH, PlayArea::HEIGHT);
|
||||
}
|
||||
|
||||
// Inicializa el renderizador
|
||||
void TilemapRenderer::initialize(const CollisionMap* collision_map) {
|
||||
setAnimatedTiles(collision_map);
|
||||
fillMapTexture(collision_map);
|
||||
void TilemapRenderer::initialize(const std::vector<int>& collision_tile_map) {
|
||||
setAnimatedTiles(collision_tile_map);
|
||||
fillMapTexture(collision_tile_map);
|
||||
}
|
||||
|
||||
// Actualiza las animaciones de tiles
|
||||
void TilemapRenderer::update(float delta_time) {
|
||||
if (is_paused_) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Actualiza el acumulador de tiempo
|
||||
if (is_paused_) { return; }
|
||||
time_accumulator_ += delta_time;
|
||||
|
||||
// Actualiza los tiles animados
|
||||
updateAnimatedTiles();
|
||||
}
|
||||
|
||||
// Renderiza el mapa completo en pantalla
|
||||
void TilemapRenderer::render() {
|
||||
// Dibuja la textura con el mapa en pantalla
|
||||
SDL_FRect dest = {.x = 0, .y = 0, .w = PlayArea::WIDTH, .h = PlayArea::HEIGHT};
|
||||
map_surface_->render(nullptr, &dest);
|
||||
|
||||
// Dibuja los tiles animados
|
||||
#ifdef _DEBUG
|
||||
if (!Debug::get()->isEnabled()) {
|
||||
renderAnimatedTiles();
|
||||
@@ -56,52 +42,11 @@ void TilemapRenderer::render() {
|
||||
}
|
||||
|
||||
#ifdef _DEBUG
|
||||
// Renderiza las superficies de colisión en modo debug (función helper estática)
|
||||
static void renderDebugCollisionSurfaces(const CollisionMap* collision_map) {
|
||||
auto surface = Screen::get()->getRendererSurface();
|
||||
|
||||
// BottomSurfaces
|
||||
for (auto l : collision_map->getBottomFloors()) {
|
||||
surface->drawLine(l.x1, l.y, l.x2, l.y, 2);
|
||||
}
|
||||
|
||||
// TopSurfaces
|
||||
for (auto l : collision_map->getTopFloors()) {
|
||||
surface->drawLine(l.x1, l.y, l.x2, l.y, 4);
|
||||
}
|
||||
|
||||
// LeftSurfaces
|
||||
for (auto l : collision_map->getLeftWalls()) {
|
||||
surface->drawLine(l.x, l.y1, l.x, l.y2, 8);
|
||||
}
|
||||
|
||||
// RightSurfaces
|
||||
for (auto l : collision_map->getRightWalls()) {
|
||||
surface->drawLine(l.x, l.y1, l.x, l.y2, 6);
|
||||
}
|
||||
|
||||
// LeftSlopes
|
||||
for (auto l : collision_map->getLeftSlopes()) {
|
||||
surface->drawLine(l.x1, l.y1, l.x2, l.y2, 10);
|
||||
}
|
||||
|
||||
// RightSlopes
|
||||
for (auto l : collision_map->getRightSlopes()) {
|
||||
surface->drawLine(l.x1, l.y1, l.x2, l.y2, 12);
|
||||
}
|
||||
|
||||
// AutoSurfaces (Conveyor Belts)
|
||||
for (auto l : collision_map->getConveyorBeltFloors()) {
|
||||
surface->drawLine(l.x1, l.y, l.x2, l.y, 14);
|
||||
}
|
||||
// Redibuja el tilemap (para actualizar modo debug: pinta collision tilemap o tiles normales)
|
||||
void TilemapRenderer::redrawMap(const std::vector<int>& collision_tile_map) {
|
||||
fillMapTexture(collision_tile_map);
|
||||
}
|
||||
|
||||
// Redibuja el tilemap (para actualizar modo debug)
|
||||
void TilemapRenderer::redrawMap(const CollisionMap* collision_map) {
|
||||
fillMapTexture(collision_map);
|
||||
}
|
||||
|
||||
// Cambia un tile y repinta solo esa celda en la map_surface
|
||||
void TilemapRenderer::setTile(int index, int tile_value) {
|
||||
if (index < 0 || index >= static_cast<int>(tile_map_.size())) { return; }
|
||||
|
||||
@@ -113,11 +58,9 @@ void TilemapRenderer::setTile(int index, int tile_value) {
|
||||
auto previous_renderer = Screen::get()->getRendererSurface();
|
||||
Screen::get()->setRendererSurface(map_surface_);
|
||||
|
||||
// Borrar la celda con el color de fondo
|
||||
SDL_FRect cell = {.x = static_cast<float>(col * TILE_SIZE), .y = static_cast<float>(row * TILE_SIZE), .w = static_cast<float>(TILE_SIZE), .h = static_cast<float>(TILE_SIZE)};
|
||||
map_surface_->fillRect(&cell, bg_color_);
|
||||
|
||||
// Dibujar el nuevo tile (si no es vacío ni animado)
|
||||
if (tile_value > -1) {
|
||||
const bool IS_ANIMATED = (tile_value >= 18 * tile_set_width_) && (tile_value < 19 * tile_set_width_);
|
||||
if (!IS_ANIMATED) {
|
||||
@@ -131,27 +74,44 @@ void TilemapRenderer::setTile(int index, int tile_value) {
|
||||
|
||||
Screen::get()->setRendererSurface(previous_renderer);
|
||||
}
|
||||
|
||||
// Renderiza el collision tilemap superpuesto (modo debug)
|
||||
static void renderDebugCollisionTilemap(const std::vector<int>& collision_tile_map) {
|
||||
auto collision_surface = Resource::Cache::get()->getSurface("collision.gif");
|
||||
if (!collision_surface) { return; }
|
||||
|
||||
for (int y = 0; y < Map::HEIGHT; ++y) {
|
||||
for (int x = 0; x < Map::WIDTH; ++x) {
|
||||
int index = (y * Map::WIDTH) + x;
|
||||
if (index >= static_cast<int>(collision_tile_map.size())) { continue; }
|
||||
int tile = collision_tile_map[index];
|
||||
if (tile <= 0) { continue; }
|
||||
SDL_FRect clip = {
|
||||
.x = static_cast<float>(tile * Tile::SIZE),
|
||||
.y = 0,
|
||||
.w = static_cast<float>(Tile::SIZE),
|
||||
.h = static_cast<float>(Tile::SIZE)};
|
||||
collision_surface->render(x * Tile::SIZE, y * Tile::SIZE, &clip);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
// Pinta el mapa estático y debug lines
|
||||
void TilemapRenderer::fillMapTexture(const CollisionMap* collision_map) { // NOLINT(readability-convert-member-functions-to-static)
|
||||
// Pinta el mapa estático
|
||||
void TilemapRenderer::fillMapTexture(const std::vector<int>& collision_tile_map) {
|
||||
auto previous_renderer = Screen::get()->getRendererSurface();
|
||||
Screen::get()->setRendererSurface(map_surface_);
|
||||
map_surface_->clear(bg_color_);
|
||||
|
||||
// Los tileSetFiles son de 20x20 tiles. El primer tile es el 0. Cuentan hacia la derecha y hacia abajo
|
||||
|
||||
SDL_FRect clip = {.x = 0, .y = 0, .w = TILE_SIZE, .h = TILE_SIZE};
|
||||
for (int y = 0; y < MAP_HEIGHT; ++y) {
|
||||
for (int x = 0; x < MAP_WIDTH; ++x) {
|
||||
// Tiled pone los tiles vacios del mapa como cero y empieza a contar de 1 a n.
|
||||
// Al cargar el mapa en memoria, se resta uno, por tanto los tiles vacios son -1
|
||||
// Tampoco hay que dibujar los tiles animados que estan en la fila 19 (indices)
|
||||
const int INDEX = (y * MAP_WIDTH) + x;
|
||||
const bool A = (tile_map_[INDEX] >= 18 * tile_set_width_) && (tile_map_[INDEX] < 19 * tile_set_width_);
|
||||
const bool B = tile_map_[INDEX] > -1;
|
||||
// Los tiles animados (fila 18 del tileset) no se pintan en la textura estática
|
||||
const bool IS_ANIMATED = (tile_map_[INDEX] >= 18 * tile_set_width_) && (tile_map_[INDEX] < 19 * tile_set_width_);
|
||||
const bool HAS_TILE = tile_map_[INDEX] > -1;
|
||||
|
||||
if (B && !A) {
|
||||
if (HAS_TILE && !IS_ANIMATED) {
|
||||
clip.x = (tile_map_[INDEX] % tile_set_width_) * TILE_SIZE;
|
||||
clip.y = (tile_map_[INDEX] / tile_set_width_) * TILE_SIZE;
|
||||
#ifdef _DEBUG
|
||||
@@ -166,25 +126,21 @@ void TilemapRenderer::fillMapTexture(const CollisionMap* collision_map) { // NO
|
||||
}
|
||||
|
||||
#ifdef _DEBUG
|
||||
// Pinta las superficies en el modo debug
|
||||
// En modo debug, pintar el collision tilemap en vez de las líneas de colisión antiguas
|
||||
if (Debug::get()->isEnabled()) {
|
||||
renderDebugCollisionSurfaces(collision_map);
|
||||
renderDebugCollisionTilemap(collision_tile_map);
|
||||
}
|
||||
#endif // _DEBUG
|
||||
#endif
|
||||
Screen::get()->setRendererSurface(previous_renderer);
|
||||
}
|
||||
|
||||
// Localiza todos los tiles animados
|
||||
void TilemapRenderer::setAnimatedTiles(const CollisionMap* collision_map) { // NOLINT(readability-convert-member-functions-to-static)
|
||||
// Recorre la habitación entera por filas buscando tiles de tipo t_animated
|
||||
for (int i = 0; i < (int)tile_map_.size(); ++i) {
|
||||
const auto TILE_TYPE = collision_map->getTile(i);
|
||||
if (TILE_TYPE == CollisionMap::Tile::ANIMATED) {
|
||||
// La i es la ubicación
|
||||
// Localiza tiles animados (conveyor belts) usando el collision_tile_map
|
||||
void TilemapRenderer::setAnimatedTiles(const std::vector<int>& collision_tile_map) {
|
||||
for (int i = 0; i < static_cast<int>(tile_map_.size()); ++i) {
|
||||
// Un tile es animado si su valor en el collision tilemap es COLLISION_ANIMATED (6)
|
||||
if (i < static_cast<int>(collision_tile_map.size()) && collision_tile_map[i] == COLLISION_ANIMATED) {
|
||||
const int X = (i % MAP_WIDTH) * TILE_SIZE;
|
||||
const int Y = (i / MAP_WIDTH) * TILE_SIZE;
|
||||
|
||||
// TileMap[i] es el tile a poner
|
||||
const int XC = (tile_map_[i] % tile_set_width_) * TILE_SIZE;
|
||||
const int YC = (tile_map_[i] / tile_set_width_) * TILE_SIZE;
|
||||
|
||||
@@ -197,14 +153,10 @@ void TilemapRenderer::setAnimatedTiles(const CollisionMap* collision_map) { //
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza tiles animados
|
||||
void TilemapRenderer::updateAnimatedTiles() { // NOLINT(readability-make-member-function-const)
|
||||
void TilemapRenderer::updateAnimatedTiles() {
|
||||
const int NUM_FRAMES = 4;
|
||||
|
||||
// Calcular frame actual basado en tiempo
|
||||
const int CURRENT_FRAME = static_cast<int>(time_accumulator_ / CONVEYOR_FRAME_DURATION) % NUM_FRAMES;
|
||||
|
||||
// Calcular offset basado en dirección
|
||||
int offset = 0;
|
||||
if (conveyor_belt_direction_ == -1) {
|
||||
offset = CURRENT_FRAME * TILE_SIZE;
|
||||
@@ -219,7 +171,6 @@ void TilemapRenderer::updateAnimatedTiles() { // NOLINT(readability-make-member
|
||||
}
|
||||
}
|
||||
|
||||
// Renderiza tiles animados
|
||||
void TilemapRenderer::renderAnimatedTiles() {
|
||||
for (const auto& a : animated_tiles_) {
|
||||
a.sprite->render();
|
||||
|
||||
Reference in New Issue
Block a user