pasaeta loca de clang-format (despres m'arrepentiré pero bueno)
This commit is contained in:
184
source/tabe.cpp
184
source/tabe.cpp
@@ -1,16 +1,17 @@
|
||||
// IWYU pragma: no_include <bits/std_abs.h>
|
||||
#include "tabe.h"
|
||||
|
||||
#include <SDL3/SDL.h> // Para SDL_FlipMode, SDL_GetTicks
|
||||
#include <stdlib.h> // Para rand, abs
|
||||
#include <SDL3/SDL.h> // Para SDL_FlipMode, SDL_GetTicks
|
||||
#include <stdlib.h> // Para rand, abs
|
||||
|
||||
#include <algorithm> // Para max
|
||||
#include <cmath> // Para abs
|
||||
#include <string> // Para basic_string
|
||||
|
||||
#include "audio.h" // Para Audio
|
||||
#include "param.h" // Para Param, ParamGame, param
|
||||
#include "resource.h" // Para Resource
|
||||
#include "utils.h" // Para Zone
|
||||
#include "audio.h" // Para Audio
|
||||
#include "param.h" // Para Param, ParamGame, param
|
||||
#include "resource.h" // Para Resource
|
||||
#include "utils.h" // Para Zone
|
||||
|
||||
// Constructor
|
||||
Tabe::Tabe()
|
||||
@@ -18,33 +19,27 @@ Tabe::Tabe()
|
||||
timer_(TabeTimer(2.5f, 4.0f)) {}
|
||||
|
||||
// Actualiza la lógica
|
||||
void Tabe::update()
|
||||
{
|
||||
if (enabled_)
|
||||
{
|
||||
void Tabe::update() {
|
||||
if (enabled_) {
|
||||
sprite_->update();
|
||||
move();
|
||||
updateState();
|
||||
}
|
||||
timer_.update();
|
||||
if (timer_.should_spawn())
|
||||
{
|
||||
if (timer_.should_spawn()) {
|
||||
enable();
|
||||
}
|
||||
}
|
||||
|
||||
// Dibuja el objeto
|
||||
void Tabe::render()
|
||||
{
|
||||
if (enabled_)
|
||||
{
|
||||
void Tabe::render() {
|
||||
if (enabled_) {
|
||||
sprite_->render();
|
||||
}
|
||||
}
|
||||
|
||||
// Mueve el objeto
|
||||
void Tabe::move()
|
||||
{
|
||||
void Tabe::move() {
|
||||
const int x = static_cast<int>(x_);
|
||||
speed_ += accel_;
|
||||
x_ += speed_;
|
||||
@@ -53,48 +48,37 @@ void Tabe::move()
|
||||
// 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 TabeDirection::TO_THE_LEFT:
|
||||
{
|
||||
if (x_ < min_x)
|
||||
{
|
||||
disable();
|
||||
switch (destiny_) {
|
||||
case TabeDirection::TO_THE_LEFT: {
|
||||
if (x_ < min_x) {
|
||||
disable();
|
||||
}
|
||||
if (x_ > param.game.game_area.rect.x + param.game.game_area.rect.w - WIDTH_ && direction_ == TabeDirection::TO_THE_RIGHT) {
|
||||
setRandomFlyPath(TabeDirection::TO_THE_LEFT, 80);
|
||||
x_ = param.game.game_area.rect.x + param.game.game_area.rect.w - WIDTH_;
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (x_ > param.game.game_area.rect.x + param.game.game_area.rect.w - WIDTH_ && direction_ == TabeDirection::TO_THE_RIGHT)
|
||||
{
|
||||
setRandomFlyPath(TabeDirection::TO_THE_LEFT, 80);
|
||||
x_ = param.game.game_area.rect.x + param.game.game_area.rect.w - WIDTH_;
|
||||
|
||||
case TabeDirection::TO_THE_RIGHT: {
|
||||
if (x_ > max_x) {
|
||||
disable();
|
||||
}
|
||||
if (x_ < param.game.game_area.rect.x && direction_ == TabeDirection::TO_THE_LEFT) {
|
||||
setRandomFlyPath(TabeDirection::TO_THE_RIGHT, 80);
|
||||
x_ = param.game.game_area.rect.x;
|
||||
}
|
||||
break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
case TabeDirection::TO_THE_RIGHT:
|
||||
{
|
||||
if (x_ > max_x)
|
||||
{
|
||||
disable();
|
||||
}
|
||||
if (x_ < param.game.game_area.rect.x && direction_ == TabeDirection::TO_THE_LEFT)
|
||||
{
|
||||
setRandomFlyPath(TabeDirection::TO_THE_RIGHT, 80);
|
||||
x_ = param.game.game_area.rect.x;
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (fly_distance_ <= 0)
|
||||
{
|
||||
if (waiting_counter_ > 0)
|
||||
{
|
||||
if (fly_distance_ <= 0) {
|
||||
if (waiting_counter_ > 0) {
|
||||
accel_ = speed_ = 0.0f;
|
||||
--waiting_counter_;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
constexpr int CHOICES = 4;
|
||||
const TabeDirection left[CHOICES] = {TabeDirection::TO_THE_LEFT, TabeDirection::TO_THE_LEFT, TabeDirection::TO_THE_LEFT, TabeDirection::TO_THE_RIGHT};
|
||||
const TabeDirection right[CHOICES] = {TabeDirection::TO_THE_LEFT, TabeDirection::TO_THE_RIGHT, TabeDirection::TO_THE_RIGHT, TabeDirection::TO_THE_RIGHT};
|
||||
@@ -107,10 +91,8 @@ void Tabe::move()
|
||||
}
|
||||
|
||||
// Habilita el objeto
|
||||
void Tabe::enable()
|
||||
{
|
||||
if (!enabled_)
|
||||
{
|
||||
void Tabe::enable() {
|
||||
if (!enabled_) {
|
||||
enabled_ = true;
|
||||
has_bonus_ = true;
|
||||
hit_counter_ = 0;
|
||||
@@ -130,8 +112,7 @@ void Tabe::enable()
|
||||
}
|
||||
|
||||
// Establece un vuelo aleatorio
|
||||
void Tabe::setRandomFlyPath(TabeDirection direction, int lenght)
|
||||
{
|
||||
void Tabe::setRandomFlyPath(TabeDirection direction, int lenght) {
|
||||
direction_ = direction;
|
||||
fly_distance_ = lenght;
|
||||
waiting_counter_ = 5 + rand() % 15;
|
||||
@@ -139,72 +120,61 @@ void Tabe::setRandomFlyPath(TabeDirection direction, int lenght)
|
||||
|
||||
constexpr float SPEED = 2.0f;
|
||||
|
||||
switch (direction)
|
||||
{
|
||||
case TabeDirection::TO_THE_LEFT:
|
||||
{
|
||||
speed_ = -1.0f * SPEED;
|
||||
accel_ = -1.0f * (1 + rand() % 10) / 30.0f;
|
||||
sprite_->setFlip(SDL_FLIP_NONE);
|
||||
break;
|
||||
}
|
||||
switch (direction) {
|
||||
case TabeDirection::TO_THE_LEFT: {
|
||||
speed_ = -1.0f * SPEED;
|
||||
accel_ = -1.0f * (1 + rand() % 10) / 30.0f;
|
||||
sprite_->setFlip(SDL_FLIP_NONE);
|
||||
break;
|
||||
}
|
||||
|
||||
case TabeDirection::TO_THE_RIGHT:
|
||||
{
|
||||
speed_ = SPEED;
|
||||
accel_ = (1 + rand() % 10) / 30.0f;
|
||||
sprite_->setFlip(SDL_FLIP_HORIZONTAL);
|
||||
break;
|
||||
}
|
||||
case TabeDirection::TO_THE_RIGHT: {
|
||||
speed_ = SPEED;
|
||||
accel_ = (1 + rand() % 10) / 30.0f;
|
||||
sprite_->setFlip(SDL_FLIP_HORIZONTAL);
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Establece el estado
|
||||
void Tabe::setState(TabeState state)
|
||||
{
|
||||
if (enabled_)
|
||||
{
|
||||
void Tabe::setState(TabeState state) {
|
||||
if (enabled_) {
|
||||
state_ = state;
|
||||
|
||||
switch (state)
|
||||
{
|
||||
case TabeState::FLY:
|
||||
sprite_->setCurrentAnimation("fly");
|
||||
break;
|
||||
switch (state) {
|
||||
case TabeState::FLY:
|
||||
sprite_->setCurrentAnimation("fly");
|
||||
break;
|
||||
|
||||
case TabeState::HIT:
|
||||
sprite_->setCurrentAnimation("hit");
|
||||
hit_counter_ = 5;
|
||||
++number_of_hits_;
|
||||
break;
|
||||
case TabeState::HIT:
|
||||
sprite_->setCurrentAnimation("hit");
|
||||
hit_counter_ = 5;
|
||||
++number_of_hits_;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza el estado
|
||||
void Tabe::updateState()
|
||||
{
|
||||
if (state_ == TabeState::HIT)
|
||||
{
|
||||
void Tabe::updateState() {
|
||||
if (state_ == TabeState::HIT) {
|
||||
--hit_counter_;
|
||||
if (hit_counter_ == 0)
|
||||
{
|
||||
if (hit_counter_ == 0) {
|
||||
setState(TabeState::FLY);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Intenta obtener el bonus
|
||||
bool Tabe::tryToGetBonus()
|
||||
{
|
||||
if (has_bonus_ && rand() % std::max(1, 15 - number_of_hits_) == 0)
|
||||
{
|
||||
bool Tabe::tryToGetBonus() {
|
||||
if (has_bonus_ && rand() % std::max(1, 15 - number_of_hits_) == 0) {
|
||||
has_bonus_ = false;
|
||||
return true;
|
||||
}
|
||||
@@ -212,16 +182,14 @@ bool Tabe::tryToGetBonus()
|
||||
}
|
||||
|
||||
// Actualiza el temporizador
|
||||
void Tabe::updateTimer()
|
||||
{
|
||||
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()
|
||||
{
|
||||
void Tabe::disable() {
|
||||
enabled_ = false;
|
||||
timer_.reset();
|
||||
}
|
||||
Reference in New Issue
Block a user