This commit is contained in:
2025-10-27 18:35:53 +01:00
parent b1dca32a5b
commit 3179a08dac
63 changed files with 686 additions and 693 deletions

View File

@@ -1,7 +1,8 @@
#include "game/entities/enemy.hpp"
#include <SDL3/SDL.h>
#include <stdlib.h> // Para rand
#include <cstdlib> // Para rand
#include "core/rendering/surface_animated_sprite.hpp" // Para SAnimatedSprite
#include "core/resources/resource.hpp" // Para Resource
@@ -87,11 +88,11 @@ void Enemy::checkPath() {
}
// Devuelve el rectangulo que contiene al enemigo
SDL_FRect Enemy::getRect() {
auto Enemy::getRect() -> SDL_FRect {
return sprite_->getRect();
}
// Obtiene el rectangulo de colision del enemigo
SDL_FRect& Enemy::getCollider() {
auto Enemy::getCollider() -> SDL_FRect& {
return collider_;
}

View File

@@ -59,8 +59,8 @@ class Enemy {
void update();
// Devuelve el rectangulo que contiene al enemigo
SDL_FRect getRect();
auto getRect() -> SDL_FRect;
// Obtiene el rectangulo de colision del enemigo
SDL_FRect& getCollider();
auto getCollider() -> SDL_FRect&;
};

View File

@@ -4,7 +4,7 @@
#include "core/resources/resource.hpp" // Para Resource
// Constructor
Item::Item(ItemData item)
Item::Item(const ItemData &item)
: sprite_(std::make_shared<SurfaceSprite>(Resource::get()->getSurface(item.tile_set_file), item.x, item.y, ITEM_SIZE, ITEM_SIZE)),
change_color_speed_(4) {
// Inicia variables
@@ -21,13 +21,13 @@ Item::Item(ItemData item)
}
// Pinta el objeto en pantalla
void Item::render() {
void Item::render() const {
const int INDEX = (counter_ / change_color_speed_) % color_.size();
sprite_->render(1, color_.at(INDEX));
}
// Obtiene su ubicación
SDL_FPoint Item::getPos() {
auto Item::getPos() -> SDL_FPoint {
const SDL_FPoint P = {sprite_->getX(), sprite_->getY()};
return P;
}

View File

@@ -9,21 +9,15 @@ class SurfaceSprite;
struct ItemData {
std::string tile_set_file; // Ruta al fichero con los gráficos del item
float x; // Posición del item en pantalla
float y; // Posición del item en pantalla
int tile; // Número de tile dentro de la textura
int counter; // Contador inicial. Es el que lo hace cambiar de color
Uint8 color1; // Uno de los dos colores que se utiliza para el item
Uint8 color2; // Uno de los dos colores que se utiliza para el item
float x{0}; // Posición del item en pantalla
float y{0}; // Posición del item en pantalla
int tile{0}; // Número de tile dentro de la textura
int counter{0}; // Contador inicial. Es el que lo hace cambiar de color
Uint8 color1{}; // Uno de los dos colores que se utiliza para el item
Uint8 color2{}; // Uno de los dos colores que se utiliza para el item
// Constructor
ItemData()
: x(0),
y(0),
tile(0),
counter(0),
color1(),
color2() {}
ItemData() = default;
};
class Item {
@@ -42,22 +36,22 @@ class Item {
public:
// Constructor
explicit Item(ItemData item);
explicit Item(const ItemData &item);
// Destructor
~Item() = default;
// Pinta el objeto en pantalla
void render();
void render() const;
// Actualiza las variables del objeto
void update() { counter_++; }
// Obtiene el rectangulo de colision del objeto
SDL_FRect& getCollider() { return collider_; }
auto getCollider() -> SDL_FRect& { return collider_; }
// Obtiene su ubicación
SDL_FPoint getPos();
auto getPos() -> SDL_FPoint;
// Asigna los colores del objeto
void setColors(Uint8 col1, Uint8 col2);

View File

@@ -32,10 +32,10 @@ Player::Player(const PlayerData& player)
feet_.resize(feet_.size() + 2, {0, 0});
#ifdef _DEBUG
debug_rect_x_ = {0, 0, 0, 0};
debug_rect_y_ = {0, 0, 0, 0};
debug_rect_x_ = {.x = 0, .y = 0, .w = 0, .h = 0};
debug_rect_y_ = {.x = 0, .y = 0, .w = 0, .h = 0};
debug_color_ = static_cast<Uint8>(PaletteColor::GREEN);
debug_point_ = {0, 0};
debug_point_ = {.x = 0, .y = 0};
#endif
}
@@ -241,7 +241,7 @@ void Player::moveHorizontalLeft() {
// Si ha tocado alguna rampa mientras camina (sin saltar), asciende
if (state_ != PlayerState::JUMPING) {
const LineVertical LEFT_SIDE = {static_cast<int>(x_), static_cast<int>(y_) + static_cast<int>(HEIGHT) - 2, static_cast<int>(y_) + static_cast<int>(HEIGHT) - 1}; // Comprueba solo los dos pixels de abajo
const LineVertical LEFT_SIDE = {.x = static_cast<int>(x_), .y1 = static_cast<int>(y_) + static_cast<int>(HEIGHT) - 2, .y2 = static_cast<int>(y_) + static_cast<int>(HEIGHT) - 1}; // Comprueba solo los dos pixels de abajo
const int LY = room_->checkLeftSlopes(&LEFT_SIDE);
if (LY > -1) {
y_ = LY - HEIGHT;
@@ -261,7 +261,7 @@ void Player::moveHorizontalRight() {
proj.x = x_ + WIDTH;
proj.y = y_;
proj.h = HEIGHT;
proj.w = ceil(vx_); // Para evitar que tenga un ancho de 0 pixels
proj.w = std::ceil(vx_); // Para evitar que tenga un ancho de 0 pixels
#ifdef _DEBUG
debug_rect_x_ = proj;
@@ -281,7 +281,7 @@ void Player::moveHorizontalRight() {
// Si ha tocado alguna rampa mientras camina (sin saltar), asciende
if (state_ != PlayerState::JUMPING) {
const LineVertical RIGHT_SIDE = {static_cast<int>(x_) + static_cast<int>(WIDTH) - 1, static_cast<int>(y_) + static_cast<int>(HEIGHT) - 2, static_cast<int>(y_) + static_cast<int>(HEIGHT) - 1}; // Comprueba solo los dos pixels de abajo
const LineVertical RIGHT_SIDE = {.x = static_cast<int>(x_) + static_cast<int>(WIDTH) - 1, .y1 = static_cast<int>(y_) + static_cast<int>(HEIGHT) - 2, .y2 = static_cast<int>(y_) + static_cast<int>(HEIGHT) - 1}; // Comprueba solo los dos pixels de abajo
const int RY = room_->checkRightSlopes(&RIGHT_SIDE);
if (RY > -1) {
y_ = RY - HEIGHT;
@@ -327,7 +327,7 @@ void Player::moveVerticalDown() {
SDL_FRect proj;
proj.x = x_;
proj.y = y_ + HEIGHT;
proj.h = ceil(vy_); // Para evitar que tenga una altura de 0 pixels
proj.h = std::ceil(vy_); // Para evitar que tenga una altura de 0 pixels
proj.w = WIDTH;
#ifdef _DEBUG
@@ -347,8 +347,8 @@ void Player::moveVerticalDown() {
// Si no hay colisión con los muros, comprueba la colisión con las rampas
if (state_ != PlayerState::JUMPING) { // Las rampas no se miran si se está saltando
auto rect = toSDLRect(proj);
const LineVertical LEFT_SIDE = {rect.x, rect.y, rect.y + rect.h - 1};
const LineVertical RIGHT_SIDE = {rect.x + rect.w - 1, rect.y, rect.y + rect.h - 1};
const LineVertical LEFT_SIDE = {.x = rect.x, .y1 = rect.y, .y2 = rect.y + rect.h - 1};
const LineVertical RIGHT_SIDE = {.x = rect.x + rect.w - 1, .y1 = rect.y, .y2 = rect.y + rect.h - 1};
const float POINT = std::max(room_->checkRightSlopes(&RIGHT_SIDE), room_->checkLeftSlopes(&LEFT_SIDE));
if (POINT > -1) {
// No está saltando y hay colisión con una rampa
@@ -357,7 +357,7 @@ void Player::moveVerticalDown() {
setState(PlayerState::STANDING);
#ifdef _DEBUG
debug_color_ = static_cast<Uint8>(PaletteColor::YELLOW);
debug_point_ = {x_ + (WIDTH / 2), POINT};
debug_point_ = {.x = x_ + (WIDTH / 2), .y = POINT};
#endif
} else {
// No está saltando y no hay colisón con una rampa
@@ -377,7 +377,7 @@ void Player::moveVerticalDown() {
// Recalcula la posición del jugador y su animación
void Player::move() {
last_position_ = {x_, y_}; // Guarda la posicion actual antes de modificarla
last_position_ = {.x = x_, .y = y_}; // Guarda la posicion actual antes de modificarla
applyGravity(); // Aplica gravedad al jugador
checkState(); // Comprueba el estado del jugador
@@ -463,7 +463,7 @@ void Player::playFallSound() {
}
// Comprueba si el jugador tiene suelo debajo de los pies
bool Player::isOnFloor() {
auto Player::isOnFloor() -> bool {
bool on_floor = false;
bool on_slope_l = false;
bool on_slope_r = false;
@@ -498,7 +498,7 @@ bool Player::isOnFloor() {
}
// Comprueba si el jugador esta sobre una superficie automática
bool Player::isOnAutoSurface() {
auto Player::isOnAutoSurface() -> bool {
bool on_auto_surface = false;
updateFeet();
@@ -518,7 +518,7 @@ bool Player::isOnAutoSurface() {
}
// Comprueba si el jugador está sobre una rampa hacia abajo
bool Player::isOnDownSlope() {
auto Player::isOnDownSlope() -> bool {
bool on_slope = false;
updateFeet();
@@ -542,7 +542,7 @@ bool Player::isOnDownSlope() {
}
// Comprueba que el jugador no toque ningun tile de los que matan
bool Player::checkKillingTiles() {
auto Player::checkKillingTiles() -> bool {
// Actualiza los puntos de colisión
updateColliderPoints();
@@ -571,25 +571,25 @@ void Player::setColor() {
// Actualiza los puntos de colisión
void Player::updateColliderPoints() {
const SDL_FRect RECT = getRect();
collider_points_[0] = {RECT.x, RECT.y};
collider_points_[1] = {RECT.x + 7, RECT.y};
collider_points_[2] = {RECT.x + 7, RECT.y + 7};
collider_points_[3] = {RECT.x, RECT.y + 7};
collider_points_[4] = {RECT.x, RECT.y + 8};
collider_points_[5] = {RECT.x + 7, RECT.y + 8};
collider_points_[6] = {RECT.x + 7, RECT.y + 15};
collider_points_[7] = {RECT.x, RECT.y + 15};
collider_points_[0] = {.x = RECT.x, .y = RECT.y};
collider_points_[1] = {.x = RECT.x + 7, .y = RECT.y};
collider_points_[2] = {.x = RECT.x + 7, .y = RECT.y + 7};
collider_points_[3] = {.x = RECT.x, .y = RECT.y + 7};
collider_points_[4] = {.x = RECT.x, .y = RECT.y + 8};
collider_points_[5] = {.x = RECT.x + 7, .y = RECT.y + 8};
collider_points_[6] = {.x = RECT.x + 7, .y = RECT.y + 15};
collider_points_[7] = {.x = RECT.x, .y = RECT.y + 15};
}
// Actualiza los puntos de los pies
void Player::updateFeet() {
const SDL_FPoint P = {x_, y_};
under_feet_[0] = {P.x, P.y + HEIGHT};
under_feet_[1] = {P.x + 7, P.y + HEIGHT};
under_feet_[0] = {.x = P.x, .y = P.y + HEIGHT};
under_feet_[1] = {.x = P.x + 7, .y = P.y + HEIGHT};
feet_[0] = {P.x, P.y + HEIGHT - 1};
feet_[1] = {P.x + 7, P.y + HEIGHT - 1};
feet_[0] = {.x = P.x, .y = P.y + HEIGHT - 1};
feet_[1] = {.x = P.x + 7, .y = P.y + HEIGHT - 1};
}
// Cambia el estado del jugador

View File

@@ -4,6 +4,7 @@
#include <memory> // Para shared_ptr, __shared_ptr_access
#include <string> // Para string
#include <utility>
#include <vector> // Para vector
#include "core/rendering/surface_animated_sprite.hpp" // Para SAnimatedSprite
@@ -57,9 +58,9 @@ struct PlayerData {
// Constructor
PlayerData(PlayerSpawn spawn, std::string texture_path, std::string animations_path, std::shared_ptr<Room> room)
: spawn(spawn),
texture_path(texture_path),
animations_path(animations_path),
room(room) {}
texture_path(std::move(std::move(texture_path))),
animations_path(std::move(std::move(animations_path))),
room(std::move(std::move(room))) {}
};
class Player {
@@ -145,16 +146,16 @@ class Player {
void playFallSound();
// Comprueba si el jugador tiene suelo debajo de los pies
bool isOnFloor();
auto isOnFloor() -> bool;
// Comprueba si el jugador esta sobre una superficie automática
bool isOnAutoSurface();
auto isOnAutoSurface() -> bool;
// Comprueba si el jugador está sobre una rampa hacia abajo
bool isOnDownSlope();
auto isOnDownSlope() -> bool;
// Comprueba que el jugador no toque ningun tile de los que matan
bool checkKillingTiles();
auto checkKillingTiles() -> bool;
// Actualiza los puntos de colisión
void updateColliderPoints();
@@ -196,31 +197,31 @@ public:
void update();
// Indica si el jugador esta en uno de los cuatro bordes de la pantalla
bool getOnBorder() const { return is_on_border_; }
[[nodiscard]] auto getOnBorder() const -> bool { return is_on_border_; }
// Indica en cual de los cuatro bordes se encuentra
RoomBorder getBorder() const { return border_; }
[[nodiscard]] auto getBorder() const -> RoomBorder { return border_; }
// Cambia al jugador de un borde al opuesto. Util para el cambio de pantalla
void switchBorders();
// Obtiene el rectangulo que delimita al jugador
SDL_FRect getRect() { return {x_, y_, WIDTH, HEIGHT}; }
auto getRect() -> SDL_FRect { return {x_, y_, WIDTH, HEIGHT}; }
// Obtiene el rectangulo de colision del jugador
SDL_FRect& getCollider() { return collider_box_; }
auto getCollider() -> SDL_FRect& { return collider_box_; }
// Obtiene el estado de reaparición del jugador
PlayerSpawn getSpawnParams() { return {x_, y_, vx_, vy_, jump_init_pos_, state_, sprite_->getFlip()}; }
auto getSpawnParams() -> PlayerSpawn { return {x_, y_, vx_, vy_, jump_init_pos_, state_, sprite_->getFlip()}; }
// Establece el color del jugador
void setColor();
// Establece la habitación en la que se encuentra el jugador
void setRoom(std::shared_ptr<Room> room) { room_ = room; }
void setRoom(std::shared_ptr<Room> room) { room_ = std::move(room); }
// Comprueba si el jugador esta vivo
bool isAlive() const { return is_alive_; }
[[nodiscard]] auto isAlive() const -> bool { return is_alive_; }
// Pone el jugador en modo pausa
void setPaused(bool value) { is_paused_ = value; }