Implement animation root motion deltas (#84)
CI / build-and-test (push) Successful in 1m33s
CI / production-build (push) Successful in 12m5s

Implements root motion and adds a dash attack.

Reviewed-on: #84
Co-authored-by: Álex Román Núñez <eirexe@eirteam.moe>
This commit was merged in pull request #84.
This commit is contained in:
2026-10-05 20:04:35 +00:00
committed by EIREXE
parent 5c277a00f0
commit 7a9adbd6a4
13 changed files with 459 additions and 23 deletions
+83 -11
View File
@@ -1,14 +1,20 @@
use crate::fighter::{
attack::{AttackKind, update_active_attack},
state::{
CollisionContext, EnterContext, ExitContext, FighterState, FighterStateImpl,
InterruptContext, UpdateContext, wait::WaitState,
use bevy::{asset::Assets, log::error};
use crate::{
fighter::{
attack::{AttackKind, FighterAttackScriptAssets, update_active_attack},
state::{
CollisionContext, EnterContext, ExitContext, FighterState, FighterStateImpl,
InterruptContext, UpdateContext, ground, wait::WaitState,
},
},
scripting::FighterAttackScript,
};
#[derive(Clone, Debug, Hash)]
pub struct GroundAttackState {
pub attack_kind: AttackKind,
pub use_root_motion: bool,
}
impl FighterStateImpl for GroundAttackState {
@@ -37,6 +43,18 @@ impl FighterStateImpl for GroundAttackState {
.expect("ground attack must have active hitboxes"),
animation_frame,
);
// root motion
if script.use_root_motion {
state_context.velocity.0.x = state_context.animation_root_motion_delta.x;
ground::apply_grounded_motion(
state_context.velocity,
state_context.translation,
state_context.prev_translation,
);
} else {
ground::update_friction_motion(state_context);
}
}
}
@@ -48,7 +66,7 @@ impl FighterStateImpl for GroundAttackState {
let script = state_context.attack_script_assets.scripts[&self.attack_kind].clone();
state_context.hitboxes.0 = Some(crate::fighter::FighterActiveAttack {
kind: self.attack_kind,
script,
script: script.clone(),
active_hitboxes: Vec::new(),
solved_hitboxes: Vec::new(),
hit_groups: Vec::new(),
@@ -62,9 +80,9 @@ impl FighterStateImpl for GroundAttackState {
fn check_collision_interrupt(
&mut self,
_state_context: &mut CollisionContext,
state_context: &mut CollisionContext,
) -> Option<FighterState> {
None
ground::collision_interrupt(state_context, !self.use_root_motion)
}
fn is_visually_grounded<'a>(&'a self) -> bool {
@@ -72,6 +90,51 @@ impl FighterStateImpl for GroundAttackState {
}
}
impl GroundAttackState {
fn create(
attack_kind: AttackKind,
attack_script_assets: &FighterAttackScriptAssets,
attack_scripts: &Assets<FighterAttackScript>,
) -> Result<Self, AirAttackStateCreateError> {
let asset_script = attack_script_assets.scripts.get(&attack_kind).ok_or(
AirAttackStateCreateError::AttackScriptAssetNotFound {
attack: attack_kind,
},
)?;
let script = attack_scripts.get(asset_script).ok_or(
AirAttackStateCreateError::AttackScriptNotFound {
attack: attack_kind,
},
)?;
Ok(Self {
attack_kind,
use_root_motion: script.use_root_motion
})
}
}
#[derive(thiserror::Error, Debug)]
pub enum AirAttackStateCreateError {
#[error("Attack script: {attack:?} not found")]
AttackScriptNotFound { attack: AttackKind },
#[error("Attack script asset: {attack:?} not found")]
AttackScriptAssetNotFound { attack: AttackKind },
}
pub fn check_interrupt_shared(state_context: &InterruptContext, attack_kind: AttackKind) -> Option<FighterState> {
let state = GroundAttackState::create(attack_kind, state_context.attack_script_assets, state_context.attack_scripts);
match state {
Ok(state) => Some(FighterState::GroundAttack(state)),
Err(create_error) => {
error!("{}", create_error);
None
},
}
}
pub fn check_interrupt(state_context: &InterruptContext) -> Option<FighterState> {
if state_context
.input
@@ -81,9 +144,18 @@ pub fn check_interrupt(state_context: &InterruptContext) -> Option<FighterState>
.input
.get_attack_command_type()
.to_attack_kind_grounded();
Some(FighterState::GroundAttack(GroundAttackState {
attack_kind,
}))
check_interrupt_shared(state_context, attack_kind)
} else {
None
}
}
pub fn check_interrupt_dash_attack(state_context: &InterruptContext) -> Option<FighterState> {
if state_context
.input
.has_command(crate::input::FighterCommands::Attack)
{
check_interrupt_shared(state_context, AttackKind::DashAttack)
} else {
None
}