delta-time: tabe.cpp
This commit is contained in:
103
source/tabe.cpp
103
source/tabe.cpp
@@ -17,7 +17,7 @@ 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_(Timer(param.tabe.min_spawn_time, param.tabe.max_spawn_time)) {}
|
timer_(Timer(param.tabe.min_spawn_time, param.tabe.max_spawn_time)) {}
|
||||||
|
|
||||||
// Actualiza la lógica
|
// Actualiza la lógica (frame-based)
|
||||||
void Tabe::update() {
|
void Tabe::update() {
|
||||||
if (enabled_ && !timer_.is_paused) {
|
if (enabled_ && !timer_.is_paused) {
|
||||||
sprite_->update();
|
sprite_->update();
|
||||||
@@ -31,6 +31,20 @@ void Tabe::update() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Actualiza la lógica (time-based)
|
||||||
|
void Tabe::update(float deltaTime) {
|
||||||
|
if (enabled_ && !timer_.is_paused) {
|
||||||
|
sprite_->update(deltaTime);
|
||||||
|
move(deltaTime);
|
||||||
|
updateState(deltaTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
timer_.update();
|
||||||
|
if (timer_.shouldSpawn()) {
|
||||||
|
enable();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Dibuja el objeto
|
// Dibuja el objeto
|
||||||
void Tabe::render() {
|
void Tabe::render() {
|
||||||
if (enabled_) {
|
if (enabled_) {
|
||||||
@@ -38,7 +52,7 @@ void Tabe::render() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mueve el objeto
|
// Mueve el objeto (frame-based)
|
||||||
void Tabe::move() {
|
void Tabe::move() {
|
||||||
const int X = static_cast<int>(x_);
|
const int X = static_cast<int>(x_);
|
||||||
speed_ += accel_;
|
speed_ += accel_;
|
||||||
@@ -103,6 +117,75 @@ void Tabe::move() {
|
|||||||
shiftSprite();
|
shiftSprite();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Mueve el objeto (time-based)
|
||||||
|
void Tabe::move(float deltaTime) {
|
||||||
|
// Convertir deltaTime (milisegundos) a factor de frame (asumiendo 60fps)
|
||||||
|
float frameFactor = deltaTime / (1000.0f / 60.0f);
|
||||||
|
|
||||||
|
const int X = static_cast<int>(x_);
|
||||||
|
speed_ += accel_ * frameFactor;
|
||||||
|
x_ += speed_ * frameFactor;
|
||||||
|
fly_distance_ -= std::abs(X - static_cast<int>(x_));
|
||||||
|
|
||||||
|
// Comprueba si sale por los bordes
|
||||||
|
const float MIN_X = param.game.game_area.rect.x - WIDTH;
|
||||||
|
const float MAX_X = param.game.game_area.rect.x + param.game.game_area.rect.w;
|
||||||
|
switch (destiny_) {
|
||||||
|
case Direction::TO_THE_LEFT: {
|
||||||
|
if (x_ < MIN_X) {
|
||||||
|
disable();
|
||||||
|
}
|
||||||
|
if (x_ > param.game.game_area.rect.x + param.game.game_area.rect.w - WIDTH && direction_ == Direction::TO_THE_RIGHT) {
|
||||||
|
setRandomFlyPath(Direction::TO_THE_LEFT, 80);
|
||||||
|
x_ = param.game.game_area.rect.x + param.game.game_area.rect.w - WIDTH;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case Direction::TO_THE_RIGHT: {
|
||||||
|
if (x_ > MAX_X) {
|
||||||
|
disable();
|
||||||
|
}
|
||||||
|
if (x_ < param.game.game_area.rect.x && direction_ == Direction::TO_THE_LEFT) {
|
||||||
|
setRandomFlyPath(Direction::TO_THE_RIGHT, 80);
|
||||||
|
x_ = param.game.game_area.rect.x;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fly_distance_ <= 0) {
|
||||||
|
if (waiting_counter_ > 0) {
|
||||||
|
accel_ = speed_ = 0.0F;
|
||||||
|
waiting_counter_ -= frameFactor;
|
||||||
|
if (waiting_counter_ < 0) waiting_counter_ = 0;
|
||||||
|
} else {
|
||||||
|
constexpr int CHOICES = 4;
|
||||||
|
const std::array<Direction, CHOICES> LEFT = {
|
||||||
|
Direction::TO_THE_LEFT,
|
||||||
|
Direction::TO_THE_LEFT,
|
||||||
|
Direction::TO_THE_LEFT,
|
||||||
|
Direction::TO_THE_RIGHT};
|
||||||
|
|
||||||
|
const std::array<Direction, CHOICES> RIGHT = {
|
||||||
|
Direction::TO_THE_LEFT,
|
||||||
|
Direction::TO_THE_RIGHT,
|
||||||
|
Direction::TO_THE_RIGHT,
|
||||||
|
Direction::TO_THE_RIGHT};
|
||||||
|
|
||||||
|
const Direction DIRECTION = destiny_ == Direction::TO_THE_LEFT
|
||||||
|
? LEFT[rand() % CHOICES]
|
||||||
|
: RIGHT[rand() % CHOICES];
|
||||||
|
|
||||||
|
setRandomFlyPath(DIRECTION, 20 + (rand() % 40));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
shiftSprite();
|
||||||
|
}
|
||||||
|
|
||||||
// Habilita el objeto
|
// Habilita el objeto
|
||||||
void Tabe::enable() {
|
void Tabe::enable() {
|
||||||
if (!enabled_) {
|
if (!enabled_) {
|
||||||
@@ -175,11 +258,23 @@ void Tabe::setState(State state) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Actualiza el estado
|
// Actualiza el estado (frame-based)
|
||||||
void Tabe::updateState() {
|
void Tabe::updateState() {
|
||||||
if (state_ == State::HIT) {
|
if (state_ == State::HIT) {
|
||||||
--hit_counter_;
|
--hit_counter_;
|
||||||
if (hit_counter_ == 0) {
|
if (hit_counter_ <= 0) {
|
||||||
|
setState(State::FLY);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Actualiza el estado (time-based)
|
||||||
|
void Tabe::updateState(float deltaTime) {
|
||||||
|
if (state_ == State::HIT) {
|
||||||
|
// Convertir deltaTime (milisegundos) a factor de frame (asumiendo 60fps)
|
||||||
|
float frameFactor = deltaTime / (1000.0f / 60.0f);
|
||||||
|
hit_counter_ -= frameFactor;
|
||||||
|
if (hit_counter_ <= 0) {
|
||||||
setState(State::FLY);
|
setState(State::FLY);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,7 +26,8 @@ class Tabe {
|
|||||||
~Tabe() = default;
|
~Tabe() = default;
|
||||||
|
|
||||||
// --- Métodos principales ---
|
// --- Métodos principales ---
|
||||||
void update(); // Actualiza la lógica
|
void update(); // Actualiza la lógica (frame-based)
|
||||||
|
void update(float deltaTime); // Actualiza la lógica (time-based)
|
||||||
void render(); // Dibuja el objeto
|
void render(); // Dibuja el objeto
|
||||||
void enable(); // Habilita el objeto
|
void enable(); // Habilita el objeto
|
||||||
void setState(State state); // Establece el estado
|
void setState(State state); // Establece el estado
|
||||||
@@ -130,21 +131,23 @@ class Tabe {
|
|||||||
float speed_ = 0.0F; // Velocidad de movimiento
|
float speed_ = 0.0F; // Velocidad de movimiento
|
||||||
float accel_ = 0.0F; // Aceleración
|
float accel_ = 0.0F; // Aceleración
|
||||||
int fly_distance_ = 0; // Distancia de vuelo
|
int fly_distance_ = 0; // Distancia de vuelo
|
||||||
int waiting_counter_ = 0; // Tiempo que pasa quieto
|
float waiting_counter_ = 0; // Tiempo que pasa quieto
|
||||||
bool enabled_ = false; // Indica si el objeto está activo
|
bool enabled_ = false; // Indica si el objeto está activo
|
||||||
Direction direction_ = Direction::TO_THE_LEFT; // Dirección actual
|
Direction direction_ = Direction::TO_THE_LEFT; // Dirección actual
|
||||||
Direction destiny_ = Direction::TO_THE_LEFT; // Destino
|
Direction destiny_ = Direction::TO_THE_LEFT; // Destino
|
||||||
State state_ = State::FLY; // Estado actual
|
State state_ = State::FLY; // Estado actual
|
||||||
int hit_counter_ = 0; // Contador para el estado HIT
|
float hit_counter_ = 0; // Contador para el estado HIT
|
||||||
int number_of_hits_ = 0; // Cantidad de disparos recibidos
|
int number_of_hits_ = 0; // Cantidad de disparos recibidos
|
||||||
bool has_bonus_ = true; // Indica si aún tiene el bonus para soltar
|
bool has_bonus_ = true; // Indica si aún tiene el bonus para soltar
|
||||||
Timer timer_; // Temporizador para gestionar la aparición
|
Timer timer_; // Temporizador para gestionar la aparición
|
||||||
|
|
||||||
// --- Métodos internos ---
|
// --- Métodos internos ---
|
||||||
void move(); // Mueve el objeto
|
void move(); // Mueve el objeto (frame-based)
|
||||||
|
void move(float deltaTime); // Mueve el objeto (time-based)
|
||||||
void shiftSprite() { sprite_->setPos(x_, y_); } // Actualiza la posición del sprite
|
void shiftSprite() { sprite_->setPos(x_, y_); } // Actualiza la posición del sprite
|
||||||
void setRandomFlyPath(Direction direction, int length); // Establece un vuelo aleatorio
|
void setRandomFlyPath(Direction direction, int length); // Establece un vuelo aleatorio
|
||||||
void updateState(); // Actualiza el estado
|
void updateState(); // Actualiza el estado (frame-based)
|
||||||
|
void updateState(float deltaTime); // Actualiza el estado (time-based)
|
||||||
void updateTimer(); // Actualiza el temporizador
|
void updateTimer(); // Actualiza el temporizador
|
||||||
void disable(); // Deshabilita el objeto
|
void disable(); // Deshabilita el objeto
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user