Compare commits
2 Commits
66d55061dd
...
6082a72392
| Author | SHA1 | Date | |
|---|---|---|---|
| 6082a72392 | |||
| 06815d4f72 |
@@ -55,20 +55,19 @@ AnimatedSprite::AnimatedSprite(std::shared_ptr<Texture> texture, const Animation
|
||||
// Obtiene el índice de la animación a partir del nombre
|
||||
int AnimatedSprite::getIndex(const std::string &name)
|
||||
{
|
||||
auto index = -1;
|
||||
|
||||
for (const auto &a : animations_)
|
||||
auto it = animation_indices_.find(name);
|
||||
if (it != animation_indices_.end())
|
||||
{
|
||||
index++;
|
||||
if (a.name == name)
|
||||
{
|
||||
return index;
|
||||
}
|
||||
// Si se encuentra la animación en el mapa, devuelve su índice
|
||||
return it->second;
|
||||
}
|
||||
|
||||
// Si no se encuentra, muestra una advertencia y devuelve -1
|
||||
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "** Warning: could not find \"%s\" animation", name.c_str());
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
// Calcula el frame correspondiente a la animación
|
||||
void AnimatedSprite::animate()
|
||||
{
|
||||
@@ -257,6 +256,9 @@ void AnimatedSprite::loadFromAnimationsFileBuffer(const AnimationsFileBuffer &so
|
||||
|
||||
// Añade la animación al vector de animaciones
|
||||
animations_.emplace_back(animation);
|
||||
|
||||
// Rellena el mapa con el nombre y el nuevo índice
|
||||
animation_indices_[animation.name] = animations_.size() - 1;
|
||||
}
|
||||
|
||||
// Una vez procesada la línea, aumenta el índice para pasar a la siguiente
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "moving_sprite.h"
|
||||
|
||||
@@ -58,6 +59,9 @@ protected:
|
||||
std::vector<Animation> animations_; // Vector de animaciones disponibles
|
||||
int current_animation_ = 0; // Índice de la animación activa
|
||||
|
||||
// --- Mapa para búsqueda rápida de animaciones por nombre ---
|
||||
std::unordered_map<std::string, int> animation_indices_;
|
||||
|
||||
// --- Métodos internos ---
|
||||
void animate(); // Calcula el frame actual de la animación
|
||||
void loadFromAnimationsFileBuffer(const AnimationsFileBuffer &source); // Carga animaciones desde un buffer
|
||||
|
||||
@@ -372,25 +372,25 @@ void Player::setAnimation()
|
||||
if (firing_state_ == PlayerState::FIRING_NONE)
|
||||
{
|
||||
// No esta disparando
|
||||
player_sprite_->setCurrentAnimation(WALK_ANIMATION);
|
||||
player_sprite_->setCurrentAnimation(WALK_ANIMATION, false);
|
||||
player_sprite_->setFlip(FLIP_WALK);
|
||||
}
|
||||
else if (isRecoiling())
|
||||
{
|
||||
// Retroceso
|
||||
player_sprite_->setCurrentAnimation(WALK_ANIMATION + RECOIL_ANIMATION);
|
||||
player_sprite_->setCurrentAnimation(WALK_ANIMATION + RECOIL_ANIMATION, false);
|
||||
player_sprite_->setFlip(FLIP_RECOIL);
|
||||
}
|
||||
else if (isCooling())
|
||||
{
|
||||
// Acaba de disparar
|
||||
player_sprite_->setCurrentAnimation(WALK_ANIMATION + COOL_ANIMATION);
|
||||
player_sprite_->setCurrentAnimation(WALK_ANIMATION + COOL_ANIMATION, false);
|
||||
player_sprite_->setFlip(FLIP_COOL);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Está disparando
|
||||
player_sprite_->setCurrentAnimation(WALK_ANIMATION + FIRE_ANIMATION);
|
||||
player_sprite_->setCurrentAnimation(WALK_ANIMATION + FIRE_ANIMATION, false);
|
||||
// Si dispara de lado, invierte el sprite segun hacia donde dispara
|
||||
// Si dispara recto, invierte el sprite segun hacia donde camina
|
||||
player_sprite_->setFlip(FIRE_ANIMATION == "-fire-center" ? FLIP_WALK : FLIP_FIRE);
|
||||
@@ -454,36 +454,47 @@ void Player::updateCooldown()
|
||||
}
|
||||
|
||||
--cant_fire_counter_;
|
||||
if (cant_fire_counter_ == 0)
|
||||
{
|
||||
recoiling_state_counter_ = recoiling_state_duration_;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (cooling_state_counter_ > COOLING_COMPLETE_)
|
||||
if (recoiling_state_counter_ > 0)
|
||||
{
|
||||
if (cooling_state_counter_ == COOLING_DURATION_)
|
||||
--recoiling_state_counter_;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (cooling_state_counter_ > COOLING_COMPLETE_)
|
||||
{
|
||||
switch (firing_state_)
|
||||
if (cooling_state_counter_ == COOLING_DURATION_)
|
||||
{
|
||||
case PlayerState::RECOILING_LEFT:
|
||||
setFiringState(PlayerState::COOLING_LEFT);
|
||||
break;
|
||||
case PlayerState::RECOILING_RIGHT:
|
||||
setFiringState(PlayerState::COOLING_RIGHT);
|
||||
break;
|
||||
case PlayerState::RECOILING_UP:
|
||||
setFiringState(PlayerState::COOLING_UP);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
switch (firing_state_)
|
||||
{
|
||||
case PlayerState::RECOILING_LEFT:
|
||||
setFiringState(PlayerState::COOLING_LEFT);
|
||||
break;
|
||||
case PlayerState::RECOILING_RIGHT:
|
||||
setFiringState(PlayerState::COOLING_RIGHT);
|
||||
break;
|
||||
case PlayerState::RECOILING_UP:
|
||||
setFiringState(PlayerState::COOLING_UP);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
--cooling_state_counter_;
|
||||
}
|
||||
|
||||
--cooling_state_counter_;
|
||||
}
|
||||
|
||||
if (cooling_state_counter_ == COOLING_COMPLETE_)
|
||||
{
|
||||
setFiringState(PlayerState::FIRING_NONE);
|
||||
cooling_state_counter_ = -1;
|
||||
if (cooling_state_counter_ == COOLING_COMPLETE_)
|
||||
{
|
||||
setFiringState(PlayerState::FIRING_NONE);
|
||||
cooling_state_counter_ = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -162,7 +162,7 @@ private:
|
||||
static constexpr int WIDTH_ = 30; // Anchura
|
||||
static constexpr int HEIGHT_ = 30; // Altura
|
||||
static constexpr float BASE_SPEED_ = 1.5f; // Velocidad base del jugador
|
||||
static constexpr int COOLING_DURATION_ = 30;
|
||||
static constexpr int COOLING_DURATION_ = 50;
|
||||
static constexpr int COOLING_COMPLETE_ = 0;
|
||||
|
||||
// --- Objetos y punteros ---
|
||||
|
||||
Reference in New Issue
Block a user