use super::{ CollisionContext, EnterContext, FighterStateImpl, InterruptContext, UpdateContext, ground, run, wait, walk, }; use crate::fighter::FighterFacingDirection; use crate::fighter::animation::AnimKind; use crate::fighter::state::FighterState; use crate::input::FighterCommands::{SmashMoveLeft, SmashMoveRight}; #[derive(Debug, Clone, Hash)] pub struct DashState { direction: FighterFacingDirection, } impl DashState { pub fn new(direction: FighterFacingDirection) -> Self { Self { direction } } } impl FighterStateImpl for DashState { const NAME: &'static str = "Dash"; fn check_interrupt(&self, ctx: &mut InterruptContext) -> Option { let attributes = ctx.fighter_attributes; // A reverse dash can interrupt before IASA if let Some(direction) = check_smash_input_with_dir(ctx).filter(|direction| *direction != self.direction) { return Some(FighterState::Dash(DashState::new(direction))); } if let Some(state) = ground::grounded_movement_common_interrupts(ctx) { return Some(state); } if ctx.is_current_animation_finished() { return run::check_input(ctx) .or_else(|| walk::check_input(ctx)) .or_else(|| wait::check_input(ctx)); } if ctx.get_current_animation_frame() > attributes.dash_acceleration_duration { return run::check_input(ctx); } None } fn on_enter(&mut self, state_context: &mut EnterContext) { *state_context.facing_direction = self.direction; state_context.velocity.x = state_context.fighter_attributes.dash_initial_velocity * self.direction.to_sign(); state_context.play_animation(AnimKind::Dash, false); match state_context.facing_direction { FighterFacingDirection::Left => { state_context .input .clear_command(crate::input::FighterCommands::SmashMoveLeft); } FighterFacingDirection::Right => { state_context .input .clear_command(crate::input::FighterCommands::SmashMoveRight); } } } fn update(&mut self, state_context: &mut UpdateContext) { if state_context.get_current_animation_frame() < state_context.fighter_attributes.dash_acceleration_duration {} let last_frame = state_context.input.get_last_frame(); let (accel, target_vel) = state_context .fighter_attributes .get_accel_and_target_dashrun(&last_frame, self.direction.to_sign()); let accel = ground::compute_ground_accel( accel, target_vel, state_context.velocity.x, state_context.fighter_attributes, state_context.game_settings, ); state_context.velocity.x += accel; ground::apply_grounded_motion( state_context.velocity, state_context.translation, state_context.prev_translation, ); } fn check_collision_interrupt( &mut self, state_context: &mut CollisionContext, ) -> Option { ground::collision_interrupt(state_context, true) } fn is_visually_grounded<'a>(&'a self) -> bool { true } } fn check_smash_input_with_dir(state_context: &InterruptContext) -> Option { if state_context.input.has_command(SmashMoveLeft) { return Some(FighterFacingDirection::Left); } else if state_context.input.has_command(SmashMoveRight) { return Some(FighterFacingDirection::Right); } else { None } } pub fn check_input(state_context: &InterruptContext) -> Option { check_smash_input_with_dir(state_context).map(|dir| FighterState::Dash(DashState::new(dir))) }