granera con sarna no pica
This commit is contained in:
@@ -23,21 +23,17 @@ Enemy::Enemy(const Data& enemy)
|
||||
sprite_->setVelX(enemy.vx);
|
||||
sprite_->setVelY(enemy.vy);
|
||||
|
||||
const int FLIP = (should_flip_ && enemy.vx < 0.0F) ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE;
|
||||
const int MIRROR = should_mirror_ ? SDL_FLIP_VERTICAL : SDL_FLIP_NONE;
|
||||
sprite_->setFlip(static_cast<SDL_FlipMode>(FLIP | MIRROR)); // NOLINT(clang-analyzer-optin.core.EnumCastOutOfRange) SDL flags are designed for bitwise OR
|
||||
applyFlipMirror(enemy.vx);
|
||||
|
||||
collider_ = getRect();
|
||||
|
||||
color_ = enemy.color;
|
||||
|
||||
// Coloca un frame al azar o el designado
|
||||
sprite_->setCurrentAnimationFrame((enemy.frame == -1) ? (rand() % sprite_->getCurrentAnimationSize()) : enemy.frame);
|
||||
}
|
||||
|
||||
// Pinta el enemigo en pantalla
|
||||
void Enemy::render() {
|
||||
sprite_->render(1, color_);
|
||||
sprite_->render();
|
||||
}
|
||||
|
||||
// Actualiza las variables del objeto
|
||||
@@ -60,9 +56,7 @@ void Enemy::resetToInitialPosition(const Data& data) {
|
||||
sprite_->setVelX(data.vx);
|
||||
sprite_->setVelY(data.vy);
|
||||
|
||||
const int FLIP = (should_flip_ && data.vx < 0.0F) ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE;
|
||||
const int MIRROR = should_mirror_ ? SDL_FLIP_VERTICAL : SDL_FLIP_NONE;
|
||||
sprite_->setFlip(static_cast<SDL_FlipMode>(FLIP | MIRROR)); // NOLINT(clang-analyzer-optin.core.EnumCastOutOfRange)
|
||||
applyFlipMirror(data.vx);
|
||||
|
||||
collider_ = getRect();
|
||||
}
|
||||
@@ -105,6 +99,13 @@ void Enemy::checkPath() { // NOLINT(readability-make-member-function-const)
|
||||
}
|
||||
}
|
||||
|
||||
// Aplica flip horizontal y/o mirror vertical al sprite
|
||||
void Enemy::applyFlipMirror(float vx) {
|
||||
const int FLIP = (should_flip_ && vx < 0.0F) ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE;
|
||||
const int MIRROR = should_mirror_ ? SDL_FLIP_VERTICAL : SDL_FLIP_NONE;
|
||||
sprite_->setFlip(static_cast<SDL_FlipMode>(FLIP | MIRROR)); // NOLINT(clang-analyzer-optin.core.EnumCastOutOfRange) SDL flags are designed for bitwise OR
|
||||
}
|
||||
|
||||
// Devuelve el rectangulo que contiene al enemigo
|
||||
auto Enemy::getRect() -> SDL_FRect {
|
||||
return sprite_->getRect();
|
||||
|
||||
@@ -2,9 +2,10 @@
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <memory> // Para shared_ptr
|
||||
#include <string> // Para string
|
||||
class AnimatedSprite; // lines 7-7
|
||||
#include <memory> // Para shared_ptr
|
||||
#include <string> // Para string
|
||||
|
||||
class AnimatedSprite;
|
||||
|
||||
class Enemy {
|
||||
public:
|
||||
@@ -21,7 +22,6 @@ class Enemy {
|
||||
bool flip{false}; // Indica si el enemigo hace flip al terminar su ruta
|
||||
bool mirror{false}; // Indica si el enemigo está volteado verticalmente
|
||||
int frame{0}; // Frame inicial para la animación del enemigo
|
||||
Uint8 color{14}; // Color del enemigo (default: white=14)
|
||||
};
|
||||
|
||||
explicit Enemy(const Data& enemy); // Constructor
|
||||
@@ -38,12 +38,12 @@ class Enemy {
|
||||
auto getCollider() -> SDL_FRect&; // Obtiene el rectangulo de colision del enemigo
|
||||
|
||||
private:
|
||||
void checkPath(); // Comprueba si ha llegado al limite del recorrido para darse media vuelta
|
||||
void applyFlipMirror(float vx); // Aplica flip horizontal y/o mirror vertical al sprite
|
||||
void checkPath(); // Comprueba si ha llegado al limite del recorrido para darse media vuelta
|
||||
|
||||
std::shared_ptr<AnimatedSprite> sprite_; // Sprite del enemigo
|
||||
|
||||
// Variables
|
||||
Uint8 color_{0}; // Color del enemigo
|
||||
int x1_{0}; // Limite izquierdo de la ruta en el eje X
|
||||
int x2_{0}; // Limite derecho de la ruta en el eje X
|
||||
int y1_{0}; // Limite superior de la ruta en el eje Y
|
||||
|
||||
@@ -8,7 +8,7 @@ Item::Item(const Data& item)
|
||||
: sprite_(std::make_shared<Sprite>(Resource::Cache::get()->getSurface(item.tile_set_file), item.x, item.y, ITEM_SIZE, ITEM_SIZE)),
|
||||
time_accumulator_(static_cast<float>(item.counter) * COLOR_CHANGE_INTERVAL) {
|
||||
// Inicia variables
|
||||
sprite_->setClip((item.tile % 10) * ITEM_SIZE, (item.tile / 10) * ITEM_SIZE, ITEM_SIZE, ITEM_SIZE);
|
||||
sprite_->setClip((item.tile % TILESET_COLUMNS) * ITEM_SIZE, (item.tile / TILESET_COLUMNS) * ITEM_SIZE, ITEM_SIZE, ITEM_SIZE);
|
||||
collider_ = sprite_->getRect();
|
||||
|
||||
// Inicializa los colores
|
||||
@@ -52,7 +52,7 @@ void Item::setPosition(float x, float y) {
|
||||
#ifdef _DEBUG
|
||||
// Cambia el tile del item (para editor)
|
||||
void Item::setTile(int tile) {
|
||||
sprite_->setClip((tile % 10) * ITEM_SIZE, (tile / 10) * ITEM_SIZE, ITEM_SIZE, ITEM_SIZE);
|
||||
sprite_->setClip((tile % TILESET_COLUMNS) * ITEM_SIZE, (tile / TILESET_COLUMNS) * ITEM_SIZE, ITEM_SIZE, ITEM_SIZE);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
@@ -36,6 +36,7 @@ class Item {
|
||||
|
||||
private:
|
||||
static constexpr float ITEM_SIZE = 8.0F; // Tamaño del item en pixels
|
||||
static constexpr int TILESET_COLUMNS = 10; // Columnas en la textura del tileset de items
|
||||
static constexpr float COLOR_CHANGE_INTERVAL = 0.06F; // Intervalo de cambio de color en segundos (4 frames a 66.67fps)
|
||||
|
||||
std::shared_ptr<Sprite> sprite_; // SSprite del objeto
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include "core/input/input.hpp" // Para Input, InputAction
|
||||
#include "core/rendering/sprite/animated_sprite.hpp" // Para AnimatedSprite
|
||||
#include "core/resources/resource_cache.hpp" // Para Resource
|
||||
#include "game/defaults.hpp" // Para Defaults::Game::Player, Defaults::Sound::Files
|
||||
#include "game/gameplay/room.hpp" // Para Room
|
||||
#include "game/gameplay/tile_collider.hpp" // Para TileCollider
|
||||
#include "game/options.hpp" // Para Cheat, Options
|
||||
@@ -24,7 +25,6 @@
|
||||
Player::Player(const Data& player)
|
||||
: room_(player.room) {
|
||||
initSprite(player.animations_path);
|
||||
setColor();
|
||||
applySpawnValues(player.spawn_data);
|
||||
placeSprite();
|
||||
initSounds();
|
||||
@@ -36,7 +36,7 @@ Player::Player(const Data& player)
|
||||
// ============================================================================
|
||||
|
||||
void Player::render() {
|
||||
sprite_->render(1, color_);
|
||||
sprite_->render();
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
@@ -560,35 +560,14 @@ void Player::animate(float delta_time) { // NOLINT(readability-make-member-func
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Color y skin
|
||||
// Skin
|
||||
// ============================================================================
|
||||
|
||||
void Player::setColor(Uint8 color) {
|
||||
if (color != 0) {
|
||||
color_ = color;
|
||||
return;
|
||||
}
|
||||
if (Options::game.player_color >= 0) {
|
||||
color_ = static_cast<Uint8>(Options::game.player_color);
|
||||
} else {
|
||||
color_ = 14;
|
||||
}
|
||||
if (room_ != nullptr && color_ == room_->getBGColor()) {
|
||||
color_ = (room_->getBGColor() != 14) ? 14 : 1;
|
||||
}
|
||||
}
|
||||
|
||||
auto Player::skinToAnimationPath(const std::string& skin_name) -> std::string {
|
||||
if (skin_name == "default") { return "player.yaml"; }
|
||||
if (skin_name == Defaults::Game::Player::SKIN) { return Defaults::Game::Player::DEFAULT_ANIMATION; }
|
||||
return skin_name + ".yaml";
|
||||
}
|
||||
|
||||
void Player::setSkin(const std::string& skin_name) {
|
||||
const auto FLIP = sprite_->getFlip();
|
||||
initSprite(skinToAnimationPath(skin_name));
|
||||
sprite_->setFlip(FLIP);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Inicialización
|
||||
// ============================================================================
|
||||
@@ -602,8 +581,8 @@ void Player::initSprite(const std::string& animations_path) { // NOLINT(readabi
|
||||
}
|
||||
|
||||
void Player::initSounds() { // NOLINT(readability-convert-member-functions-to-static)
|
||||
jump_sound_ = Resource::Cache::get()->getSound("jump.wav");
|
||||
land_sound_ = Resource::Cache::get()->getSound("land.wav");
|
||||
jump_sound_ = Resource::Cache::get()->getSound(Defaults::Sound::Files::JUMP);
|
||||
land_sound_ = Resource::Cache::get()->getSound(Defaults::Sound::Files::LAND);
|
||||
}
|
||||
|
||||
void Player::applySpawnValues(const SpawnData& spawn) {
|
||||
|
||||
@@ -67,8 +67,6 @@ class Player {
|
||||
auto getRect() -> SDL_FRect { return {.x = x_, .y = y_, .w = WIDTH, .h = HEIGHT}; }
|
||||
auto getCollider() -> SDL_FRect& { return collider_box_; }
|
||||
auto getSpawnParams() -> SpawnData { return {.x = x_, .y = y_, .vx = vx_, .vy = vy_, .last_grounded_position = last_grounded_position_, .state = state_, .flip = sprite_->getFlip()}; }
|
||||
void setColor(Uint8 color = 0);
|
||||
void setSkin(const std::string& skin_name);
|
||||
static auto skinToAnimationPath(const std::string& skin_name) -> std::string;
|
||||
void setRoom(std::shared_ptr<Room> room) { room_ = std::move(room); }
|
||||
void setAdjacentRoom(std::shared_ptr<Room> room, Room::Border direction);
|
||||
@@ -134,7 +132,6 @@ class Player {
|
||||
int last_grounded_position_ = 0;
|
||||
|
||||
// --- Renderizado y sonido ---
|
||||
Uint8 color_ = 0;
|
||||
JA_Sound_t* jump_sound_ = nullptr;
|
||||
JA_Sound_t* land_sound_ = nullptr;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user