Implement directional influence (#43)
CI / build-and-test (push) Successful in 1m9s

Reviewed-on: #43
Co-authored-by: Álex Román Núñez <eirexe@eirteam.moe>
This commit was merged in pull request #43.
This commit is contained in:
2026-09-18 20:43:41 +00:00
committed by EIREXE
parent 2b5af49e15
commit 9f51207f48
8 changed files with 116 additions and 20 deletions
@@ -0,0 +1,69 @@
use fixed::traits::ToFixed;
use fixed_math::SinCos;
use crate::math::{
int::{FGWide, FGi32},
vec::FGVec2,
};
/// Applies DI to a given attack direction
pub fn apply_directional_influence(
receiver_stick: FGVec2,
attack_angle: FGi32,
di_max_angle: FGi32,
) -> FGVec2 {
let (sin, cos) = attack_angle.sin_cos();
let base_launch_direction = FGVec2::new(cos, sin);
let Some(base_launch_direction) = base_launch_direction.normalize() else {
return FGVec2::ZERO;
};
if receiver_stick.length_squared().is_zero() {
return base_launch_direction;
}
let normal = FGVec2::new(-base_launch_direction.y, base_launch_direction.x);
let di_amount = receiver_stick.dot(normal);
let angle_change = di_amount * di_max_angle.to_fixed::<FGWide>();
let angle_change = angle_change.clamp(
-di_max_angle.to_fixed::<FGWide>(),
di_max_angle.to_fixed::<FGWide>(),
);
let (sin, cos) = (attack_angle + angle_change.to_fixed::<FGi32>()).sin_cos();
FGVec2::new(cos, sin).normalize_or_zero()
}
#[cfg(test)]
mod test {
use crate::{
fighter::attack::directional_influence::apply_directional_influence,
math::{int::FGi32, vec::FGVec2},
};
#[test]
fn di_zero_passes_unmodified() {
let di = apply_directional_influence(FGVec2::ZERO, FGi32::lit("0.0"), FGi32::lit("18.0"));
const EPSILON: FGi32 = FGi32::from_bits(4);
assert!(di.x.abs_diff(FGi32::ONE) < EPSILON);
assert!(di.y.abs_diff(FGi32::ZERO) < EPSILON);
}
#[test]
fn di_applies_on_the_correct_side() {
let di = apply_directional_influence(
FGVec2::lit("0.0", "1.0"),
FGi32::lit("0.0"),
FGi32::lit("18.0"),
);
assert!(di.x > FGi32::ZERO);
assert!(di.y > FGi32::ZERO);
let di = apply_directional_influence(
FGVec2::lit("0.0", "-1.0"),
FGi32::lit("0.0"),
FGi32::lit("18.0"),
);
assert!(di.x > FGi32::ZERO);
assert!(di.y < FGi32::ZERO);
}
}
+2 -1
View File
@@ -1,6 +1,7 @@
use std::collections::{HashMap, VecDeque};
mod reaction;
pub mod directional_influence;
use bevy::ecs::entity::{EntityMapper, MapEntities};
use bevy::prelude::*;
@@ -42,7 +43,7 @@ pub enum HitReactionKind {
#[derive(Debug, Hash, Clone)]
pub struct FighterHitReaction {
pub launch_velocity: FGVec2,
pub launch_velocity: FGi32,
pub launch_angle: FGi32,
pub hitstop_frames: HitStop,
pub hitstun_frames: u32,
+3 -5
View File
@@ -1,12 +1,11 @@
use bevy::prelude::*;
use bevy_ggrs::prelude::GgrsSchedule;
use fixed_math::SinCos;
use super::*;
use crate::{
fighter::{
Fighter, FighterFacingDirection, FighterHitboxes, FighterHitstop, FighterHitstopShake, FighterHitstopShakeKind, FighterHitstopShakeRole, FighterPendingHitReaction, FighterStaleMoveQueue, errors::FighterErrors, manifest::FighterManifest, state::FighterState,
}, game_settings::GameSettings, math::{int::FGi32, vec::FGVec2}, player::Player, schedule::GameplaySet, scripting::FighterAttackScript,
}, game_settings::GameSettings, math::{int::FGi32}, player::Player, schedule::GameplaySet, scripting::FighterAttackScript,
};
pub fn resolve_fighter_hits(
@@ -90,14 +89,13 @@ pub fn resolve_fighter_hits(
} else {
angle
};
let (sin, cos) = launch_angle.sin_cos();
let launch_direction = FGVec2::new(cos, sin);
let hitstop = HitStop::calculate(FighterDamage(attack_damage), false);
reactions.push((
attacker_entity,
attacker_slot,
FighterHitReaction {
launch_velocity: launch_direction * knockback.get_launch_speed(&game_settings),
launch_velocity: knockback.get_launch_speed(&game_settings),
launch_angle,
hitstun_frames: knockback.get_hitstun_frames(&game_settings),
hitstop_frames: hitstop,