7ee359b910
Sweep final del naming a CamelCase/camelBack/lower_case:
Fitxers renombrats:
- effects/gestor_puntuacio_flotant.{hpp,cpp} -> floating_score_manager.{hpp,cpp}
- effects/puntuacio_flotant.hpp -> floating_score.hpp
Tipus (CamelCase):
- GestorPuntuacioFlotant -> FloatingScoreManager
- PuntuacioFlotant -> FloatingScore
- ConfigStage -> StageConfig
- ConfigSistemaStages -> StageSystemConfig
- NauTitol -> TitleShip
- EstatNau -> ShipState
Metodes publics (camelBack):
- obte_renderer -> getRenderer
- get_num_actius -> getActiveCount
- calcular_direccio_explosio -> computeExplosionDirection
- trobar_slot_lliure -> findFreeSlot
- explotar -> explode
- reiniciar -> reset
- es_valida -> isValid
- parsejar_fitxer -> parseFile
- carregar -> load
- crear_explosio -> createExplosion
- registrar_puntuacio -> registerScore
- construir_marcador -> buildScoreboard
- render_centered -> renderCentered
Camps struct publics (snake_case):
- actiu/actius -> active
- rotacio -> rotation, rotacio_visual -> visual_rotation
- acceleracio -> acceleration
- velocitat -> velocity
- escala/escala_inicial/objectiu/actual -> scale/initial_scale/...
- posicio/posicio_inicial/objectiu/actual -> position/initial_position/...
- fase_oscilacio -> oscillation_phase
- temps_estat -> state_time
- jugador_id -> player_id
- estat -> state
- brillantor -> brightness
- tipus -> type
Camps privats (sufix _):
- naus_ -> ships_, orni_ -> enemies_, bales_ -> bullets_
- gestor_puntuacio_ -> floating_score_manager_
- punt_mort_ -> death_position_, punt_spawn_ -> spawn_position_
- itocado_per_jugador_ -> hit_timer_per_player_
- vides_per_jugador_ -> lives_per_player_
- puntuacio_per_jugador_ -> score_per_player_
- estat_game_over_ -> game_over_state_
- continues_usados_ -> continues_used_
Constants:
- MARGE_ESQ/DRET/DALT/BAIX -> MARGIN_LEFT/RIGHT/TOP/BOTTOM
Variables locals i parametres comuns (snake_case):
- nau -> ship, enemic -> enemy, bala -> bullet
- forma -> shape, punt(s) -> point(s)
- jugador -> player, partida -> match
- temps -> time, missatge -> message
Diff: 59 fitxers, +1000/-1000 (simetric). Compila i enllaça.
Pendents per a futures fases (no bloquejants):
- Comentaris de capçalera en catala -> castella
- Variables locals/parametres minoritaris en catala
- Include guards (queden alguns #ifndef en lloc de #pragma once)
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
552 lines
20 KiB
C++
552 lines
20 KiB
C++
// enemy.cpp - Implementació d'enemics (ORNIs)
|
|
// © 1999 Visente i Sergi (versió Pascal)
|
|
// © 2025 Port a C++20 amb SDL3
|
|
|
|
#include "game/entities/enemy.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <cstdlib>
|
|
#include <iostream>
|
|
|
|
#include "core/defaults.hpp"
|
|
#include "core/entities/entity.hpp"
|
|
#include "core/graphics/shape_loader.hpp"
|
|
#include "core/rendering/shape_renderer.hpp"
|
|
#include "core/types.hpp"
|
|
#include "game/constants.hpp"
|
|
|
|
Enemy::Enemy(SDL_Renderer* renderer)
|
|
: Entity(renderer),
|
|
velocity_(0.0F),
|
|
drotacio_(0.0F),
|
|
rotacio_(0.0F),
|
|
esta_(false),
|
|
type_(EnemyType::PENTAGON),
|
|
tracking_timer_(0.0F),
|
|
ship_position_(nullptr),
|
|
tracking_strength_(0.5F), // Default tracking strength
|
|
timer_invulnerabilitat_(0.0F) { // Start vulnerable
|
|
// [NUEVO] Brightness específic per enemics
|
|
brightness_ = Defaults::Brightness::ENEMIC;
|
|
|
|
// [NUEVO] Forma es carrega a init() segons el type
|
|
// Constructor no carrega shape per permetre type diferents
|
|
}
|
|
|
|
void Enemy::init(EnemyType type, const Vec2* ship_pos) {
|
|
// Guardar type
|
|
type_ = type;
|
|
|
|
// Carregar shape segons el type
|
|
const char* shape_file;
|
|
float drotacio_min;
|
|
float drotacio_max;
|
|
|
|
switch (type_) {
|
|
case EnemyType::PENTAGON:
|
|
shape_file = Defaults::Enemies::Pentagon::SHAPE_FILE;
|
|
velocity_ = Defaults::Enemies::Pentagon::VELOCITAT;
|
|
drotacio_min = Defaults::Enemies::Pentagon::DROTACIO_MIN;
|
|
drotacio_max = Defaults::Enemies::Pentagon::DROTACIO_MAX;
|
|
break;
|
|
|
|
case EnemyType::QUADRAT:
|
|
shape_file = Defaults::Enemies::Quadrat::SHAPE_FILE;
|
|
velocity_ = Defaults::Enemies::Quadrat::VELOCITAT;
|
|
drotacio_min = Defaults::Enemies::Quadrat::DROTACIO_MIN;
|
|
drotacio_max = Defaults::Enemies::Quadrat::DROTACIO_MAX;
|
|
tracking_timer_ = 0.0F;
|
|
break;
|
|
|
|
case EnemyType::MOLINILLO:
|
|
shape_file = Defaults::Enemies::Molinillo::SHAPE_FILE;
|
|
velocity_ = Defaults::Enemies::Molinillo::VELOCITAT;
|
|
drotacio_min = Defaults::Enemies::Molinillo::DROTACIO_MIN;
|
|
drotacio_max = Defaults::Enemies::Molinillo::DROTACIO_MAX;
|
|
break;
|
|
|
|
default:
|
|
// Fallback segur: usar valors de PENTAGON
|
|
std::cerr << "[Enemy] Error: type desconegut ("
|
|
<< static_cast<int>(type_) << "), utilitzant PENTAGON\n";
|
|
shape_file = Defaults::Enemies::Pentagon::SHAPE_FILE;
|
|
velocity_ = Defaults::Enemies::Pentagon::VELOCITAT;
|
|
drotacio_min = Defaults::Enemies::Pentagon::DROTACIO_MIN;
|
|
drotacio_max = Defaults::Enemies::Pentagon::DROTACIO_MAX;
|
|
break;
|
|
}
|
|
|
|
// Carregar shape
|
|
shape_ = Graphics::ShapeLoader::load(shape_file);
|
|
if (!shape_ || !shape_->isValid()) {
|
|
std::cerr << "[Enemy] Error: no s'ha pogut load " << shape_file << '\n';
|
|
}
|
|
|
|
// [MODIFIED] Posició aleatòria amb comprovació de seguretat
|
|
float min_x;
|
|
float max_x;
|
|
float min_y;
|
|
float max_y;
|
|
Constants::obtenir_limits_zona_segurs(Defaults::Entities::ENEMY_RADIUS,
|
|
min_x,
|
|
max_x,
|
|
min_y,
|
|
max_y);
|
|
|
|
if (ship_pos != nullptr) {
|
|
// [NEW] Safe spawn: attempt to find position away from ship
|
|
bool found_safe_position = false;
|
|
|
|
for (int attempt = 0; attempt < Defaults::Enemies::Spawn::MAX_SPAWN_ATTEMPTS; attempt++) {
|
|
float candidate_x;
|
|
float candidate_y;
|
|
|
|
if (intent_spawn_safe(*ship_pos, candidate_x, candidate_y)) {
|
|
center_.x = candidate_x;
|
|
center_.y = candidate_y;
|
|
found_safe_position = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!found_safe_position) {
|
|
// Fallback: spawn anywhere (user's preference)
|
|
int range_x = static_cast<int>(max_x - min_x);
|
|
int range_y = static_cast<int>(max_y - min_y);
|
|
center_.x = static_cast<float>((std::rand() % range_x) + static_cast<int>(min_x));
|
|
center_.y = static_cast<float>((std::rand() % range_y) + static_cast<int>(min_y));
|
|
|
|
std::cout << "[Enemy] Advertència: spawn sense zona segura després de "
|
|
<< Defaults::Enemies::Spawn::MAX_SPAWN_ATTEMPTS << " intents" << '\n';
|
|
}
|
|
} else {
|
|
// [EXISTING] No ship position: spawn anywhere (backward compatibility)
|
|
int range_x = static_cast<int>(max_x - min_x);
|
|
int range_y = static_cast<int>(max_y - min_y);
|
|
center_.x = static_cast<float>((std::rand() % range_x) + static_cast<int>(min_x));
|
|
center_.y = static_cast<float>((std::rand() % range_y) + static_cast<int>(min_y));
|
|
}
|
|
|
|
// Angle aleatori de moviment
|
|
angle_ = (std::rand() % 360) * Constants::PI / 180.0F;
|
|
|
|
// Rotació visual aleatòria (rad/s) dins del rang del type
|
|
float drotacio_range = drotacio_max - drotacio_min;
|
|
drotacio_ = drotacio_min + ((static_cast<float>(std::rand()) / RAND_MAX) * drotacio_range);
|
|
rotacio_ = 0.0F;
|
|
|
|
// Inicialitzar state d'animació
|
|
animacio_ = EnemyAnimation(); // Reset to defaults
|
|
animacio_.drotacio_base = drotacio_;
|
|
animacio_.drotacio_objetivo = drotacio_;
|
|
animacio_.drotacio_t = 1.0F; // Start without interpolating
|
|
|
|
// [NEW] Inicialitzar invulnerabilitat
|
|
timer_invulnerabilitat_ = Defaults::Enemies::Spawn::INVULNERABILITY_DURATION; // 3.0s
|
|
brightness_ = Defaults::Enemies::Spawn::INVULNERABILITY_BRIGHTNESS_START; // 0.3f
|
|
|
|
// Activar
|
|
esta_ = true;
|
|
}
|
|
|
|
void Enemy::update(float delta_time) {
|
|
if (esta_) {
|
|
// [NEW] Update invulnerability timer and brightness
|
|
if (timer_invulnerabilitat_ > 0.0F) {
|
|
timer_invulnerabilitat_ -= delta_time;
|
|
|
|
timer_invulnerabilitat_ = std::max(timer_invulnerabilitat_, 0.0F);
|
|
|
|
// [NEW] Update brightness with LERP during invulnerability
|
|
float t_inv = timer_invulnerabilitat_ / Defaults::Enemies::Spawn::INVULNERABILITY_DURATION;
|
|
float t = 1.0F - t_inv; // 0.0 → 1.0
|
|
float smooth_t = t * t * (3.0F - 2.0F * t); // smoothstep
|
|
|
|
constexpr float START = Defaults::Enemies::Spawn::INVULNERABILITY_BRIGHTNESS_START;
|
|
constexpr float END = Defaults::Enemies::Spawn::INVULNERABILITY_BRIGHTNESS_END;
|
|
brightness_ = START + ((END - START) * smooth_t);
|
|
}
|
|
|
|
// Moviment autònom
|
|
mou(delta_time);
|
|
|
|
// Actualitzar animacions (palpitació, rotació accelerada)
|
|
actualitzar_animacio(delta_time);
|
|
|
|
// Rotació visual (time-based: drotacio_ està en rad/s)
|
|
rotacio_ += drotacio_ * delta_time;
|
|
}
|
|
}
|
|
|
|
void Enemy::draw() const {
|
|
if (esta_ && shape_) {
|
|
// Calculate animated scale (includes invulnerability LERP)
|
|
float scale = calcular_escala_actual();
|
|
|
|
// brightness_ is already updated in update()
|
|
Rendering::render_shape(renderer_, shape_, center_, rotacio_, scale, 1.0F, brightness_);
|
|
}
|
|
}
|
|
|
|
void Enemy::mou(float delta_time) {
|
|
// Dispatcher: crida el comportament específic segons el type
|
|
switch (type_) {
|
|
case EnemyType::PENTAGON:
|
|
comportament_pentagon(delta_time);
|
|
break;
|
|
case EnemyType::QUADRAT:
|
|
comportament_quadrat(delta_time);
|
|
break;
|
|
case EnemyType::MOLINILLO:
|
|
comportament_molinillo(delta_time);
|
|
break;
|
|
default:
|
|
// Fallback: comportament bàsic (Pentagon)
|
|
comportament_pentagon(delta_time);
|
|
break;
|
|
}
|
|
}
|
|
|
|
void Enemy::comportament_pentagon(float delta_time) {
|
|
// Pentagon: zigzag esquivador (frequent direction changes)
|
|
// Similar a comportament original però amb probabilitat més alta
|
|
|
|
float velocitat_efectiva = velocity_ * delta_time;
|
|
|
|
// Calcular desplaçament (angle-PI/2 perquè angle=0 apunta amunt)
|
|
float dy = velocitat_efectiva * std::sin(angle_ - (Constants::PI / 2.0F));
|
|
float dx = velocitat_efectiva * std::cos(angle_ - (Constants::PI / 2.0F));
|
|
|
|
float new_y = center_.y + dy;
|
|
float new_x = center_.x + dx;
|
|
|
|
// Obtenir límits segurs
|
|
float min_x;
|
|
float max_x;
|
|
float min_y;
|
|
float max_y;
|
|
Constants::obtenir_limits_zona_segurs(Defaults::Entities::ENEMY_RADIUS,
|
|
min_x,
|
|
max_x,
|
|
min_y,
|
|
max_y);
|
|
|
|
// Zigzag: canvi d'angle més freqüent en tocar límits
|
|
if (new_y >= min_y && new_y <= max_y) {
|
|
center_.y = new_y;
|
|
} else {
|
|
// Probabilitat més alta de canvi d'angle
|
|
if (static_cast<float>(std::rand()) / RAND_MAX < Defaults::Enemies::Pentagon::CANVI_ANGLE_PROB) {
|
|
float rand_angle = (static_cast<float>(std::rand()) / RAND_MAX) *
|
|
Defaults::Enemies::Pentagon::CANVI_ANGLE_MAX;
|
|
angle_ += (std::rand() % 2 == 0) ? rand_angle : -rand_angle;
|
|
}
|
|
}
|
|
|
|
if (new_x >= min_x && new_x <= max_x) {
|
|
center_.x = new_x;
|
|
} else {
|
|
if (static_cast<float>(std::rand()) / RAND_MAX < Defaults::Enemies::Pentagon::CANVI_ANGLE_PROB) {
|
|
float rand_angle = (static_cast<float>(std::rand()) / RAND_MAX) *
|
|
Defaults::Enemies::Pentagon::CANVI_ANGLE_MAX;
|
|
angle_ += (std::rand() % 2 == 0) ? rand_angle : -rand_angle;
|
|
}
|
|
}
|
|
}
|
|
|
|
void Enemy::comportament_quadrat(float delta_time) {
|
|
// Quadrat: perseguidor (tracks player position)
|
|
|
|
// Update tracking timer
|
|
tracking_timer_ += delta_time;
|
|
|
|
// Periodically update angle toward ship
|
|
if (tracking_timer_ >= Defaults::Enemies::Quadrat::TRACKING_INTERVAL) {
|
|
tracking_timer_ = 0.0F;
|
|
|
|
if (ship_position_ != nullptr) {
|
|
// Calculate angle to ship
|
|
float dx = ship_position_->x - center_.x;
|
|
float dy = ship_position_->y - center_.y;
|
|
float target_angle = std::atan2(dy, dx) + (Constants::PI / 2.0F);
|
|
|
|
// Interpolate toward target angle
|
|
float angle_diff = target_angle - angle_;
|
|
|
|
// Normalize angle difference to [-π, π]
|
|
while (angle_diff > Constants::PI) {
|
|
angle_diff -= 2.0F * Constants::PI;
|
|
}
|
|
while (angle_diff < -Constants::PI) {
|
|
angle_diff += 2.0F * Constants::PI;
|
|
}
|
|
|
|
// Apply tracking strength (uses member variable, defaults to 0.5)
|
|
angle_ += angle_diff * tracking_strength_;
|
|
}
|
|
}
|
|
|
|
// Move in current direction
|
|
float velocitat_efectiva = velocity_ * delta_time;
|
|
float dy = velocitat_efectiva * std::sin(angle_ - (Constants::PI / 2.0F));
|
|
float dx = velocitat_efectiva * std::cos(angle_ - (Constants::PI / 2.0F));
|
|
|
|
float new_y = center_.y + dy;
|
|
float new_x = center_.x + dx;
|
|
|
|
// Obtenir límits segurs
|
|
float min_x;
|
|
float max_x;
|
|
float min_y;
|
|
float max_y;
|
|
Constants::obtenir_limits_zona_segurs(Defaults::Entities::ENEMY_RADIUS,
|
|
min_x,
|
|
max_x,
|
|
min_y,
|
|
max_y);
|
|
|
|
// Bounce on walls (simple reflection)
|
|
if (new_y >= min_y && new_y <= max_y) {
|
|
center_.y = new_y;
|
|
} else {
|
|
angle_ = -angle_; // Vertical reflection
|
|
}
|
|
|
|
if (new_x >= min_x && new_x <= max_x) {
|
|
center_.x = new_x;
|
|
} else {
|
|
angle_ = Constants::PI - angle_; // Horizontal reflection
|
|
}
|
|
}
|
|
|
|
void Enemy::comportament_molinillo(float delta_time) {
|
|
// Molinillo: agressiu (fast, straight lines, proximity spin-up)
|
|
|
|
// Check proximity to ship for spin-up effect
|
|
if (ship_position_ != nullptr) {
|
|
float dx = ship_position_->x - center_.x;
|
|
float dy = ship_position_->y - center_.y;
|
|
float distance = std::sqrt((dx * dx) + (dy * dy));
|
|
|
|
if (distance < Defaults::Enemies::Molinillo::PROXIMITY_DISTANCE) {
|
|
// Temporarily boost rotation speed when near ship
|
|
float boost = Defaults::Enemies::Molinillo::DROTACIO_PROXIMITY_MULTIPLIER;
|
|
drotacio_ = animacio_.drotacio_base * boost;
|
|
} else {
|
|
// Normal rotation speed
|
|
drotacio_ = animacio_.drotacio_base;
|
|
}
|
|
}
|
|
|
|
// Fast straight-line movement
|
|
float velocitat_efectiva = velocity_ * delta_time;
|
|
float dy = velocitat_efectiva * std::sin(angle_ - (Constants::PI / 2.0F));
|
|
float dx = velocitat_efectiva * std::cos(angle_ - (Constants::PI / 2.0F));
|
|
|
|
float new_y = center_.y + dy;
|
|
float new_x = center_.x + dx;
|
|
|
|
// Obtenir límits segurs
|
|
float min_x;
|
|
float max_x;
|
|
float min_y;
|
|
float max_y;
|
|
Constants::obtenir_limits_zona_segurs(Defaults::Entities::ENEMY_RADIUS,
|
|
min_x,
|
|
max_x,
|
|
min_y,
|
|
max_y);
|
|
|
|
// Rare angle changes on wall hits
|
|
if (new_y >= min_y && new_y <= max_y) {
|
|
center_.y = new_y;
|
|
} else {
|
|
if (static_cast<float>(std::rand()) / RAND_MAX < Defaults::Enemies::Molinillo::CANVI_ANGLE_PROB) {
|
|
float rand_angle = (static_cast<float>(std::rand()) / RAND_MAX) *
|
|
Defaults::Enemies::Molinillo::CANVI_ANGLE_MAX;
|
|
angle_ += (std::rand() % 2 == 0) ? rand_angle : -rand_angle;
|
|
}
|
|
}
|
|
|
|
if (new_x >= min_x && new_x <= max_x) {
|
|
center_.x = new_x;
|
|
} else {
|
|
if (static_cast<float>(std::rand()) / RAND_MAX < Defaults::Enemies::Molinillo::CANVI_ANGLE_PROB) {
|
|
float rand_angle = (static_cast<float>(std::rand()) / RAND_MAX) *
|
|
Defaults::Enemies::Molinillo::CANVI_ANGLE_MAX;
|
|
angle_ += (std::rand() % 2 == 0) ? rand_angle : -rand_angle;
|
|
}
|
|
}
|
|
}
|
|
|
|
void Enemy::actualitzar_animacio(float delta_time) {
|
|
actualitzar_palpitacio(delta_time);
|
|
actualitzar_rotacio_accelerada(delta_time);
|
|
}
|
|
|
|
void Enemy::actualitzar_palpitacio(float delta_time) {
|
|
if (animacio_.palpitacio_activa) {
|
|
// Advance phase (2π * frequency * dt)
|
|
animacio_.palpitacio_fase += 2.0F * Constants::PI * animacio_.palpitacio_frequencia * delta_time;
|
|
|
|
// Decrement timer
|
|
animacio_.palpitacio_temps_restant -= delta_time;
|
|
|
|
// Deactivate when timer expires
|
|
if (animacio_.palpitacio_temps_restant <= 0.0F) {
|
|
animacio_.palpitacio_activa = false;
|
|
}
|
|
} else {
|
|
// Random trigger (probability per second)
|
|
float rand_val = static_cast<float>(std::rand()) / RAND_MAX;
|
|
float trigger_prob = Defaults::Enemies::Animation::PALPITACIO_TRIGGER_PROB * delta_time;
|
|
|
|
if (rand_val < trigger_prob) {
|
|
// Activate palpitation
|
|
animacio_.palpitacio_activa = true;
|
|
animacio_.palpitacio_fase = 0.0F;
|
|
|
|
// Randomize parameters
|
|
float freq_range = Defaults::Enemies::Animation::PALPITACIO_FREQ_MAX -
|
|
Defaults::Enemies::Animation::PALPITACIO_FREQ_MIN;
|
|
animacio_.palpitacio_frequencia = Defaults::Enemies::Animation::PALPITACIO_FREQ_MIN +
|
|
((static_cast<float>(std::rand()) / RAND_MAX) * freq_range);
|
|
|
|
float amp_range = Defaults::Enemies::Animation::PALPITACIO_AMPLITUD_MAX -
|
|
Defaults::Enemies::Animation::PALPITACIO_AMPLITUD_MIN;
|
|
animacio_.palpitacio_amplitud = Defaults::Enemies::Animation::PALPITACIO_AMPLITUD_MIN +
|
|
((static_cast<float>(std::rand()) / RAND_MAX) * amp_range);
|
|
|
|
float dur_range = Defaults::Enemies::Animation::PALPITACIO_DURACIO_MAX -
|
|
Defaults::Enemies::Animation::PALPITACIO_DURACIO_MIN;
|
|
animacio_.palpitacio_temps_restant = Defaults::Enemies::Animation::PALPITACIO_DURACIO_MIN +
|
|
((static_cast<float>(std::rand()) / RAND_MAX) * dur_range);
|
|
}
|
|
}
|
|
}
|
|
|
|
void Enemy::actualitzar_rotacio_accelerada(float delta_time) {
|
|
if (animacio_.drotacio_t < 1.0F) {
|
|
// Transitioning to new target
|
|
animacio_.drotacio_t += delta_time / animacio_.drotacio_duracio;
|
|
|
|
if (animacio_.drotacio_t >= 1.0F) {
|
|
animacio_.drotacio_t = 1.0F;
|
|
animacio_.drotacio_base = animacio_.drotacio_objetivo; // Reached target
|
|
drotacio_ = animacio_.drotacio_base;
|
|
} else {
|
|
// Smoothstep interpolation: t² * (3 - 2t)
|
|
float t = animacio_.drotacio_t;
|
|
float smooth_t = t * t * (3.0F - 2.0F * t);
|
|
|
|
// Interpolate between base and target
|
|
float initial = animacio_.drotacio_base;
|
|
float target = animacio_.drotacio_objetivo;
|
|
drotacio_ = initial + ((target - initial) * smooth_t);
|
|
}
|
|
} else {
|
|
// Random trigger for new acceleration
|
|
float rand_val = static_cast<float>(std::rand()) / RAND_MAX;
|
|
float trigger_prob = Defaults::Enemies::Animation::ROTACIO_ACCEL_TRIGGER_PROB * delta_time;
|
|
|
|
if (rand_val < trigger_prob) {
|
|
// Start new transition
|
|
animacio_.drotacio_t = 0.0F;
|
|
|
|
// Randomize target speed (multiplier * base)
|
|
float mult_range = Defaults::Enemies::Animation::ROTACIO_ACCEL_MULTIPLIER_MAX -
|
|
Defaults::Enemies::Animation::ROTACIO_ACCEL_MULTIPLIER_MIN;
|
|
float multiplier = Defaults::Enemies::Animation::ROTACIO_ACCEL_MULTIPLIER_MIN +
|
|
((static_cast<float>(std::rand()) / RAND_MAX) * mult_range);
|
|
|
|
animacio_.drotacio_objetivo = animacio_.drotacio_base * multiplier;
|
|
|
|
// Randomize duration
|
|
float dur_range = Defaults::Enemies::Animation::ROTACIO_ACCEL_DURACIO_MAX -
|
|
Defaults::Enemies::Animation::ROTACIO_ACCEL_DURACIO_MIN;
|
|
animacio_.drotacio_duracio = Defaults::Enemies::Animation::ROTACIO_ACCEL_DURACIO_MIN +
|
|
((static_cast<float>(std::rand()) / RAND_MAX) * dur_range);
|
|
}
|
|
}
|
|
}
|
|
|
|
float Enemy::calcular_escala_actual() const {
|
|
float scale = 1.0F;
|
|
|
|
// [NEW] Invulnerability LERP prioritza sobre palpitació
|
|
if (timer_invulnerabilitat_ > 0.0F) {
|
|
// Calculate t: 0.0 at spawn → 1.0 at end
|
|
float t_inv = timer_invulnerabilitat_ / Defaults::Enemies::Spawn::INVULNERABILITY_DURATION;
|
|
float t = 1.0F - t_inv; // 0.0 → 1.0
|
|
|
|
// Apply smoothstep: t² * (3 - 2t)
|
|
float smooth_t = t * t * (3.0F - 2.0F * t);
|
|
|
|
// LERP scale from 0.0 to 1.0
|
|
constexpr float START = Defaults::Enemies::Spawn::INVULNERABILITY_SCALE_START;
|
|
constexpr float END = Defaults::Enemies::Spawn::INVULNERABILITY_SCALE_END;
|
|
scale = START + ((END - START) * smooth_t);
|
|
} else if (animacio_.palpitacio_activa) {
|
|
// [EXISTING] Palpitació només quan no invulnerable
|
|
scale += animacio_.palpitacio_amplitud * std::sin(animacio_.palpitacio_fase);
|
|
}
|
|
|
|
return scale;
|
|
}
|
|
|
|
// [NEW] Stage system API implementations
|
|
|
|
float Enemy::get_base_velocity() const {
|
|
switch (type_) {
|
|
case EnemyType::PENTAGON:
|
|
return Defaults::Enemies::Pentagon::VELOCITAT;
|
|
case EnemyType::QUADRAT:
|
|
return Defaults::Enemies::Quadrat::VELOCITAT;
|
|
case EnemyType::MOLINILLO:
|
|
return Defaults::Enemies::Molinillo::VELOCITAT;
|
|
default:
|
|
return Defaults::Enemies::Pentagon::VELOCITAT; // Fallback segur
|
|
}
|
|
}
|
|
|
|
float Enemy::get_base_rotation() const {
|
|
// Return the base rotation speed (drotacio_base if available, otherwise current drotacio_)
|
|
return animacio_.drotacio_base != 0.0F ? animacio_.drotacio_base : drotacio_;
|
|
}
|
|
|
|
void Enemy::set_tracking_strength(float strength) {
|
|
// Only applies to QUADRAT type
|
|
if (type_ == EnemyType::QUADRAT) {
|
|
tracking_strength_ = strength;
|
|
}
|
|
}
|
|
|
|
// [NEW] Safe spawn helper - checks if position is away from ship
|
|
bool Enemy::intent_spawn_safe(const Vec2& ship_pos, float& out_x, float& out_y) {
|
|
// Generate random position within safe bounds
|
|
float min_x;
|
|
float max_x;
|
|
float min_y;
|
|
float max_y;
|
|
Constants::obtenir_limits_zona_segurs(Defaults::Entities::ENEMY_RADIUS,
|
|
min_x,
|
|
max_x,
|
|
min_y,
|
|
max_y);
|
|
|
|
int range_x = static_cast<int>(max_x - min_x);
|
|
int range_y = static_cast<int>(max_y - min_y);
|
|
|
|
out_x = static_cast<float>((std::rand() % range_x) + static_cast<int>(min_x));
|
|
out_y = static_cast<float>((std::rand() % range_y) + static_cast<int>(min_y));
|
|
|
|
// Check Euclidean distance to ship
|
|
float dx = out_x - ship_pos.x;
|
|
float dy = out_y - ship_pos.y;
|
|
float distancia = std::sqrt((dx * dx) + (dy * dy));
|
|
|
|
// Return true if position is safe (>= 36px from ship)
|
|
return distancia >= Defaults::Enemies::Spawn::SAFETY_DISTANCE;
|
|
}
|