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"))),
|
||||
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() {
|
||||
if (enabled_ && !timer_.is_paused) {
|
||||
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
|
||||
void Tabe::render() {
|
||||
if (enabled_) {
|
||||
@@ -38,7 +52,7 @@ void Tabe::render() {
|
||||
}
|
||||
}
|
||||
|
||||
// Mueve el objeto
|
||||
// Mueve el objeto (frame-based)
|
||||
void Tabe::move() {
|
||||
const int X = static_cast<int>(x_);
|
||||
speed_ += accel_;
|
||||
@@ -103,6 +117,75 @@ void Tabe::move() {
|
||||
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
|
||||
void Tabe::enable() {
|
||||
if (!enabled_) {
|
||||
@@ -175,11 +258,23 @@ void Tabe::setState(State state) {
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza el estado
|
||||
// Actualiza el estado (frame-based)
|
||||
void Tabe::updateState() {
|
||||
if (state_ == State::HIT) {
|
||||
--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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user