revert: vuelve al modelo de efd18ff + ENEMY_LIFETIME 3.0 -> 4.5
This commit is contained in:
1 parent
0c8a9b744e
commit
ae1d1397b1
5 files changed
+133
-170
No files matched your search
@@ -57,8 +57,7 @@ namespace Effects {
|
||||
SDL_Color color,
|
||||
float lifetime,
|
||||
float friction,
|
||||
int segment_multiplier,
|
||||
float shrink_rate) {
|
||||
int segment_multiplier) {
|
||||
if (!shape || !shape->isValid()) {
|
||||
return;
|
||||
}
|
||||
@@ -80,7 +79,7 @@ namespace Effects {
|
||||
Vec2 world_p2 = transformPoint(local_p2, shape_centre, centro, angle, scale);
|
||||
|
||||
// Si el pool es ple, no té sentit continuar amb la resta de segments
|
||||
if (!spawnDebris(world_p1, world_p2, centro, velocitat_base, brightness, velocitat_objecte, velocitat_angular, factor_herencia_visual, color, lifetime, friction, shrink_rate)) {
|
||||
if (!spawnDebris(world_p1, world_p2, centro, velocitat_base, brightness, velocitat_objecte, velocitat_angular, factor_herencia_visual, color, lifetime, friction)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -106,21 +105,14 @@ namespace Effects {
|
||||
return segments;
|
||||
}
|
||||
|
||||
auto DebrisManager::spawnDebris(const Vec2& world_p1, const Vec2& world_p2, const Vec2& centro, float velocitat_base, float brightness, const Vec2& velocitat_objecte, float velocitat_angular, float factor_herencia_visual, SDL_Color color, float lifetime, float friction, float shrink_rate) -> bool {
|
||||
auto DebrisManager::spawnDebris(const Vec2& world_p1, const Vec2& world_p2, const Vec2& centro, float velocitat_base, float brightness, const Vec2& velocitat_objecte, float velocitat_angular, float factor_herencia_visual, SDL_Color color, float lifetime, float friction) -> bool {
|
||||
Debris* debris = findFreeSlot();
|
||||
if (debris == nullptr) {
|
||||
std::cerr << "[DebrisManager] Warning: no debris slots disponibles\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
// Geometria autoritaritzada: centro + original_angle + original_half_length.
|
||||
// p1/p2 es reconstrueixen cada frame en update() des d'aquestes dades.
|
||||
const float DX = world_p2.x - world_p1.x;
|
||||
const float DY = world_p2.y - world_p1.y;
|
||||
debris->centro = {.x = (world_p1.x + world_p2.x) / 2.0F,
|
||||
.y = (world_p1.y + world_p2.y) / 2.0F};
|
||||
debris->original_angle = std::atan2(DY, DX);
|
||||
debris->original_half_length = std::sqrt((DX * DX) + (DY * DY)) / 2.0F;
|
||||
// Geometria
|
||||
debris->p1 = world_p1;
|
||||
debris->p2 = world_p2;
|
||||
|
||||
@@ -144,14 +136,11 @@ namespace Effects {
|
||||
|
||||
debris->angle_rotacio = 0.0F;
|
||||
|
||||
// Estat inicial: INTACTE durant `lifetime` segons (o fins que la
|
||||
// velocity baixi de SHRINK_SPEED_THRESHOLD). Després MENGUANT a
|
||||
// shrink_rate per segon fins arribar a size_factor=0 → mor.
|
||||
// Vida i shrinking — min_lifetime és el temps mínim garantit; després
|
||||
// el fragment mor quan |velocity| < MIN_SPEED_TO_DIE.
|
||||
debris->temps_vida = 0.0F;
|
||||
debris->intact_time = lifetime;
|
||||
debris->shrinking = false;
|
||||
debris->size_factor = 1.0F;
|
||||
debris->shrink_rate = shrink_rate;
|
||||
debris->min_lifetime = lifetime;
|
||||
debris->factor_shrink = Defaults::Physics::Debris::SHRINK_RATE;
|
||||
|
||||
// Visuals heretades
|
||||
debris->brightness = brightness;
|
||||
@@ -223,116 +212,109 @@ namespace Effects {
|
||||
}
|
||||
}
|
||||
|
||||
// INTACTE → MENGUANT → mort. Retorna true si el debris segueix viu.
|
||||
static auto updateLifecycle(Debris& debris, float delta_time) -> bool {
|
||||
debris.temps_vida += delta_time;
|
||||
|
||||
const float SPEED_SQ = (debris.velocity.x * debris.velocity.x) +
|
||||
(debris.velocity.y * debris.velocity.y);
|
||||
|
||||
if (!debris.shrinking) {
|
||||
const bool TIME_TRIGGER = debris.temps_vida >= debris.intact_time;
|
||||
const bool SPEED_TRIGGER = SPEED_SQ < Defaults::Physics::Debris::SHRINK_SPEED_THRESHOLD_SQ;
|
||||
if (TIME_TRIGGER || SPEED_TRIGGER) {
|
||||
debris.shrinking = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (debris.shrinking) {
|
||||
debris.size_factor -= debris.shrink_rate * delta_time;
|
||||
if (debris.size_factor <= 0.0F) {
|
||||
debris.active = false;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Fricció lineal: redueix |velocity| en acceleration per segon.
|
||||
static void applyLinearFriction(Debris& debris, float delta_time) {
|
||||
const float SPEED = std::sqrt((debris.velocity.x * debris.velocity.x) +
|
||||
(debris.velocity.y * debris.velocity.y));
|
||||
if (SPEED > 1.0F) {
|
||||
const float DIR_X = debris.velocity.x / SPEED;
|
||||
const float DIR_Y = debris.velocity.y / SPEED;
|
||||
const float NEW_SPEED = std::max(SPEED + (debris.acceleration * delta_time), 0.0F);
|
||||
debris.velocity.x = DIR_X * NEW_SPEED;
|
||||
debris.velocity.y = DIR_Y * NEW_SPEED;
|
||||
} else {
|
||||
debris.velocity.x = 0.0F;
|
||||
debris.velocity.y = 0.0F;
|
||||
}
|
||||
}
|
||||
|
||||
// Aplica fricció a una velocity angular (genèric per a trajectòria i visual).
|
||||
static void decayAngular(float& vel, float friction, float delta_time) {
|
||||
if (std::abs(vel) <= 0.01F) {
|
||||
vel = 0.0F;
|
||||
return;
|
||||
}
|
||||
const float SIGN = (vel > 0) ? 1.0F : -1.0F;
|
||||
vel -= SIGN * friction * delta_time;
|
||||
if ((vel > 0) != (SIGN > 0)) {
|
||||
vel = 0.0F;
|
||||
}
|
||||
}
|
||||
|
||||
// Rota el vector velocity (trajectòria corba) i aplica fricció a ambdues
|
||||
// velocitats angulars (trajectòria i visual).
|
||||
static void applyAngularDynamics(Debris& debris, float delta_time) {
|
||||
// Rotar vector de velocity amb matriu 2D (només si encara gira la trajectòria)
|
||||
if (std::abs(debris.velocitat_rot) > 0.01F) {
|
||||
const float DANGLE = debris.velocitat_rot * delta_time;
|
||||
const float VX = debris.velocity.x;
|
||||
const float VY = debris.velocity.y;
|
||||
const float COS_A = std::cos(DANGLE);
|
||||
const float SIN_A = std::sin(DANGLE);
|
||||
debris.velocity.x = (VX * COS_A) - (VY * SIN_A);
|
||||
debris.velocity.y = (VX * SIN_A) + (VY * COS_A);
|
||||
}
|
||||
|
||||
// Decay independent de les dues velocitats angulars.
|
||||
decayAngular(debris.velocitat_rot,
|
||||
Defaults::Physics::Debris::FRICCIO_ANGULAR,
|
||||
delta_time);
|
||||
decayAngular(debris.velocitat_rot_visual,
|
||||
Defaults::Physics::Debris::FRICCIO_VISUAL,
|
||||
delta_time);
|
||||
}
|
||||
|
||||
void DebrisManager::update(float delta_time) {
|
||||
for (auto& debris : debris_pool_) {
|
||||
if (!debris.active) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!updateLifecycle(debris, delta_time)) {
|
||||
continue;
|
||||
// 1. Actualitzar time de vida
|
||||
debris.temps_vida += delta_time;
|
||||
|
||||
// Política de mort: viu sí o sí durant min_lifetime; després mor
|
||||
// quan la velocity cau per sota d'un llindar. Així els fragments
|
||||
// ràpids no desapareixen en moviment.
|
||||
if (debris.temps_vida >= debris.min_lifetime) {
|
||||
const float SPEED_SQ = (debris.velocity.x * debris.velocity.x) +
|
||||
(debris.velocity.y * debris.velocity.y);
|
||||
if (SPEED_SQ < Defaults::Physics::Debris::MIN_SPEED_TO_DIE_SQ) {
|
||||
debris.active = false;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
applyLinearFriction(debris, delta_time);
|
||||
applyAngularDynamics(debris, delta_time);
|
||||
// 2. Actualitzar velocity (desacceleració)
|
||||
// Aplicar fricció en la direcció del movement
|
||||
float speed = std::sqrt((debris.velocity.x * debris.velocity.x) +
|
||||
(debris.velocity.y * debris.velocity.y));
|
||||
|
||||
// Posició del centre: integra velocity (separat de p1/p2).
|
||||
debris.centro.x += debris.velocity.x * delta_time;
|
||||
debris.centro.y += debris.velocity.y * delta_time;
|
||||
if (speed > 1.0F) {
|
||||
// Calcular direcció normalitzada
|
||||
float dir_x = debris.velocity.x / speed;
|
||||
float dir_y = debris.velocity.y / speed;
|
||||
|
||||
// Acumular rotació visual. Modulada per size_factor: durant
|
||||
// INTACTE (size=1) no afecta, però quan el segment mengua la
|
||||
// rotació també s'apaga.
|
||||
debris.angle_rotacio += debris.velocitat_rot_visual * debris.size_factor * delta_time;
|
||||
// Aplicar aceleración negativa (fricció)
|
||||
float nova_speed = speed + (debris.acceleration * delta_time);
|
||||
nova_speed = std::max(nova_speed, 0.0F);
|
||||
|
||||
// Reconstruir p1/p2 des de la geometria autoritaritzada:
|
||||
// centro + (cos/sin(original_angle + angle_rotacio)) × original_half_length × size_factor
|
||||
// No iteratiu — evita la rotació quadràtica i el shrink exponencial.
|
||||
const float CURRENT_ANGLE = debris.original_angle + debris.angle_rotacio;
|
||||
const float HALF_LEN = debris.original_half_length * std::max(0.0F, debris.size_factor);
|
||||
const float COS_A = std::cos(CURRENT_ANGLE);
|
||||
const float SIN_A = std::sin(CURRENT_ANGLE);
|
||||
debris.p1.x = debris.centro.x - (HALF_LEN * COS_A);
|
||||
debris.p1.y = debris.centro.y - (HALF_LEN * SIN_A);
|
||||
debris.p2.x = debris.centro.x + (HALF_LEN * COS_A);
|
||||
debris.p2.y = debris.centro.y + (HALF_LEN * SIN_A);
|
||||
debris.velocity.x = dir_x * nova_speed;
|
||||
debris.velocity.y = dir_y * nova_speed;
|
||||
} else {
|
||||
// Velocidad mucho baixa, aturar
|
||||
debris.velocity.x = 0.0F;
|
||||
debris.velocity.y = 0.0F;
|
||||
}
|
||||
|
||||
// 2b. Rotar vector de velocity (trayectoria curva)
|
||||
if (std::abs(debris.velocitat_rot) > 0.01F) {
|
||||
// Calcular angle de rotación este frame
|
||||
float dangle = debris.velocitat_rot * delta_time;
|
||||
|
||||
// Rotar vector de velocity usant matriu de rotación 2D
|
||||
float vel_x_old = debris.velocity.x;
|
||||
float vel_y_old = debris.velocity.y;
|
||||
|
||||
float cos_a = std::cos(dangle);
|
||||
float sin_a = std::sin(dangle);
|
||||
|
||||
debris.velocity.x = (vel_x_old * cos_a) - (vel_y_old * sin_a);
|
||||
debris.velocity.y = (vel_x_old * sin_a) + (vel_y_old * cos_a);
|
||||
}
|
||||
|
||||
// 2c. Aplicar fricció angular (desacceleració gradual)
|
||||
if (std::abs(debris.velocitat_rot) > 0.01F) {
|
||||
float sign = (debris.velocitat_rot > 0) ? 1.0F : -1.0F;
|
||||
float reduccion =
|
||||
Defaults::Physics::Debris::FRICCIO_ANGULAR * delta_time;
|
||||
debris.velocitat_rot -= sign * reduccion;
|
||||
|
||||
// Evitar canvi de signe (no pot passar de CW a CCW)
|
||||
if ((debris.velocitat_rot > 0) != (sign > 0)) {
|
||||
debris.velocitat_rot = 0.0F;
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Calcular centro del segment
|
||||
Vec2 centro = {.x = (debris.p1.x + debris.p2.x) / 2.0F,
|
||||
.y = (debris.p1.y + debris.p2.y) / 2.0F};
|
||||
|
||||
// 4. Actualitzar posición del centro
|
||||
centro.x += debris.velocity.x * delta_time;
|
||||
centro.y += debris.velocity.y * delta_time;
|
||||
|
||||
// 5. Actualitzar rotación VISUAL
|
||||
debris.angle_rotacio += debris.velocitat_rot_visual * delta_time;
|
||||
|
||||
// 6. Aplicar shrinking (reducció de distancia entre points).
|
||||
// El shrink es normalitza al min_lifetime (capat a 1.0) perquè els
|
||||
// fragments que viuen més no es continuïn fent més petits per sempre.
|
||||
const float SHRINK_T = std::min(debris.temps_vida / debris.min_lifetime, 1.0F);
|
||||
float shrink_factor = 1.0F - (debris.factor_shrink * SHRINK_T);
|
||||
shrink_factor = std::max(0.0F, shrink_factor); // No negatiu
|
||||
|
||||
// Calcular distancia original entre points
|
||||
float dx = debris.p2.x - debris.p1.x;
|
||||
float dy = debris.p2.y - debris.p1.y;
|
||||
|
||||
// 7. Reconstruir segment con nueva mida i rotación
|
||||
float half_length = std::sqrt((dx * dx) + (dy * dy)) * shrink_factor / 2.0F;
|
||||
float original_angle = std::atan2(dy, dx);
|
||||
float new_angle = original_angle + debris.angle_rotacio;
|
||||
|
||||
debris.p1.x = centro.x - (half_length * std::cos(new_angle));
|
||||
debris.p1.y = centro.y - (half_length * std::sin(new_angle));
|
||||
debris.p2.x = centro.x + (half_length * std::cos(new_angle));
|
||||
debris.p2.y = centro.y + (half_length * std::sin(new_angle));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user