Item: nou spawn de la máquina de café

This commit is contained in:
2025-06-29 18:50:43 +02:00
parent e058b12f07
commit b71e923975
7 changed files with 95 additions and 34 deletions

View File

@@ -1,7 +1,5 @@
## --- GAME --- ## --- GAME ---
game.item_size 20 # Tamaño de los items del juego (en píxeles) game.item_size 20 # Tamaño de los items del juego (en píxeles)
game.coffee_machine_w 32 # Ancho de la máquina de café (en píxeles)
game.coffee_machine_h 39 # Alto de la máquina de café (en píxeles)
game.width 320 # Ancho de la resolución nativa del juego (en píxeles) game.width 320 # Ancho de la resolución nativa del juego (en píxeles)
game.height 240 # Alto de la resolución nativa del juego (en píxeles) game.height 240 # Alto de la resolución nativa del juego (en píxeles)
game.play_area.rect.x 0 # Posición X de la zona jugable game.play_area.rect.x 0 # Posición X de la zona jugable

View File

@@ -1,7 +1,5 @@
## --- GAME --- ## --- GAME ---
game.item_size 20 # Tamaño de los items del juego (en píxeles) game.item_size 20 # Tamaño de los items del juego (en píxeles)
game.coffee_machine_w 32 # Ancho de la máquina de café (en píxeles)
game.coffee_machine_h 39 # Alto de la máquina de café (en píxeles)
game.width 320 # Ancho de la resolución nativa del juego (en píxeles) game.width 320 # Ancho de la resolución nativa del juego (en píxeles)
game.height 256 # Alto de la resolución nativa del juego (en píxeles) game.height 256 # Alto de la resolución nativa del juego (en píxeles)
game.play_area.rect.x 0 # Posición X de la zona jugable game.play_area.rect.x 0 # Posición X de la zona jugable

View File

