millorat el spawn d'enemics: perimetre de seguretat i animació amb invulnerabilitat
This commit is contained in:
@@ -25,12 +25,13 @@ Enemic::Enemic(SDL_Renderer* renderer)
|
||||
tipus_(TipusEnemic::PENTAGON),
|
||||
tracking_timer_(0.0f),
|
||||
ship_position_(nullptr),
|
||||
tracking_strength_(0.5f) { // Default tracking strength
|
||||
tracking_strength_(0.5f), // Default tracking strength
|
||||
timer_invulnerabilitat_(0.0f) { // Start vulnerable
|
||||
// [NUEVO] Forma es carrega a inicialitzar() segons el tipus
|
||||
// Constructor no carrega forma per permetre tipus diferents
|
||||
}
|
||||
|
||||
void Enemic::inicialitzar(TipusEnemic tipus) {
|
||||
void Enemic::inicialitzar(TipusEnemic tipus, const Punt* ship_pos) {
|
||||
// Guardar tipus
|
||||
tipus_ = tipus;
|
||||
|
||||
@@ -68,7 +69,7 @@ void Enemic::inicialitzar(TipusEnemic tipus) {
|
||||
std::cerr << "[Enemic] Error: no s'ha pogut carregar " << shape_file << std::endl;
|
||||
}
|
||||
|
||||
// Posició aleatòria dins de l'àrea de joc
|
||||
// [MODIFIED] Posició aleatòria amb comprovació de seguretat
|
||||
float min_x, max_x, min_y, max_y;
|
||||
Constants::obtenir_limits_zona_segurs(Defaults::Entities::ENEMY_RADIUS,
|
||||
min_x,
|
||||
@@ -76,10 +77,38 @@ void Enemic::inicialitzar(TipusEnemic tipus) {
|
||||
min_y,
|
||||
max_y);
|
||||
|
||||
int range_x = static_cast<int>(max_x - min_x);
|
||||
int range_y = static_cast<int>(max_y - min_y);
|
||||
centre_.x = static_cast<float>((std::rand() % range_x) + static_cast<int>(min_x));
|
||||
centre_.y = static_cast<float>((std::rand() % range_y) + static_cast<int>(min_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, candidate_y;
|
||||
|
||||
if (intent_spawn_safe(*ship_pos, candidate_x, candidate_y)) {
|
||||
centre_.x = candidate_x;
|
||||
centre_.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);
|
||||
centre_.x = static_cast<float>((std::rand() % range_x) + static_cast<int>(min_x));
|
||||
centre_.y = static_cast<float>((std::rand() % range_y) + static_cast<int>(min_y));
|
||||
|
||||
std::cout << "[Enemic] Advertència: spawn sense zona segura després de "
|
||||
<< Defaults::Enemies::Spawn::MAX_SPAWN_ATTEMPTS << " intents" << std::endl;
|
||||
}
|
||||
} 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);
|
||||
centre_.x = static_cast<float>((std::rand() % range_x) + static_cast<int>(min_x));
|
||||
centre_.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;
|
||||
@@ -95,12 +124,34 @@ void Enemic::inicialitzar(TipusEnemic tipus) {
|
||||
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 Enemic::actualitzar(float delta_time) {
|
||||
if (esta_) {
|
||||
// [NEW] Update invulnerability timer and brightness
|
||||
if (timer_invulnerabilitat_ > 0.0f) {
|
||||
timer_invulnerabilitat_ -= delta_time;
|
||||
|
||||
if (timer_invulnerabilitat_ < 0.0f) {
|
||||
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);
|
||||
|
||||
@@ -114,8 +165,10 @@ void Enemic::actualitzar(float delta_time) {
|
||||
|
||||
void Enemic::dibuixar() const {
|
||||
if (esta_ && forma_) {
|
||||
// [NUEVO] Usar render_shape amb escala animada
|
||||
// Calculate animated scale (includes invulnerability LERP)
|
||||
float escala = calcular_escala_actual();
|
||||
|
||||
// brightness_ is already updated in actualitzar()
|
||||
Rendering::render_shape(renderer_, forma_, centre_, rotacio_, escala, true, 1.0f, brightness_);
|
||||
}
|
||||
}
|
||||
@@ -388,8 +441,21 @@ void Enemic::actualitzar_rotacio_accelerada(float delta_time) {
|
||||
float Enemic::calcular_escala_actual() const {
|
||||
float escala = 1.0f;
|
||||
|
||||
if (animacio_.palpitacio_activa) {
|
||||
// Add pulsating scale variation
|
||||
// [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;
|
||||
escala = START + (END - START) * smooth_t;
|
||||
} else if (animacio_.palpitacio_activa) {
|
||||
// [EXISTING] Palpitació només quan no invulnerable
|
||||
escala += animacio_.palpitacio_amplitud * std::sin(animacio_.palpitacio_fase);
|
||||
}
|
||||
|
||||
@@ -421,3 +487,28 @@ void Enemic::set_tracking_strength(float strength) {
|
||||
tracking_strength_ = strength;
|
||||
}
|
||||
}
|
||||
|
||||
// [NEW] Safe spawn helper - checks if position is away from ship
|
||||
bool Enemic::intent_spawn_safe(const Punt& ship_pos, float& out_x, float& out_y) {
|
||||
// Generate random position within safe bounds
|
||||
float min_x, max_x, min_y, 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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user