6262b5814d
No canviava al marcador, ni el nom del que tenia la maxima puntuació en calent ni al posar nom retallat el nom de 8 a 6 caracters, i tots en majuscula pa que capia en el marcador ja actualitza be la cadena amb el nom al posar nom per segona vegada en la mateixa partida
234 lines
9.6 KiB
C++
234 lines
9.6 KiB
C++
#pragma once
|
|
|
|
#include "animated_sprite.h" // Para AnimatedSprite
|
|
#include "enter_name.h" // Para EnterName
|
|
#include "utils.h" // Para Circle
|
|
#include <SDL2/SDL_rect.h> // Para SDL_Rect
|
|
#include <SDL2/SDL_stdinc.h> // Para Uint32
|
|
#include <memory> // Para unique_ptr, shared_ptr
|
|
#include <string> // Para string
|
|
#include <vector> // Para vector
|
|
#include "options.h"
|
|
|
|
class Texture; // lines 12-12
|
|
enum class InputType : int; // lines 13-13
|
|
enum class ScoreboardMode; // lines 14-14
|
|
|
|
// Estados del jugador
|
|
enum class PlayerState
|
|
{
|
|
WALKING_LEFT,
|
|
WALKING_RIGHT,
|
|
WALKING_STOP,
|
|
|
|
FIRING_UP,
|
|
FIRING_LEFT,
|
|
FIRING_RIGHT,
|
|
FIRING_NONE,
|
|
|
|
COOLING_UP,
|
|
COOLING_LEFT,
|
|
COOLING_RIGHT,
|
|
|
|
PLAYING, // Está jugando
|
|
CONTINUE, // Está con la cuenta atras para continuar
|
|
WAITING, // No está jugando pero puede entrar a jugar
|
|
ENTERING_NAME, // Introduciendo nombre
|
|
DYING, // El cadaver está volando por ahi
|
|
DIED, // El cadaver ha desaparecido por el fondo
|
|
GAME_OVER, // No está jugando y no puede entrar a jugar
|
|
CELEBRATING, // Poniendo pose de victoria
|
|
ENTERING_NAME_GAME_COMPLETED, // Poniendo nombre en el tramo final del juego
|
|
GAME_COMPLETED, // Moviendose fuera de la pantalla
|
|
};
|
|
|
|
// Clase Player
|
|
class Player
|
|
{
|
|
private:
|
|
// Constantes
|
|
static constexpr int POWERUP_COUNTER_ = 1500; // Duración del estado PowerUp
|
|
static constexpr int INVULNERABLE_COUNTER_ = 200; // Duración del estado invulnerable
|
|
static constexpr int WIDTH_ = 30; // Anchura
|
|
static constexpr int HEIGHT_ = 30; // Altura
|
|
static constexpr float BASE_SPEED_ = 1.5f; // Velocidad base del jugador
|
|
|
|
// Objetos y punteros
|
|
std::unique_ptr<AnimatedSprite> player_sprite_; // Sprite para dibujar el jugador
|
|
std::unique_ptr<AnimatedSprite> power_sprite_; // Sprite para dibujar el aura del jugador con el poder a tope
|
|
std::unique_ptr<EnterName> enter_name_; // Clase utilizada para introducir el nombre
|
|
|
|
// Variables
|
|
int id_; // Numero de identificación para el jugador. Player1 = 1, Player2 = 2
|
|
SDL_Rect play_area_; // Rectangulo con la zona de juego
|
|
float pos_x_ = 0.0f; // Posicion en el eje X
|
|
int pos_y_ = 0; // Posicion en el eje Y
|
|
float default_pos_x_; // Posición inicial para el jugador
|
|
int default_pos_y_; // Posición inicial para el jugador
|
|
float vel_x_ = 0.0f; // Cantidad de pixeles a desplazarse en el eje X
|
|
int vel_y_ = 0.0f; // Cantidad de pixeles a desplazarse en el eje Y
|
|
int cooldown_ = 0; // Contador durante el cual no puede disparar
|
|
int cooling_status_counter_ = 0; // Contador para la animación del estado cooling
|
|
int score_ = 0; // Puntos del jugador
|
|
float score_multiplier_ = 1.0f; // Multiplicador de puntos
|
|
PlayerState walking_state_ = PlayerState::WALKING_STOP; // Estado del jugador al moverse
|
|
PlayerState firing_state_ = PlayerState::FIRING_NONE; // Estado del jugador al disparar
|
|
PlayerState playing_state_ = PlayerState::WAITING; // Estado del jugador en el juego
|
|
bool invulnerable_ = true; // Indica si el jugador es invulnerable
|
|
int invulnerable_counter_ = INVULNERABLE_COUNTER_; // Contador para la invulnerabilidad
|
|
bool extra_hit_ = false; // Indica si el jugador tiene un toque extra
|
|
int coffees_ = 0; // Indica cuantos cafes lleva acumulados
|
|
bool power_up_ = false; // Indica si el jugador tiene activo el modo PowerUp
|
|
int power_up_counter_ = POWERUP_COUNTER_; // Temporizador para el modo PowerUp
|
|
int power_up_desp_x_ = 0; // Desplazamiento del sprite de PowerUp respecto al sprite del jugador
|
|
Circle collider_ = Circle(0, 0, 9); // Circulo de colisión del jugador
|
|
int continue_counter_ = 10; // Contador para poder continuar
|
|
Uint32 continue_ticks_ = 0; // Variable para poder cambiar el contador de continue en función del tiempo
|
|
int scoreboard_panel_ = 0; // Panel del marcador asociado al jugador
|
|
std::string name_; // Nombre del jugador
|
|
int controller_index_ = 0; // Indice del array de mandos que utilizará para moverse
|
|
bool demo_; // Para que el jugador sepa si está en el modo demostración
|
|
int enter_name_counter_; // Contador para poner nombre
|
|
Uint32 enter_name_ticks_ = 0; // Variable para poder cambiar el contador de poner nombre en función del tiempo
|
|
|
|
// Actualiza el circulo de colisión a la posición del jugador
|
|
void shiftColliders();
|
|
|
|
// Recoloca el sprite
|
|
void shiftSprite();
|
|
|
|
// Monitoriza el estado
|
|
void updateInvulnerable();
|
|
|
|
// Actualiza el contador de continue
|
|
void updateContinueCounter();
|
|
|
|
// Actualiza el contador de entrar nombre
|
|
void updateEnterNameCounter();
|
|
|
|
// Decrementa el contador de entrar nombre
|
|
void decEnterNameCounter();
|
|
|
|
// Actualiza el panel del marcador
|
|
void updateScoreboard();
|
|
|
|
// Cambia el modo del marcador
|
|
void setScoreboardMode(ScoreboardMode mode);
|
|
|
|
bool isRenderable() const { return isPlaying() || isDying() || isCelebrating() || isEnteringNameGameCompleted() || isGameCompleted(); }
|
|
|
|
public:
|
|
// Constructor
|
|
Player(int id, float x, int y, bool demo, SDL_Rect &play_area, std::vector<std::shared_ptr<Texture>> texture, const std::vector<std::vector<std::string>> &animations);
|
|
|
|
// Destructor
|
|
~Player() = default;
|
|
|
|
// Iniciador
|
|
void init();
|
|
|
|
// Actualiza al jugador a su posicion, animación y controla los contadores
|
|
void update();
|
|
|
|
// Pinta el jugador en pantalla
|
|
void render();
|
|
|
|
// Pone las texturas del jugador
|
|
void setPlayerTextures(const std::vector<std::shared_ptr<Texture>> &texture);
|
|
|
|
// Actua en consecuencia de la entrada recibida
|
|
void setInput(InputType input);
|
|
|
|
// Procesa inputs para cuando está jugando
|
|
void setInputPlaying(InputType input);
|
|
|
|
// Procesa inputs para cuando está introduciendo el nombre
|
|
void setInputEnteringName(InputType input);
|
|
|
|
// Mueve el jugador a la posición y animación que le corresponde
|
|
void move();
|
|
|
|
// Establece la animación correspondiente al estado
|
|
void setAnimation();
|
|
|
|
// Actualiza el valor de la variable
|
|
void updateCooldown();
|
|
|
|
// Incrementa la puntuación del jugador
|
|
void addScore(int score);
|
|
|
|
// Establece el estado del jugador en el juego
|
|
void setPlayingState(PlayerState state);
|
|
|
|
// Aumenta el valor de la variable hasta un máximo
|
|
void incScoreMultiplier();
|
|
|
|
// Decrementa el valor de la variable hasta un mínimo
|
|
void decScoreMultiplier();
|
|
|
|
// Establece el valor del estado
|
|
void setInvulnerable(bool value);
|
|
|
|
// Establece el valor de la variable a verdadero
|
|
void setPowerUp();
|
|
|
|
// Actualiza el valor de la variable
|
|
void updatePowerUp();
|
|
|
|
// Concede un toque extra al jugador
|
|
void giveExtraHit();
|
|
|
|
// Quita el toque extra al jugador
|
|
void removeExtraHit();
|
|
|
|
// Decrementa el contador de continuar
|
|
void decContinueCounter();
|
|
|
|
// Obtiene la posición que se está editando del nombre del jugador para la tabla de mejores puntuaciones
|
|
int getRecordNamePos() const;
|
|
|
|
bool canFire() const { return cooldown_ > 0 ? false : true; }
|
|
bool hasDied() const { return playing_state_ == PlayerState::DIED; }
|
|
bool hasExtraHit() const { return extra_hit_; }
|
|
bool isCelebrating() const { return playing_state_ == PlayerState::CELEBRATING; }
|
|
bool isContinue() const { return playing_state_ == PlayerState::CONTINUE; }
|
|
bool isCooling() { return firing_state_ == PlayerState::COOLING_LEFT || firing_state_ == PlayerState::COOLING_UP || firing_state_ == PlayerState::COOLING_RIGHT; }
|
|
bool isDying() const { return playing_state_ == PlayerState::DYING; }
|
|
bool IsEligibleForHighScore() { return score_ > options.game.hi_score_table.back().score; }
|
|
bool isEnteringName() const { return playing_state_ == PlayerState::ENTERING_NAME; }
|
|
bool isEnteringNameGameCompleted() const { return playing_state_ == PlayerState::ENTERING_NAME_GAME_COMPLETED; }
|
|
bool isGameCompleted() const { return playing_state_ == PlayerState::GAME_COMPLETED; }
|
|
bool isGameOver() const { return playing_state_ == PlayerState::GAME_OVER; }
|
|
bool isInvulnerable() const { return invulnerable_; }
|
|
bool isPlaying() const { return playing_state_ == PlayerState::PLAYING; }
|
|
bool isPowerUp() const { return power_up_; }
|
|
bool isWaiting() const { return playing_state_ == PlayerState::WAITING; }
|
|
Circle &getCollider() { return collider_; }
|
|
float getScoreMultiplier() const { return score_multiplier_; }
|
|
int getCoffees() const { return coffees_; }
|
|
int getContinueCounter() const { return continue_counter_; }
|
|
int getController() const { return controller_index_; }
|
|
int getHeight() const { return HEIGHT_; }
|
|
int getId() const { return id_; }
|
|
int getInvulnerableCounter() const { return invulnerable_counter_; }
|
|
int getPosX() const { return static_cast<int>(pos_x_); }
|
|
int getPosY() const { return pos_y_; }
|
|
int getPowerUpCounter() const { return power_up_counter_; }
|
|
std::string getRecordName() const { return enter_name_->getName(); }
|
|
int getScore() const { return score_; }
|
|
int getScoreBoardPanel() const { return scoreboard_panel_; }
|
|
int getWidth() const { return WIDTH_; }
|
|
PlayerState getPlayingState() const { return playing_state_; }
|
|
std::string getName() const { return name_; }
|
|
void setController(int index) { controller_index_ = index; }
|
|
void setFireCooldown(int time) { cooldown_ = time; }
|
|
void setFiringState(PlayerState state) { firing_state_ = state; }
|
|
void setInvulnerableCounter(int value) { invulnerable_counter_ = value; }
|
|
void setName(const std::string &name) { name_ = name; }
|
|
void setPowerUpCounter(int value) { power_up_counter_ = value; }
|
|
void setScore(int score) { score_ = score; }
|
|
void setScoreBoardPanel(int panel) { scoreboard_panel_ = panel; }
|
|
void setScoreMultiplier(float value) { score_multiplier_ = value; }
|
|
void setWalkingState(PlayerState state) { walking_state_ = state; }
|
|
};
|