Files
shinespark/src/fighter/state/mod.rs
T
EIREXE ff78847512
CI / build-and-test (push) Successful in 10m58s
Fix run direction not being taken into account when accelerating (Fixes #11) (#35)
Reviewed-on: #35
Co-authored-by: Álex Román Núñez <eirexe@eirteam.moe>
2026-09-16 19:17:40 +00:00

476 lines
15 KiB
Rust

//! The fighter state machine: the [`FighterState`] trait every state
//! implements, the context they inspect, and the generic system that runs the
//! interrupt check. One state per submodule.
use bevy::ecs::query::QueryData;
use bevy::prelude::*;
use crate::fighter::animation::{AnimKind, FighterAnimationFrame};
use crate::fighter::attack::{
FighterAttackScriptAssets, FighterKnockbackVelocity, FighterPendingHitReaction,
FighterStaleMoveQueue,
};
use crate::fighter::attributes::FighterAttributes;
use crate::fighter::baked_animation::BakedFighterAnimations;
use crate::fighter::ecb::FighterPreviousECB;
use crate::fighter::manifest::FighterManifest;
use crate::fighter::state::hitstun::HitstunState;
use crate::fighter::visual::FighterAnimations;
use crate::fighter::{
Fighter, FighterECB, FighterFacingDirection, FighterHitboxes, FighterHitstop,
FighterPreviousTranslation, FighterTranslation, FighterVelocity,
};
use crate::game_settings::GameSettings;
use crate::input::FighterInput;
use crate::scripting::FighterAttackScript;
use crate::stage::line::StageCollision;
pub mod air;
pub mod air_dodge;
pub mod dash;
pub mod fall;
pub mod ground;
pub mod ground_attack;
pub mod hitstun;
pub mod jump;
pub mod land;
pub mod run;
pub mod turn;
pub mod wait;
pub mod walk;
macro_rules! fighter_states {
($($variant:ident => $ty:ty),* $(,)?) => {
#[derive(Component, Clone, Debug, Hash)]
pub enum FighterState { $($variant($ty)),* }
impl FighterState {
pub fn name(&self) -> &'static str {
match self { $(Self::$variant(_) => <$ty>::NAME),* }
}
pub fn check_interrupt(&self, ctx: &mut InterruptContext) -> Option<FighterState> {
match self { $(Self::$variant(s) => s.check_interrupt(ctx)),* }
}
pub fn update(&mut self, ctx: &mut UpdateContext) {
match self { $(Self::$variant(s) => s.update(ctx)),* }
}
pub fn on_enter(&mut self, ctx: &mut EnterContext) {
match self { $(Self::$variant(s) => s.on_enter(ctx)),* }
}
pub fn on_exit(&mut self, ctx: &mut ExitContext) {
match self { $(Self::$variant(s) => s.on_exit(ctx)),* }
}
pub fn check_collision_interrupt(&mut self, ctx: &mut CollisionContext) -> Option<FighterState> {
match self { $(Self::$variant(s) => s.check_collision_interrupt(ctx)),* }
}
}
$(impl From<$ty> for FighterState {
fn from(s: $ty) -> Self { Self::$variant(s) }
})*
};
}
pub trait FighterStateImpl: Sized {
const NAME: &'static str;
fn check_interrupt(&self, state_context: &mut InterruptContext) -> Option<FighterState>;
fn on_enter(&mut self, _state_context: &mut EnterContext) {}
fn on_exit(&mut self, _state_context: &mut ExitContext) {}
fn name(&self) -> String {
Self::NAME.to_owned()
}
fn update(&mut self, _state_context: &mut UpdateContext);
fn check_collision_interrupt(
&mut self,
_state_context: &mut CollisionContext,
) -> Option<FighterState> {
None
}
}
pub struct AnimationStatus {
pub frame: u32,
pub finished: bool,
}
pub struct InterruptContext<'a> {
pub input: &'a FighterInput,
pub facing_direction: &'a mut FighterFacingDirection,
pub game_settings: &'a GameSettings,
pub fighter_attributes: &'a FighterAttributes,
pub animation: AnimationStatus,
}
impl InterruptContext<'_> {
pub fn get_current_animation_frame(&self) -> u32 {
self.animation.frame
}
pub fn is_current_animation_finished(&self) -> bool {
self.animation.finished
}
}
pub struct EnterContext<'a> {
pub input: &'a mut FighterInput,
pub hitboxes: &'a mut FighterHitboxes,
pub velocity: &'a mut FighterVelocity,
pub knockback_velocity: &'a mut FighterKnockbackVelocity,
pub stale_move_queue: &'a mut FighterStaleMoveQueue,
pub facing_direction: &'a mut FighterFacingDirection,
pub animation_frame: &'a mut FighterAnimationFrame,
pub fighter_attributes: &'a FighterAttributes,
pub attack_script_assets: &'a FighterAttackScriptAssets,
}
impl EnterContext<'_> {
pub fn play_animation(&mut self, kind: AnimKind, repeat: bool) {
self.animation_frame.reset(kind, repeat);
}
}
pub struct ExitContext<'a> {
pub hitboxes: &'a mut FighterHitboxes,
}
pub struct UpdateContext<'a> {
pub translation: &'a mut FighterTranslation,
pub prev_translation: &'a mut FighterPreviousTranslation,
pub input: &'a FighterInput,
pub hitboxes: &'a mut FighterHitboxes,
pub velocity: &'a mut FighterVelocity,
pub knockback_velocity: &'a mut FighterKnockbackVelocity,
pub game_settings: &'a GameSettings,
pub animation_frame: u32,
pub fighter_attributes: &'a FighterAttributes,
pub attack_scripts: &'a Assets<FighterAttackScript>,
pub attack_script_assets: &'a FighterAttackScriptAssets,
pub facing_direction: &'a FighterFacingDirection,
}
impl UpdateContext<'_> {
pub fn get_current_animation_frame(&self) -> u32 {
self.animation_frame
}
}
pub struct CollisionContext<'a> {
pub translation: &'a mut FighterTranslation,
pub prev_translation: &'a FighterPreviousTranslation,
pub velocity: &'a mut FighterVelocity,
pub ecb: &'a FighterECB,
pub prev_ecb: &'a FighterPreviousECB,
pub stage_collision: &'a StageCollision,
}
impl CollisionContext<'_> {
pub fn air_collide_with_stage(
&self,
) -> Option<crate::fighter::collision::AirCollisionWithStageResult> {
crate::fighter::collision::air_collide_with_stage(
self.prev_translation,
self.translation,
self.prev_ecb,
self.ecb,
self.stage_collision,
)
}
}
fighter_states! {
Wait => wait::WaitState,
Walk => walk::WalkState,
Dash => dash::DashState,
Run => run::RunState,
Fall => fall::FallState,
JumpSquat => jump::JumpSquatState,
Jump => jump::JumpState,
Land => land::LandingState,
AirDodge => air_dodge::AirDodgeState,
Turn => turn::TurnState,
GroundAttack => ground_attack::GroundAttackState,
Hitstun => hitstun::HitstunState,
}
#[derive(QueryData)]
#[query_data(mutable)]
pub struct FighterFrameQuery {
pub translation: &'static mut FighterTranslation,
pub prev_translation: &'static mut FighterPreviousTranslation,
pub input: &'static mut FighterInput,
pub hitboxes: &'static mut FighterHitboxes,
pub velocity: &'static mut FighterVelocity,
pub knockback_velocity: &'static mut FighterKnockbackVelocity,
pub stale_move_queue: &'static mut FighterStaleMoveQueue,
pub ecb: &'static FighterECB,
pub prev_ecb: &'static FighterPreviousECB,
pub facing_direction: &'static mut FighterFacingDirection,
pub animation_frame: &'static mut FighterAnimationFrame,
pub fighter: &'static Fighter,
}
impl<'w, 's> FighterFrameQueryItem<'w, 's> {
fn animation_status(
&self,
animations: &FighterAnimations,
baked_animations: &Assets<BakedFighterAnimations>,
) -> AnimationStatus {
let baked = baked_animations
.get(&animations.baked)
.expect("Animation should exist");
let frame = self.animation_frame.frame;
let finished = baked
.frame_count(self.animation_frame.kind)
.expect("Current animation should have baked frames")
<= frame;
AnimationStatus { frame, finished }
}
fn interrupt_context<'a>(
&'a mut self,
game_settings: &'a GameSettings,
animations: &'a FighterAnimations,
baked_animations: &'a Assets<BakedFighterAnimations>,
attributes: &'a FighterAttributes,
) -> InterruptContext<'a> {
let animation = self.animation_status(animations, baked_animations);
InterruptContext {
input: &self.input,
facing_direction: &mut self.facing_direction,
game_settings,
fighter_attributes: attributes,
animation,
}
}
fn enter_context<'a>(
&'a mut self,
attributes: &'a FighterAttributes,
attack_script_assets: &'a FighterAttackScriptAssets,
) -> EnterContext<'a> {
EnterContext {
input: &mut self.input,
hitboxes: &mut self.hitboxes,
velocity: &mut self.velocity,
knockback_velocity: &mut self.knockback_velocity,
stale_move_queue: &mut self.stale_move_queue,
facing_direction: &mut self.facing_direction,
animation_frame: &mut self.animation_frame,
fighter_attributes: attributes,
attack_script_assets,
}
}
fn exit_context(&mut self) -> ExitContext<'_> {
ExitContext {
hitboxes: &mut self.hitboxes,
}
}
fn update_context<'a>(
&'a mut self,
game_settings: &'a GameSettings,
attributes: &'a FighterAttributes,
attack_scripts: &'a Assets<FighterAttackScript>,
attack_script_assets: &'a FighterAttackScriptAssets,
) -> UpdateContext<'a> {
UpdateContext {
translation: &mut self.translation,
prev_translation: &mut self.prev_translation,
input: &self.input,
hitboxes: &mut self.hitboxes,
velocity: &mut self.velocity,
knockback_velocity: &mut self.knockback_velocity,
game_settings,
animation_frame: self.animation_frame.frame,
fighter_attributes: attributes,
attack_scripts,
attack_script_assets,
facing_direction: &self.facing_direction
}
}
fn collision_context<'a>(
&'a mut self,
stage_collision: &'a StageCollision,
) -> CollisionContext<'a> {
CollisionContext {
translation: &mut self.translation,
prev_translation: &self.prev_translation,
velocity: &mut self.velocity,
ecb: &self.ecb,
prev_ecb: &self.prev_ecb,
stage_collision,
}
}
}
#[derive(QueryData)]
#[query_data(mutable)]
pub struct StatefulFighterQuery {
pub state: &'static mut FighterState,
pub frame: FighterFrameQuery,
pub animations: &'static FighterAnimations,
pub fighter: &'static Fighter,
pub attack_script_assets: &'static FighterAttackScriptAssets,
pub pending_hit_reaction: &'static mut FighterPendingHitReaction,
pub hitstop: &'static mut FighterHitstop,
}
pub fn state_update_system(
mut query: Query<StatefulFighterQuery>,
game_settings: Res<GameSettings>,
attack_scripts: Res<Assets<FighterAttackScript>>,
manifests: Res<Assets<FighterManifest>>,
) {
for fighter in &mut query {
let StatefulFighterQueryItem {
mut state,
mut frame,
hitstop,
..
} = fighter;
if hitstop.remaining_frames > 0 {
continue;
}
let manifest = manifests
.get(&fighter.fighter.manifest)
.expect("Fighter manifest should be valid");
let mut update_ctx = frame.update_context(
&game_settings,
&manifest.attributes,
&attack_scripts,
&fighter.attack_script_assets,
);
state.update(&mut update_ctx);
}
}
fn transition_state(
current: &mut FighterState,
mut next: FighterState,
frame: &mut FighterFrameQueryItem,
attributes: &FighterAttributes,
attack_script_assets: &FighterAttackScriptAssets,
) {
let old_name = current.name();
let new_name = next.name();
current.on_exit(&mut frame.exit_context());
next.on_enter(&mut frame.enter_context(attributes, attack_script_assets));
*current = next;
info!("{} -> {}", old_name, new_name);
}
pub fn state_interrupt_system(
mut query: Query<StatefulFighterQuery>,
game_settings: Res<GameSettings>,
baked_animations: Res<Assets<BakedFighterAnimations>>,
fighter_manifests: Res<Assets<FighterManifest>>,
) {
for fighter in &mut query {
let StatefulFighterQueryItem {
mut state,
mut frame,
animations,
fighter,
attack_script_assets,
mut pending_hit_reaction,
mut hitstop,
} = fighter;
let manifest = fighter_manifests
.get(&fighter.manifest)
.expect("Fighter manifest should be valid");
let new_state = if let Some(reaction) = pending_hit_reaction.take() {
hitstop.remaining_frames = *reaction.hitstop_frames;
let hitstop_kind = match &*state {
FighterState::Wait(value) => {
hitstun::HitstopStateKind::Grounded(value.grounded_common.clone())
}
FighterState::Walk(value) => {
hitstun::HitstopStateKind::Grounded(value.grounded_common.clone())
}
FighterState::Dash(value) => {
hitstun::HitstopStateKind::Grounded(value.ground_common.clone())
}
FighterState::Run(value) => {
hitstun::HitstopStateKind::Grounded(value.grounded_common.clone())
}
FighterState::Turn(value) => {
hitstun::HitstopStateKind::Grounded(value.grounded_common.clone())
}
FighterState::GroundAttack(value) => {
hitstun::HitstopStateKind::Grounded(value.grounded_common.clone())
}
_ => hitstun::HitstopStateKind::Airborne,
};
Some(FighterState::Hitstun(HitstunState {
hit_reaction: reaction,
hitstop_kind,
}))
} else if hitstop.remaining_frames > 0 {
None
} else {
state.check_interrupt(&mut frame.interrupt_context(
&game_settings,
animations,
&baked_animations,
&manifest.attributes,
))
};
if let Some(new_state) = new_state {
transition_state(
&mut state,
new_state,
&mut frame,
&manifest.attributes,
attack_script_assets,
);
};
}
}
pub fn state_collision_interrupt_system(
mut query: Query<StatefulFighterQuery>,
stage_collision: Res<StageCollision>,
fighter_manifests: Res<Assets<FighterManifest>>,
) {
for fighter in &mut query {
let StatefulFighterQueryItem {
mut state,
mut frame,
fighter,
attack_script_assets,
hitstop,
..
} = fighter;
if hitstop.remaining_frames > 0 {
continue;
}
let manifest = fighter_manifests
.get(&fighter.manifest)
.expect("Fighter manifest should be valid");
let new_state =
state.check_collision_interrupt(&mut frame.collision_context(&stage_collision));
if let Some(new_state) = new_state {
transition_state(
&mut state,
new_state,
&mut frame,
&manifest.attributes,
&attack_script_assets,
);
}
}
}