afegida Player::computeAnimation() i soluciona algunes animacions incorrectes que havia
This commit is contained in:
@@ -15,6 +15,9 @@
|
|||||||
#include "scoreboard.h" // Para Scoreboard
|
#include "scoreboard.h" // Para Scoreboard
|
||||||
#include "stage.h" // Para power_can_be_added
|
#include "stage.h" // Para power_can_be_added
|
||||||
#include "texture.h" // Para Texture
|
#include "texture.h" // Para Texture
|
||||||
|
#ifdef _DEBUG
|
||||||
|
#include <iostream>
|
||||||
|
#endif
|
||||||
|
|
||||||
// Constructor
|
// Constructor
|
||||||
Player::Player(const Config &config)
|
Player::Player(const Config &config)
|
||||||
@@ -389,6 +392,60 @@ void Player::render() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Calcula la animacion de moverse y disparar del jugador
|
||||||
|
auto Player::computeAnimation() const -> std::pair<std::string, SDL_FlipMode> {
|
||||||
|
const std::string baseAnim = (walking_state_ == State::WALKING_STOP) ? "stand" : "walk";
|
||||||
|
std::string animName;
|
||||||
|
SDL_FlipMode flipMode = SDL_FLIP_NONE;
|
||||||
|
|
||||||
|
switch (firing_state_) {
|
||||||
|
case State::FIRING_NONE:
|
||||||
|
animName = baseAnim;
|
||||||
|
flipMode = (walking_state_ == State::WALKING_RIGHT) ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case State::FIRING_UP:
|
||||||
|
animName = baseAnim + "-fire-center";
|
||||||
|
flipMode = (walking_state_ == State::WALKING_RIGHT) ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case State::FIRING_LEFT:
|
||||||
|
case State::FIRING_RIGHT:
|
||||||
|
animName = baseAnim + "-fire-side";
|
||||||
|
flipMode = (firing_state_ == State::FIRING_RIGHT) ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case State::RECOILING_UP:
|
||||||
|
animName = baseAnim + "-recoil-center";
|
||||||
|
flipMode = (walking_state_ == State::WALKING_RIGHT) ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case State::RECOILING_LEFT:
|
||||||
|
case State::RECOILING_RIGHT:
|
||||||
|
animName = baseAnim + "-recoil-side";
|
||||||
|
flipMode = (firing_state_ == State::RECOILING_RIGHT) ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case State::COOLING_UP:
|
||||||
|
animName = baseAnim + "-cool-center";
|
||||||
|
flipMode = (walking_state_ == State::WALKING_RIGHT) ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case State::COOLING_LEFT:
|
||||||
|
case State::COOLING_RIGHT:
|
||||||
|
animName = baseAnim + "-cool-side";
|
||||||
|
flipMode = (firing_state_ == State::COOLING_RIGHT) ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE;
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
animName = baseAnim;
|
||||||
|
flipMode = (walking_state_ == State::WALKING_RIGHT) ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {animName, flipMode};
|
||||||
|
}
|
||||||
|
|
||||||
// Establece la animación correspondiente al estado
|
// Establece la animación correspondiente al estado
|
||||||
void Player::setAnimation() {
|
void Player::setAnimation() {
|
||||||
switch (playing_state_) {
|
switch (playing_state_) {
|
||||||
@@ -398,64 +455,36 @@ void Player::setAnimation() {
|
|||||||
case State::LEAVING_SCREEN:
|
case State::LEAVING_SCREEN:
|
||||||
case State::TITLE_ANIMATION:
|
case State::TITLE_ANIMATION:
|
||||||
case State::CREDITS: {
|
case State::CREDITS: {
|
||||||
// Crea cadenas de texto para componer el nombre de la animación
|
auto [animName, flipMode] = computeAnimation();
|
||||||
const std::string WALK_ANIMATION = walking_state_ == State::WALKING_STOP ? "stand" : "walk";
|
player_sprite_->setCurrentAnimation(animName, false);
|
||||||
const std::string FIRE_ANIMATION = firing_state_ == State::FIRING_UP ? "-fire-center" : "-fire-side";
|
player_sprite_->setFlip(flipMode);
|
||||||
const std::string RECOIL_ANIMATION = firing_state_ == State::RECOILING_UP ? "-recoil-center" : "-recoil-side";
|
|
||||||
const std::string COOL_ANIMATION = firing_state_ == State::COOLING_UP ? "-cool-center" : "-cool-side";
|
|
||||||
|
|
||||||
const SDL_FlipMode FLIP_WALK = walking_state_ == State::WALKING_RIGHT ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE;
|
|
||||||
const SDL_FlipMode FLIP_FIRE = firing_state_ == State::FIRING_RIGHT ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE;
|
|
||||||
const SDL_FlipMode FLIP_RECOIL = firing_state_ == State::RECOILING_RIGHT ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE;
|
|
||||||
const SDL_FlipMode FLIP_COOL = firing_state_ == State::COOLING_RIGHT ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE;
|
|
||||||
|
|
||||||
// Establece la animación a partir de las cadenas
|
|
||||||
if (firing_state_ == State::FIRING_NONE) {
|
|
||||||
// No esta disparando
|
|
||||||
player_sprite_->setCurrentAnimation(WALK_ANIMATION, false);
|
|
||||||
player_sprite_->setFlip(FLIP_WALK);
|
|
||||||
} else if (isRecoiling()) {
|
|
||||||
// Retroceso
|
|
||||||
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, false);
|
|
||||||
player_sprite_->setFlip(FLIP_COOL);
|
|
||||||
} else {
|
|
||||||
// Está disparando
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case State::WAITING: {
|
|
||||||
|
case State::WAITING:
|
||||||
player_sprite_->setCurrentAnimation("hello");
|
player_sprite_->setCurrentAnimation("hello");
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
case State::ROLLING:
|
case State::ROLLING:
|
||||||
case State::CONTINUE_TIME_OUT: {
|
case State::CONTINUE_TIME_OUT:
|
||||||
player_sprite_->setCurrentAnimation("rolling");
|
player_sprite_->setCurrentAnimation("rolling");
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
case State::LYING_ON_THE_FLOOR_FOREVER:
|
case State::LYING_ON_THE_FLOOR_FOREVER:
|
||||||
case State::ENTERING_NAME:
|
case State::ENTERING_NAME:
|
||||||
case State::CONTINUE: {
|
case State::CONTINUE:
|
||||||
player_sprite_->setCurrentAnimation("dead");
|
player_sprite_->setCurrentAnimation("dead");
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
case State::CELEBRATING: {
|
case State::CELEBRATING:
|
||||||
player_sprite_->setCurrentAnimation("celebration");
|
player_sprite_->setCurrentAnimation("celebration");
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Actualiza las animaciones de los sprites
|
player_sprite_->update();
|
||||||
player_sprite_->update(); // Hace avanzar las animaciones y mueve el cadaver del jugador
|
|
||||||
power_sprite_->update();
|
power_sprite_->update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -297,4 +297,5 @@ class Player {
|
|||||||
void updateWalkingStateForCredits(); // Actualiza el estado de caminata de algún personaje u elemento animado en los créditos
|
void updateWalkingStateForCredits(); // Actualiza el estado de caminata de algún personaje u elemento animado en los créditos
|
||||||
void setInputBasedOnPlayerId(); // Asocia las entradas de control en función del identificador del jugador (teclas, mando, etc.)
|
void setInputBasedOnPlayerId(); // Asocia las entradas de control en función del identificador del jugador (teclas, mando, etc.)
|
||||||
void updateStepCounter(); // Incrementa o ajusta el contador de pasos para animaciones o mecánicas relacionadas con movimiento
|
void updateStepCounter(); // Incrementa o ajusta el contador de pasos para animaciones o mecánicas relacionadas con movimiento
|
||||||
|
auto computeAnimation() const -> std::pair<std::string, SDL_FlipMode>; // Calcula la animacion de moverse y disparar del jugador
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user