desacoplament de Player i Options

Player: canviat id de int a enum
migrant input: eliminat Device, keyboard separat de la llista de mandos, llig i guarda configuracions de mandos
falta: definir botons, asignar mandos a jugadors i guardar la asignació
This commit is contained in:
2025-08-03 22:49:28 +02:00
parent de9fb5aa4b
commit 90c080f3e3
19 changed files with 433 additions and 353 deletions

View File

@@ -16,20 +16,22 @@
#include "texture.h" // Para Texture
// Constructor
Player::Player(int id, float x, int y, bool demo, SDL_FRect &play_area, std::vector<std::shared_ptr<Texture>> texture, const std::vector<std::vector<std::string>> &animations)
: player_sprite_(std::make_unique<AnimatedSprite>(texture.at(0), animations.at(0))),
power_sprite_(std::make_unique<AnimatedSprite>(texture.at(1), animations.at(1))),
Player::Player(const Config& config)
: player_sprite_(std::make_unique<AnimatedSprite>(config.texture.at(0), config.animations.at(0))),
power_sprite_(std::make_unique<AnimatedSprite>(config.texture.at(1), config.animations.at(1))),
enter_name_(std::make_unique<EnterName>()),
id_(id),
play_area_(play_area),
default_pos_x_(x),
default_pos_y_(y),
demo_(demo) {
id_(config.id),
play_area_(*config.play_area),
default_pos_x_(config.x),
default_pos_y_(config.y),
hi_score_table_(*config.hi_score_table),
glowing_entry_(*config.glowing_entry),
demo_(config.demo) {
// Configura objetos
player_sprite_->getTexture()->setPalette(coffees_);
power_sprite_->getTexture()->setAlpha(224);
power_up_x_offset_ = (power_sprite_->getWidth() - player_sprite_->getWidth()) / 2;
power_sprite_->setPosY(y - (power_sprite_->getHeight() - player_sprite_->getHeight()));
power_sprite_->setPosY(default_pos_y_ - (power_sprite_->getHeight() - player_sprite_->getHeight()));
// Inicializa variables
pos_x_ = default_pos_x_;
@@ -219,8 +221,8 @@ void Player::handleRollingGroundCollision() {
}
void Player::handleRollingStop() {
const auto NEXT_PLAYER_STATUS = isEligibleForHighScore() ? State::ENTERING_NAME : State::CONTINUE;
const auto NEXT_STATE = demo_ ? State::LYING_ON_THE_FLOOR_FOREVER : NEXT_PLAYER_STATUS;
const auto NEXT_PLAYER_STATE = qualifiesForHighScore() ? State::ENTERING_NAME : State::CONTINUE;
const auto NEXT_STATE = demo_ ? State::LYING_ON_THE_FLOOR_FOREVER : NEXT_PLAYER_STATE;
setPlayingState(NEXT_STATE);
pos_x_ = player_sprite_->getPosX();
@@ -278,10 +280,15 @@ void Player::handleLeavingScreen() {
void Player::handleEnteringScreen() {
updateStepCounter();
if (id_ == 1) {
handlePlayer1Entering();
} else if (id_ == 2) {
handlePlayer2Entering();
switch (id_) {
case Id::PLAYER1:
handlePlayer1Entering();
break;
case Id::PLAYER2:
handlePlayer2Entering();
break;
default:
break;
}
shiftSprite();
@@ -350,10 +357,10 @@ void Player::updateWalkingStateForCredits() {
void Player::setInputBasedOnPlayerId() {
switch (id_) {
case 1:
case Id::PLAYER1:
setInputPlaying(Input::Action::LEFT);
break;
case 2:
case Id::PLAYER2:
setInputPlaying(Input::Action::RIGHT);
break;
default:
@@ -552,9 +559,10 @@ void Player::update() {
}
// Incrementa la puntuación del jugador
void Player::addScore(int score) {
void Player::addScore(int score, int last_hi_score_entry) {
if (isPlaying()) {
score_ += score;
qualifies_for_high_score_ = score_ > last_hi_score_entry;
}
}
@@ -611,10 +619,10 @@ void Player::setPlayingState(State state) {
}
case State::WAITING: {
switch (id_) {
case 1:
case Id::PLAYER1:
pos_x_ = param.game.game_area.rect.x;
break;
case 2:
case Id::PLAYER2:
pos_x_ = param.game.game_area.rect.w - WIDTH;
break;
default:
@@ -698,11 +706,11 @@ void Player::setPlayingState(State state) {
step_counter_ = 0;
setScoreboardMode(Scoreboard::Mode::SCORE);
switch (id_) {
case 1:
case Id::PLAYER1:
pos_x_ = param.game.game_area.rect.x - WIDTH;
break;
case 2:
case Id::PLAYER2:
pos_x_ = param.game.game_area.rect.x + param.game.game_area.rect.w;
break;
@@ -898,7 +906,7 @@ auto Player::isRenderable() const -> bool {
// Añade una puntuación a la tabla de records
void Player::addScoreToScoreBoard() const {
const auto ENTRY = HiScoreEntry(trim(getLastEnterName()), getScore(), get1CC());
auto manager = std::make_unique<ManageHiScoreTable>(Options::settings.hi_score_table);
Options::settings.last_hi_score_entry.at(getId() - 1) = manager->add(ENTRY);
auto manager = std::make_unique<ManageHiScoreTable>(hi_score_table_);
glowing_entry_ = manager->add(ENTRY);
manager->saveToFile(Asset::get()->get("score.bin"));
}