Afegit el disparador per a la aparició del enemic nou
This commit is contained in:
@@ -1,16 +1,18 @@
|
|||||||
// IWYU pragma: no_include <bits/std_abs.h>
|
// IWYU pragma: no_include <bits/std_abs.h>
|
||||||
#include "tabe.h"
|
#include "tabe.h"
|
||||||
#include <SDL2/SDL_render.h> // Para SDL_FLIP_HORIZONTAL, SDL_FLIP_NONE
|
#include <SDL2/SDL_render.h> // Para SDL_FLIP_HORIZONTAL, SDL_FLIP_NONE
|
||||||
#include <stdlib.h> // Para rand, abs
|
#include <SDL2/SDL.h>
|
||||||
#include <algorithm> // Para max
|
#include <stdlib.h> // Para rand, abs
|
||||||
#include "jail_audio.h" // Para JA_PlaySound
|
#include <algorithm> // Para max
|
||||||
#include "param.h" // Para Param, ParamGame, param
|
#include "jail_audio.h" // Para JA_PlaySound
|
||||||
#include "resource.h" // Para Resource
|
#include "param.h" // Para Param, ParamGame, param
|
||||||
#include "utils.h" // Para Zone
|
#include "resource.h" // Para Resource
|
||||||
|
#include "utils.h" // Para Zone
|
||||||
|
|
||||||
// Constructor
|
// Constructor
|
||||||
Tabe::Tabe()
|
Tabe::Tabe()
|
||||||
: sprite_(std::make_unique<AnimatedSprite>(Resource::get()->getTexture("tabe.png"), Resource::get()->getAnimation("tabe.ani"))) {}
|
: sprite_(std::make_unique<AnimatedSprite>(Resource::get()->getTexture("tabe.png"), Resource::get()->getAnimation("tabe.ani"))),
|
||||||
|
timer_(TabeTimer(2.5f, 4.0f)) {}
|
||||||
|
|
||||||
// Actualiza la lógica
|
// Actualiza la lógica
|
||||||
void Tabe::update()
|
void Tabe::update()
|
||||||
@@ -21,6 +23,11 @@ void Tabe::update()
|
|||||||
move();
|
move();
|
||||||
updateState();
|
updateState();
|
||||||
}
|
}
|
||||||
|
timer_.update();
|
||||||
|
if (timer_.should_spawn())
|
||||||
|
{
|
||||||
|
enable();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dibuja el objeto
|
// Dibuja el objeto
|
||||||
@@ -49,7 +56,7 @@ void Tabe::move()
|
|||||||
{
|
{
|
||||||
if (x_ < min_x)
|
if (x_ < min_x)
|
||||||
{
|
{
|
||||||
enabled_ = false;
|
disable();
|
||||||
}
|
}
|
||||||
if (x_ > param.game.game_area.rect.x + param.game.game_area.rect.w - WIDTH_ && direction_ == TabeDirection::TO_THE_RIGHT)
|
if (x_ > param.game.game_area.rect.x + param.game.game_area.rect.w - WIDTH_ && direction_ == TabeDirection::TO_THE_RIGHT)
|
||||||
{
|
{
|
||||||
@@ -63,7 +70,7 @@ void Tabe::move()
|
|||||||
{
|
{
|
||||||
if (x_ > max_x)
|
if (x_ > max_x)
|
||||||
{
|
{
|
||||||
enabled_ = false;
|
disable();
|
||||||
}
|
}
|
||||||
if (x_ < param.game.game_area.rect.x && direction_ == TabeDirection::TO_THE_LEFT)
|
if (x_ < param.game.game_area.rect.x && direction_ == TabeDirection::TO_THE_LEFT)
|
||||||
{
|
{
|
||||||
@@ -193,10 +200,25 @@ void Tabe::updateState()
|
|||||||
// Intenta obtener el bonus
|
// Intenta obtener el bonus
|
||||||
bool Tabe::tryToGetBonus()
|
bool Tabe::tryToGetBonus()
|
||||||
{
|
{
|
||||||
if (has_bonus_ && rand() % std::max(1, 10 - number_of_hits_) == 0)
|
if (has_bonus_ && rand() % std::max(1, 15 - number_of_hits_) == 0)
|
||||||
{
|
{
|
||||||
has_bonus_ = false;
|
has_bonus_ = false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Actualiza el temporizador
|
||||||
|
void Tabe::updateTimer()
|
||||||
|
{
|
||||||
|
timer_.current_time = SDL_GetTicks();
|
||||||
|
timer_.delta_time = timer_.current_time - timer_.last_time;
|
||||||
|
timer_.last_time = timer_.current_time;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Deshabilita el objeto
|
||||||
|
void Tabe::disable()
|
||||||
|
{
|
||||||
|
enabled_ = false;
|
||||||
|
timer_.reset();
|
||||||
|
}
|
||||||
@@ -1,8 +1,9 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <SDL2/SDL_rect.h> // Para SDL_Rect
|
#include <SDL2/SDL_rect.h> // Para SDL_Rect
|
||||||
#include <memory> // Para unique_ptr
|
#include <SDL2/SDL.h>
|
||||||
#include "animated_sprite.h" // Para AnimatedSprite
|
#include <memory> // Para unique_ptr
|
||||||
|
#include "animated_sprite.h" // Para AnimatedSprite
|
||||||
|
|
||||||
enum class TabeDirection : int
|
enum class TabeDirection : int
|
||||||
{
|
{
|
||||||
@@ -16,6 +17,55 @@ enum class TabeState : int
|
|||||||
HIT = 1,
|
HIT = 1,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct TabeTimer
|
||||||
|
{
|
||||||
|
Uint32 time_until_next_spawn; // Tiempo restante para la próxima aparición
|
||||||
|
Uint32 min_spawn_time; // Tiempo mínimo entre apariciones
|
||||||
|
Uint32 max_spawn_time; // Tiempo máximo entre apariciones
|
||||||
|
Uint32 current_time; // Tiempo actual
|
||||||
|
Uint32 delta_time; // Diferencia de tiempo desde la última actualización
|
||||||
|
Uint32 last_time; // Tiempo de la última actualización
|
||||||
|
|
||||||
|
// Constructor
|
||||||
|
TabeTimer(float minTime, float maxTime)
|
||||||
|
: min_spawn_time(minTime * 60000), max_spawn_time(maxTime * 60000)
|
||||||
|
{
|
||||||
|
current_time = SDL_GetTicks();
|
||||||
|
reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Restablece el temporizador con un nuevo tiempo hasta la próxima aparición
|
||||||
|
void reset()
|
||||||
|
{
|
||||||
|
Uint32 range = max_spawn_time - min_spawn_time;
|
||||||
|
time_until_next_spawn = min_spawn_time + rand() % (range + 1);
|
||||||
|
last_time = SDL_GetTicks();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Actualiza el temporizador, decrementando el tiempo hasta la próxima aparición
|
||||||
|
void update()
|
||||||
|
{
|
||||||
|
current_time = SDL_GetTicks();
|
||||||
|
delta_time = current_time - last_time;
|
||||||
|
last_time = current_time;
|
||||||
|
|
||||||
|
if (time_until_next_spawn > delta_time)
|
||||||
|
{
|
||||||
|
time_until_next_spawn -= delta_time;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
time_until_next_spawn = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Indica si el temporizador ha finalizado
|
||||||
|
bool should_spawn() const
|
||||||
|
{
|
||||||
|
return time_until_next_spawn == 0;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// Clase Tabe
|
// Clase Tabe
|
||||||
class Tabe
|
class Tabe
|
||||||
{
|
{
|
||||||
@@ -41,6 +91,7 @@ private:
|
|||||||
int hit_counter_ = 0; // Contador para el estado HIT
|
int hit_counter_ = 0; // Contador para el estado HIT
|
||||||
int number_of_hits_ = 0; // Cantidad de disparos que ha recibido
|
int number_of_hits_ = 0; // Cantidad de disparos que ha recibido
|
||||||
bool has_bonus_ = true; // Indica si el Tabe aun tiene el bonus para soltar
|
bool has_bonus_ = true; // Indica si el Tabe aun tiene el bonus para soltar
|
||||||
|
TabeTimer timer_; // Temporizador para gestionar la aparición del Tabe
|
||||||
|
|
||||||
// Mueve el objeto
|
// Mueve el objeto
|
||||||
void move();
|
void move();
|
||||||
@@ -54,6 +105,12 @@ private:
|
|||||||
// Actualiza el estado
|
// Actualiza el estado
|
||||||
void updateState();
|
void updateState();
|
||||||
|
|
||||||
|
// Actualiza el temporizador
|
||||||
|
void updateTimer();
|
||||||
|
|
||||||
|
// Deshabilita el objeto
|
||||||
|
void disable();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// Constructor
|
// Constructor
|
||||||
Tabe();
|
Tabe();
|
||||||
|
|||||||
Reference in New Issue
Block a user