@@ -14,11 +14,11 @@ Item::Item(ItemType type, float x, float y, SDL_FRect &play_area, std::shared_pt
{ {
case ItemType::COFFEE_MACHINE: case ItemType::COFFEE_MACHINE:
{ {
width_ = param.game.coffee_machine_w; width_ = COFFEE_MACHINE_WIDTH;
height_ = param.game.coffee_machine_h; height_ = COFFEE_MACHINE_HEIGHT;
pos_x_ = (static_cast<int>(x + (play_area.w / 2)) % static_cast<int>(play_area.w - width_ - 5)) + 2; pos_x_ = getCoffeeMachineSpawn(x, width_, play_area_.w);
pos_y_ = y; pos_y_ = y;
vel_x_ = 0.0f; vel_x_ = ((rand() % 3) - 1) * 0.5f;
vel_y_ = -0.1f; vel_y_ = -0.1f;
accel_y_ = 0.1f; accel_y_ = 0.1f;
collider_.r = 10; collider_.r = 10;
@@ -86,12 +86,12 @@ void Item::move()
vel_y_ += accel_y_; vel_y_ += accel_y_;
// Comprueba los laterales de la zona de juego // Comprueba los laterales de la zona de juego
const float min_x = param.game.play_area.rect.x; const float MIN_X = param.game.play_area.rect.x;
const float max_x = play_area_.w - width_; const float MAX_X = play_area_.w - width_;
pos_x_ = std::clamp(pos_x_, min_x, max_x); pos_x_ = std::clamp(pos_x_, MIN_X, MAX_X);
// Si toca el borde lateral, invierte la velocidad horizontal // Si toca el borde lateral, invierte la velocidad horizontal
if (pos_x_ == min_x || pos_x_ == max_x) if (pos_x_ == MIN_X || pos_x_ == MAX_X)
{ {
vel_x_ = -vel_x_; vel_x_ = -vel_x_;
} }
@@ -112,14 +112,24 @@ void Item::move()
// Corrige la posición // Corrige la posición
pos_y_ = play_area_.h - height_; pos_y_ = play_area_.h - height_;
if (type_ == ItemType::COFFEE_MACHINE) switch (type_)
{ {
// Si es una máquina de café, detiene el objeto case ItemType::COFFEE_MACHINE:
vel_y_ = vel_x_ = accel_x_ = accel_y_ = 0; // La máquina de café es mas pesada y tiene una fisica diferente, ademas hace ruido
floor_collision_ = true; floor_collision_ = true;
} if (vel_y_ < 1.0f)
else {
{ // Si la velocidad vertical es baja, detiene el objeto
vel_y_ = vel_x_ = accel_x_ = accel_y_ = 0;
}
else
{
// Si la velocidad vertical es alta, el objeto rebota y pierde velocidad
vel_y_ *= -0.20f;
vel_x_ *= 0.75f;
}
break;
default:
// Si no es una máquina de café // Si no es una máquina de café
if (vel_y_ < 1.0f) if (vel_y_ < 1.0f)
{ {
@@ -132,6 +142,7 @@ void Item::move()
vel_y_ *= -0.5f; vel_y_ *= -0.5f;
vel_x_ *= 0.75f; vel_x_ *= 0.75f;
} }
break;
} }
} }
@@ -172,3 +183,64 @@ void Item::shiftSprite()
sprite_->setPosX(pos_x_); sprite_->setPosX(pos_x_);
sprite_->setPosY(pos_y_); sprite_->setPosY(pos_y_);
} }
// Calcula la zona de aparición de la máquina de café
int Item::getCoffeeMachineSpawn(int player_x, int item_width, int area_width, int margin)
{
// Distancia mínima del jugador (ajusta según necesites)
const int MIN_DISTANCE_FROM_PLAYER = area_width / 2;
const int LEFT_BOUND = margin;
const int RIGHT_BOUND = area_width - item_width - margin;
// Calcular zona de exclusión alrededor del jugador
int exclude_left = player_x - MIN_DISTANCE_FROM_PLAYER;
int exclude_right = player_x + MIN_DISTANCE_FROM_PLAYER;
// Verificar si hay espacio suficiente a la izquierda
bool can_spawn_left = (exclude_left > LEFT_BOUND) && (exclude_left - LEFT_BOUND > item_width);
// Verificar si hay espacio suficiente a la derecha
bool can_spawn_right = (exclude_right < RIGHT_BOUND) && (RIGHT_BOUND - exclude_right > item_width);
if (can_spawn_left && can_spawn_right)
{
// Ambos lados disponibles, elegir aleatoriamente
if (rand() % 2 == 0)
{
// Lado izquierdo
return rand() % (exclude_left - LEFT_BOUND) + LEFT_BOUND;
}
else
{
// Lado derecho
return rand() % (RIGHT_BOUND - exclude_right) + exclude_right;
}
}
else if (can_spawn_left)
{
// Solo lado izquierdo disponible
return rand() % (exclude_left - LEFT_BOUND) + LEFT_BOUND;
}
else if (can_spawn_right)
{
// Solo lado derecho disponible
return rand() % (RIGHT_BOUND - exclude_right) + exclude_right;
}
else
{
// No hay espacio suficiente lejos del jugador
// Por ahora, intentar spawn en el extremo más lejano posible
int distance_to_left = abs(player_x - LEFT_BOUND);
int distance_to_right = abs(RIGHT_BOUND - player_x);
if (distance_to_left > distance_to_right)
{
return LEFT_BOUND;
}
else
{
return RIGHT_BOUND - item_width;
}
}
}

View File

@@ -29,6 +29,10 @@ enum class ItemType : int
class Item class Item
{ {
public: public:
// Constantes
static constexpr int COFFEE_MACHINE_WIDTH = 30;
static constexpr int COFFEE_MACHINE_HEIGHT = 39;
// Constructor. Inicializa un objeto Item con el tipo, posición, área de juego, textura y animación. // Constructor. Inicializa un objeto Item con el tipo, posición, área de juego, textura y animación.
Item(ItemType type, float x, float y, SDL_FRect &play_area, std::shared_ptr<Texture> texture, const std::vector<std::string> &animation); Item(ItemType type, float x, float y, SDL_FRect &play_area, std::shared_ptr<Texture> texture, const std::vector<std::string> &animation);
@@ -95,4 +99,7 @@ private:
// Actualiza el contador de tiempo de vida del objeto. // Actualiza el contador de tiempo de vida del objeto.
// Si el tiempo de vida es mayor a 0, lo decrementa. Si llega a 0, desactiva el objeto. // Si el tiempo de vida es mayor a 0, lo decrementa. Si llega a 0, desactiva el objeto.
void updateTimeToLive(); void updateTimeToLive();
// Calcula la zona de aparición de la máquina de café
int getCoffeeMachineSpawn(int player_x, int item_width, int area_width, int margin = 2);
}; };

View File

@@ -21,8 +21,6 @@ void initParam()
param.game.width = 320; param.game.width = 320;
param.game.height = 256; param.game.height = 256;
param.game.item_size = 20; param.game.item_size = 20;
param.game.coffee_machine_w = 28;
param.game.coffee_machine_h = 37;
param.game.game_area.rect = {0, 0, param.game.width, param.game.height}; param.game.game_area.rect = {0, 0, param.game.width, param.game.height};
param.game.play_area.rect = {0, 0, param.game.width, 216}; param.game.play_area.rect = {0, 0, param.game.width, 216};
param.game.enter_name_seconds = 30; param.game.enter_name_seconds = 30;
@@ -137,16 +135,6 @@ bool setParams(const std::string &var, const std::string &value)
param.game.item_size = std::stoi(value); param.game.item_size = std::stoi(value);
} }
else if (var == "game.coffee_machine_w")
{
param.game.coffee_machine_w = std::stoi(value);
}
else if (var == "game.coffee_machine_h")
{
param.game.coffee_machine_h = std::stoi(value);
}
else if (var == "game.play_area.rect.x") else if (var == "game.play_area.rect.x")
{ {
param.game.play_area.rect.x = std::stoi(value); param.game.play_area.rect.x = std::stoi(value);

View File

@@ -12,8 +12,6 @@ struct ParamGame
float width; // Ancho de la resolución nativa del juego float width; // Ancho de la resolución nativa del juego
float height; // Alto de la resolución nativa del juego float height; // Alto de la resolución nativa del juego
float item_size; // Tamaño de los ítems del juego float item_size; // Tamaño de los ítems del juego
float coffee_machine_w; // Ancho de la máquina de café
float coffee_machine_h; // Alto de la máquina de café
Zone play_area; // Rectángulo con la posición de la zona de juego Zone play_area; // Rectángulo con la posición de la zona de juego
Zone game_area; // Rectángulo con las dimensiones del juego Zone game_area; // Rectángulo con las dimensiones del juego
int enter_name_seconds; // Duración en segundos para introducir el nombre al finalizar la partida int enter_name_seconds; // Duración en segundos para introducir el nombre al finalizar la partida

View File

@@ -598,7 +598,7 @@ void Game::checkBulletCollision()
} }
else else
{ {
createItem(dropped_item, player->getPosX(), param.game.game_area.rect.y - param.game.coffee_machine_h); createItem(dropped_item, player->getPosX(), param.game.game_area.rect.y - Item::COFFEE_MACHINE_HEIGHT);
coffee_machine_enabled_ = true; coffee_machine_enabled_ = true;
} }
} }
@@ -2026,7 +2026,7 @@ void Game::checkDebugEvents(const SDL_Event &event)
{ {
if (player->isPlaying()) if (player->isPlaying())
{ {
createItem(ItemType::COFFEE_MACHINE, player->getPosX(), param.game.game_area.rect.y - param.game.coffee_machine_h); createItem(ItemType::COFFEE_MACHINE, player->getPosX(), param.game.game_area.rect.y - Item::COFFEE_MACHINE_HEIGHT);
break; break;
} }
} }