Refactor loading

This commit is contained in:
2026-08-26 19:08:58 +02:00
parent 4ff388f9c0
commit 21a5170dbc
40 changed files with 1034 additions and 426 deletions
+2 -2
View File
@@ -1,6 +1,6 @@
use bevy::prelude::*;
use crate::fighter::{*};
use crate::fighter::*;
pub fn integrate_gravity(velocity: &mut FighterVelocity, attributes: &FighterAttributes) {
velocity.y -= attributes.gravity;
@@ -15,4 +15,4 @@ pub fn apply_air_motion(
// Vertical velocity should be 0 on the ground
prev_translation.0 = translation.0;
translation.0 += velocity.0;
}
}
+35 -27
View File
@@ -3,13 +3,13 @@ use std::time::Duration;
use bevy::prelude::*;
use super::{FighterStateImpl, FighterStateContext, ground, run, wait, walk};
use super::{FighterStateContext, FighterStateImpl, ground, run, wait, walk};
use crate::fighter::animation::AnimKind;
use crate::fighter::state::FighterState;
use crate::fighter::state::fall::FallState;
use crate::fighter::state::ground::{GroundedMotionResult, GroundedStateCommon};
use crate::fighter::state::wait::WaitState;
use crate::fighter::state::{FighterState};
use crate::fighter::{FighterAttributes, FighterVelocity, collision};
use crate::fighter::{FighterAttributes, FighterFacingDirection, FighterVelocity, collision};
use crate::game_settings::GameSettings;
use crate::input::{
FighterCommands::{SmashMoveLeft, SmashMoveRight},
@@ -20,33 +20,30 @@ use crate::math::int::FGi32;
#[derive(Debug, Clone)]
pub struct DashState {
frames_in_dash: u32,
direction: FGi32,
ground_common: GroundedStateCommon
direction: FighterFacingDirection,
ground_common: GroundedStateCommon,
}
impl std::hash::Hash for DashState {
fn hash<H: Hasher>(&self, state: &mut H) {
self.direction.to_bits().hash(state);
self.direction.hash(state);
self.frames_in_dash.hash(state);
}
}
impl DashState {
pub fn new(direction: FGi32, ground_common: GroundedStateCommon) -> Self {
pub fn new(direction: FighterFacingDirection, ground_common: GroundedStateCommon) -> Self {
Self {
frames_in_dash: 0,
direction,
ground_common
ground_common,
}
}
}
impl FighterStateImpl for DashState {
const NAME: &'static str = "Dash";
fn check_interrupt(
&self,
state_context: &FighterStateContext,
) -> Option<FighterState> {
fn check_interrupt(&self, state_context: &FighterStateContext) -> Option<FighterState> {
if self.frames_in_dash > state_context.fighter_attribs.dash_duration {
run::check_input(state_context, &self.ground_common)
.or_else(|| walk::check_input(state_context, &self.ground_common))
@@ -58,13 +55,20 @@ impl FighterStateImpl for DashState {
} else {
check_smash_input_with_dir(state_context)
.filter(|dir| *dir != self.direction)
.and_then(|dir| Some(FighterState::Dash(DashState::new(dir, self.ground_common.clone()))))
.and_then(|dir| {
Some(FighterState::Dash(DashState::new(
dir,
self.ground_common.clone(),
)))
})
}
}
fn on_enter(&mut self, state_context: &mut FighterStateContext) {
*state_context.facing_direction = self.direction;
state_context.input.clear_buffer();
state_context.velocity.x = state_context.fighter_attribs.dash_initial_velocity * self.direction;
state_context.velocity.x =
state_context.fighter_attribs.dash_initial_velocity * self.direction.to_sign();
state_context.animation_transitions.play(
&mut state_context.animation_player,
@@ -72,14 +76,16 @@ impl FighterStateImpl for DashState {
Duration::ZERO,
);
}
fn update(&mut self, state_context: &mut FighterStateContext) {
self.frames_in_dash += 1;
info!("{}", self.frames_in_dash);
let last_frame = state_context.input.get_last_frame();
let (accel, target_vel) = state_context.fighter_attribs.get_accel_and_target_dashrun(&last_frame, self.direction);
let (accel, target_vel) = state_context
.fighter_attribs
.get_accel_and_target_dashrun(&last_frame, self.direction.to_sign());
let accel = ground::compute_ground_accel(
accel,
target_vel,
@@ -100,11 +106,9 @@ impl FighterStateImpl for DashState {
&mut self,
state_context: &mut FighterStateContext,
) -> Option<FighterState> {
if let GroundedMotionResult::InAir = ground::collide_with_stage_grounded(
state_context,
&mut self.ground_common,
true
) {
if let GroundedMotionResult::InAir =
ground::collide_with_stage_grounded(state_context, &mut self.ground_common, true)
{
Some(FighterState::Fall(FallState))
} else {
None
@@ -112,16 +116,20 @@ impl FighterStateImpl for DashState {
}
}
fn check_smash_input_with_dir(state_context: &FighterStateContext) -> Option<FGi32> {
fn check_smash_input_with_dir(state_context: &FighterStateContext) -> Option<FighterFacingDirection> {
if state_context.input.has_command(SmashMoveLeft) {
return Some(FGi32::NEG_ONE);
return Some(FighterFacingDirection::Left);
} else if state_context.input.has_command(SmashMoveRight) {
return Some(FGi32::ONE);
return Some(FighterFacingDirection::Right);
} else {
None
}
}
pub fn check_input(state_context: &FighterStateContext, ground_common: &GroundedStateCommon) -> Option<FighterState> {
check_smash_input_with_dir(state_context).map(|dir| FighterState::Dash(DashState::new(dir, ground_common.clone())))
pub fn check_input(
state_context: &FighterStateContext,
ground_common: &GroundedStateCommon,
) -> Option<FighterState> {
check_smash_input_with_dir(state_context)
.map(|dir| FighterState::Dash(DashState::new(dir, ground_common.clone())))
}
+12 -12
View File
@@ -1,9 +1,9 @@
use bevy::prelude::*;
use super::{FighterStateImpl, FighterStateContext, ground, walk};
use crate::fighter::state::{FighterState, air};
use super::{FighterStateContext, FighterStateImpl, ground, walk};
use crate::fighter::state::ground::GroundedStateCommon;
use crate::fighter::state::wait::WaitState;
use crate::fighter::state::{FighterState, air};
use crate::fighter::{FighterAttributes, FighterVelocity, collision, motion};
use crate::game_settings::GameSettings;
use crate::stage::line::StageCollision;
@@ -13,18 +13,19 @@ pub struct FallState;
impl FighterStateImpl for FallState {
const NAME: &'static str = "Fall";
fn check_interrupt(
&self,
state_context: &FighterStateContext,
) -> Option<FighterState> {
fn check_interrupt(&self, state_context: &FighterStateContext) -> Option<FighterState> {
None
}
fn on_enter(&mut self, _state_context: &mut super::FighterStateContext) {}
fn update(&mut self, state_context: &mut super::FighterStateContext) {
air::integrate_gravity(state_context.velocity, state_context.fighter_attribs);
air::apply_air_motion(state_context.velocity, state_context.translation, state_context.prev_translation);
air::apply_air_motion(
state_context.velocity,
state_context.translation,
state_context.prev_translation,
);
}
fn check_collision_interrupt(
@@ -35,9 +36,9 @@ impl FighterStateImpl for FallState {
// Adjust translation
state_context.translation.0 = res.hit_position - state_context.ecb.get_bottom_point();
let grounded_common = GroundedStateCommon {
current_line_id: res.line_id
current_line_id: res.line_id,
};
Some(FighterState::Wait(WaitState { grounded_common } ))
Some(FighterState::Wait(WaitState { grounded_common }))
} else {
None
}
@@ -52,4 +53,3 @@ pub fn check_input(state_context: &FighterStateContext) -> Option<FighterState>
}
None
}
+2 -2
View File
@@ -44,7 +44,7 @@ pub fn compute_ground_accel(
} else {
let mut accel = accel;
let ground_max_horizontal_velocity =
game_settings.figher_common.ground_max_horizontal_velocity;
game_settings.fighter_common.ground_max_horizontal_velocity;
if !(gr_vel * accel < FGi32::ZERO) {
// accelerating, not reversing
if accel > FGi32::ZERO {
@@ -146,7 +146,7 @@ pub fn collide_with_stage_grounded(
pub fn grounded_movement_common_interrupts(
state_context: &FighterStateContext,
ground_common: &GroundedStateCommon
ground_common: &GroundedStateCommon,
) -> Option<FighterState> {
super::jump::check_input(state_context, ground_common)
}
+17 -7
View File
@@ -1,12 +1,13 @@
use std::time::Duration;
use bevy::prelude::*;
use crate::{
fighter::state::{
fighter::{animation::AnimKind, state::{
FighterState, FighterStateContext, FighterStateImpl,
fall::FallState,
ground::{self, GroundedMotionResult, GroundedStateCommon},
},
input::FighterCommands,
}}, input::FighterCommands,
};
pub enum JumpType {
@@ -52,7 +53,7 @@ impl FighterStateImpl for JumpSquatState {
state_context.fighter_attribs.ground_friction
* state_context
.game_settings
.figher_common
.fighter_common
.ground_friction_over_walk_speed_multiplier
} else {
state_context.fighter_attribs.ground_friction
@@ -81,6 +82,17 @@ impl FighterStateImpl for JumpSquatState {
None
}
}
fn play_animation(
&self,
transitions: &mut AnimationTransitions,
player: &mut AnimationPlayer,
anims: &crate::fighter::visual::FighterAnimations,
) {
transitions.play(player, anims.clips[&AnimKind::JumpSquat], Duration::ZERO);
}
fn on_enter(&mut self, _state_context: &mut FighterStateContext) {}
}
impl FighterStateImpl for JumpState {
@@ -99,7 +111,5 @@ pub fn check_input(
state_context
.input
.has_command(FighterCommands::Jump)
.then(|| FighterState::JumpSquat(JumpSquatState::create(
ground_common.clone(),
)))
.then(|| FighterState::JumpSquat(JumpSquatState::create(ground_common.clone())))
}
+4 -1
View File
@@ -9,7 +9,7 @@ use crate::fighter::animation::FighterAnimationPlayerLink;
use crate::fighter::ecb::FighterPreviousECB;
use crate::fighter::visual::FighterAnimations;
use crate::fighter::{
FighterAttributes, FighterECB, FighterPreviousTranslation, FighterTranslation, FighterVelocity,
FighterAttributes, FighterECB, FighterFacingDirection, FighterPreviousTranslation, FighterTranslation, FighterVelocity,
};
use crate::game_settings::GameSettings;
use crate::input::FighterInput;
@@ -88,6 +88,7 @@ pub struct FighterStateContext<'a> {
pub ecb: &'a mut FighterECB,
pub prev_ecb: &'a mut FighterPreviousECB,
pub fighter_attribs: &'a FighterAttributes,
pub facing_direction: &'a mut FighterFacingDirection,
pub game_settings: &'a GameSettings,
pub stage_collision: &'a StageCollision,
pub animations: &'a FighterAnimations,
@@ -114,6 +115,7 @@ pub struct FighterFrameQuery {
pub velocity: &'static mut FighterVelocity,
pub ecb: &'static mut FighterECB,
pub prev_ecb: &'static mut FighterPreviousECB,
pub facing_direction: &'static mut FighterFacingDirection,
pub attributes: &'static FighterAttributes,
}
@@ -133,6 +135,7 @@ impl<'w, 's> FighterFrameQueryItem<'w, 's> {
velocity: &mut self.velocity,
ecb: &mut self.ecb,
prev_ecb: &mut self.prev_ecb,
facing_direction: &mut self.facing_direction,
fighter_attribs: self.attributes,
game_settings,
stage_collision,
+32 -24
View File
@@ -2,7 +2,7 @@ use std::time::Duration;
use bevy::prelude::*;
use super::{FighterStateImpl, FighterStateContext, ground, wait, walk};
use super::{FighterStateContext, FighterStateImpl, ground, wait, walk};
use crate::fighter::animation::AnimKind;
use crate::fighter::state::FighterState;
use crate::fighter::state::fall::FallState;
@@ -14,34 +14,39 @@ use crate::math::int::FGi32;
#[derive(Component, Debug, Clone, Hash)]
pub struct RunState {
pub grounded_common: GroundedStateCommon
pub grounded_common: GroundedStateCommon,
}
impl FighterStateImpl for RunState {
const NAME: &'static str = "Run";
fn check_interrupt(
&self,
state_context: &FighterStateContext,
) -> Option<FighterState> {
fn check_interrupt(&self, state_context: &FighterStateContext) -> Option<FighterState> {
let input_frame = state_context.input.get_last_frame();
if input_frame.movement.x.abs() >= state_context.game_settings.input_common.run_stick_threshold {
if input_frame.movement.x.abs()
>= state_context.game_settings.input_common.run_stick_threshold
{
None
} else {
walk::check_input(state_context, &self.grounded_common).or_else(|| wait::check_input(state_context, &self.grounded_common))
walk::check_input(state_context, &self.grounded_common)
.or_else(|| wait::check_input(state_context, &self.grounded_common))
}
}
fn on_enter(&mut self, state_context: &mut super::FighterStateContext) {
state_context.animation_transitions.play(
&mut state_context.animation_player,
state_context.animations.clips[&AnimKind::Run],
Duration::ZERO,
).repeat();
state_context
.animation_transitions
.play(
&mut state_context.animation_player,
state_context.animations.clips[&AnimKind::Run],
Duration::ZERO,
)
.repeat();
}
fn update(&mut self, state_context: &mut super::FighterStateContext) {
let last_frame = state_context.input.get_last_frame();
let (accel, target_vel) = state_context.fighter_attribs.get_accel_and_target_dashrun(&last_frame, FGi32::ONE);
let (accel, target_vel) = state_context
.fighter_attribs
.get_accel_and_target_dashrun(&last_frame, FGi32::ONE);
let accel = ground::compute_ground_accel(
accel,
target_vel,
@@ -62,11 +67,9 @@ impl FighterStateImpl for RunState {
&mut self,
state_context: &mut FighterStateContext,
) -> Option<FighterState> {
if let GroundedMotionResult::InAir = ground::collide_with_stage_grounded(
state_context,
&mut self.grounded_common,
false
) {
if let GroundedMotionResult::InAir =
ground::collide_with_stage_grounded(state_context, &mut self.grounded_common, false)
{
Some(FighterState::Fall(FallState))
} else {
None
@@ -74,11 +77,16 @@ impl FighterStateImpl for RunState {
}
}
pub fn check_input(state_context: &FighterStateContext, ground_state_common: &GroundedStateCommon) -> Option<FighterState> {
pub fn check_input(
state_context: &FighterStateContext,
ground_state_common: &GroundedStateCommon,
) -> Option<FighterState> {
let input_frame = state_context.input.get_last_frame();
if input_frame.movement.x.abs() >= state_context.game_settings.input_common.run_stick_threshold
{
return Some(FighterState::Run(RunState { grounded_common: ground_state_common.clone() }));
return Some(FighterState::Run(RunState {
grounded_common: ground_state_common.clone(),
}));
}
None
}
}
+14 -10
View File
@@ -4,11 +4,11 @@ use bevy::prelude::*;
use super::{FighterStateContext, FighterStateImpl, ground, walk};
use crate::fighter::animation::AnimKind;
use crate::fighter::state::{FighterState, dash};
use crate::fighter::state::fall::FallState;
use crate::fighter::state::ground::{
GroundedMotionResult, GroundedStateCommon, grounded_movement_common_interrupts,
};
use crate::fighter::state::{FighterState, dash};
use crate::fighter::{FighterAttributes, FighterVelocity};
use crate::game_settings::GameSettings;
use crate::stage::line::StageLineID;
@@ -21,17 +21,21 @@ pub struct WaitState {
impl FighterStateImpl for WaitState {
const NAME: &'static str = "Wait";
fn check_interrupt(&self, state_context: &FighterStateContext) -> Option<FighterState> {
dash::check_input(state_context, &self.grounded_common).or_else(||
grounded_movement_common_interrupts(state_context, &self.grounded_common)
.or_else(|| walk::check_input(state_context, &self.grounded_common)))
dash::check_input(state_context, &self.grounded_common).or_else(|| {
grounded_movement_common_interrupts(state_context, &self.grounded_common)
.or_else(|| walk::check_input(state_context, &self.grounded_common))
})
}
fn on_enter(&mut self, state_context: &mut super::FighterStateContext) {
state_context.animation_transitions.play(
&mut state_context.animation_player,
state_context.animations.clips[&AnimKind::Wait],
Duration::ZERO,
).repeat();
state_context
.animation_transitions
.play(
&mut state_context.animation_player,
state_context.animations.clips[&AnimKind::Wait],
Duration::ZERO,
)
.repeat();
}
fn update(&mut self, state_context: &mut super::FighterStateContext) {
@@ -40,7 +44,7 @@ impl FighterStateImpl for WaitState {
state_context.fighter_attribs.ground_friction
* state_context
.game_settings
.figher_common
.fighter_common
.ground_friction_over_walk_speed_multiplier
} else {
state_context.fighter_attribs.ground_friction
+8 -5
View File
@@ -24,11 +24,14 @@ impl FighterStateImpl for WalkState {
}
fn on_enter(&mut self, state_context: &mut FighterStateContext) {
state_context.animation_transitions.play(
&mut state_context.animation_player,
state_context.animations.clips[&AnimKind::Walk],
Duration::ZERO,
).repeat();
state_context
.animation_transitions
.play(
&mut state_context.animation_player,
state_context.animations.clips[&AnimKind::Walk],
Duration::ZERO,
)
.repeat();
}
fn update(&mut self, state_context: &mut FighterStateContext) {