Refactor state system
This commit is contained in:
+7
-1
@@ -5,6 +5,12 @@ pub struct FighterAttributes {
|
||||
pub base_walk_accel: f32,
|
||||
pub stick_walk_accel: f32,
|
||||
pub max_walk_vel: f32,
|
||||
|
||||
pub dash_duration: u32,
|
||||
pub dash_initial_velocity: f32,
|
||||
pub base_dash_accel: f32,
|
||||
pub stick_dash_accel: f32,
|
||||
pub max_dash_vel: f32,
|
||||
|
||||
pub ground_friction: f32,
|
||||
pub dash_duration: u32
|
||||
}
|
||||
@@ -1,100 +0,0 @@
|
||||
use bevy::{
|
||||
color::palettes::css::{GREEN, HOT_PINK, RED, YELLOW},
|
||||
ecs::relationship::Relationship,
|
||||
prelude::*,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
game_settings::GameSettings,
|
||||
input::player::{ControlledBy, FighterInput},
|
||||
stage::StagePlane,
|
||||
};
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct Grounded;
|
||||
|
||||
#[derive(Component, Deref, DerefMut, Default, Debug)]
|
||||
pub struct FighterVelocity(pub Vec2);
|
||||
|
||||
#[derive(Component, Deref, DerefMut, Default)]
|
||||
pub struct FighterPreviousTranslation(pub Vec2);
|
||||
|
||||
#[derive(Component, Deref, DerefMut, Default)]
|
||||
#[require(FighterPreviousTranslation)]
|
||||
pub struct FighterTranslation(pub Vec2);
|
||||
|
||||
#[derive(Component, Debug)]
|
||||
#[require(Transform)]
|
||||
pub struct FighterECB {
|
||||
pub vertical_half: f32,
|
||||
pub horizontal_half: f32,
|
||||
}
|
||||
|
||||
impl FighterECB {
|
||||
pub fn to_3d_lineloop(&self, trf: Transform) -> [Vec3; 4] {
|
||||
let rel_up = trf.transform_point(Vec3::new(0.0, self.vertical_half, 0.0));
|
||||
let rel_down = trf.transform_point(Vec3::new(0.0, -self.vertical_half, 0.0));
|
||||
let rel_left = trf.transform_point(Vec3::new(-self.horizontal_half, 0.0, 0.0));
|
||||
let rel_right = trf.transform_point(Vec3::new(self.horizontal_half, 0.0, 0.0));
|
||||
[rel_up, rel_right, rel_down, rel_left]
|
||||
}
|
||||
}
|
||||
|
||||
pub fn debug_draw_ecb(ecbs: Query<(&FighterECB, &FighterTranslation)>, mut gizmos: Gizmos) {
|
||||
for (ecb, trf) in ecbs {
|
||||
let transformed = ecb.to_3d_lineloop(Transform::from_xyz(trf.x, trf.y, 0.0));
|
||||
gizmos.lineloop(transformed, YELLOW);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn integrate_gravity(
|
||||
fighters: Query<(&mut FighterVelocity)>,
|
||||
time: Res<Time<Fixed>>,
|
||||
) {
|
||||
/*for (movement, mut velocity) in fighters {
|
||||
//velocity.y -= movement.gravity;
|
||||
}*/
|
||||
}
|
||||
|
||||
pub fn apply_motion(
|
||||
fighters: Query<(
|
||||
&FighterVelocity,
|
||||
&mut FighterTranslation,
|
||||
&mut FighterPreviousTranslation,
|
||||
)>,
|
||||
time: Res<Time<Fixed>>,
|
||||
) {
|
||||
for (velocity, mut translation, mut prev_translation) in fighters {
|
||||
prev_translation.0 = translation.0;
|
||||
translation.0 += velocity.0;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn collide_fighter_with_scene(
|
||||
fighters: Query<(&FighterECB, &mut FighterVelocity, &mut FighterTranslation)>,
|
||||
stage_planes: Query<&StagePlane>,
|
||||
mut gizmos: Gizmos,
|
||||
) {
|
||||
for (ecb, mut velocity, mut translation) in fighters {
|
||||
for plane in stage_planes {
|
||||
let dist_sq = ecb.vertical_half * ecb.vertical_half;
|
||||
let ray = Ray2d::new(translation.0, Dir2::NEG_Y);
|
||||
|
||||
let intersection_result = plane
|
||||
.intersect_ray(ray)
|
||||
.filter(|result| result.position.distance_squared(translation.0) <= dist_sq);
|
||||
|
||||
if let Some(result) = intersection_result {
|
||||
gizmos.sphere(
|
||||
Isometry3d::from(Vec3::new(result.position.x, result.position.y, 0.0)),
|
||||
0.1f32,
|
||||
HOT_PINK,
|
||||
);
|
||||
velocity.0 = plane.project(velocity.0);
|
||||
translation.0 = result.position + Vec2::new(0.0, ecb.vertical_half);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn fighter_movement() {}
|
||||
@@ -1,8 +0,0 @@
|
||||
pub mod plugin;
|
||||
pub mod fighter;
|
||||
pub mod data;
|
||||
pub mod state;
|
||||
pub mod states;
|
||||
pub use fighter::*;
|
||||
pub use plugin::*;
|
||||
pub use data::*;
|
||||
+22
-3
@@ -1,8 +1,11 @@
|
||||
use bevy::prelude::*;
|
||||
use crate::fighter::states;
|
||||
use bevy_egui::{EguiContexts, EguiPrimaryContextPass, egui};
|
||||
use crate::fighter::state::StateNameDebug;
|
||||
use crate::fighter::{FighterVelocity, states};
|
||||
use crate::fighter::{
|
||||
FighterECB, FighterTranslation, FighterVelocity, apply_motion, collide_fighter_with_scene, integrate_gravity, state::state_interrupt_system,
|
||||
apply_motion, collide_fighter_with_scene, integrate_gravity, state::state_interrupt_system,
|
||||
};
|
||||
use crate::input::player::FighterInput;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct FighterPlugin;
|
||||
@@ -16,14 +19,30 @@ impl Plugin for FighterPlugin {
|
||||
state_interrupt_system::<states::wait::WaitState>,
|
||||
state_interrupt_system::<states::walk::WalkState>,
|
||||
state_interrupt_system::<states::dash::DashState>,
|
||||
state_interrupt_system::<states::run::RunState>,
|
||||
states::wait::wait_update,
|
||||
states::walk::walk_update,
|
||||
states::dash::dash_update,
|
||||
states::run::run_update,
|
||||
integrate_gravity,
|
||||
apply_motion,
|
||||
collide_fighter_with_scene,
|
||||
)
|
||||
.chain(),
|
||||
)
|
||||
.add_observer(states::dash::on_insert_dash);
|
||||
.add_observer(states::dash::on_insert_dash)
|
||||
.add_systems(EguiPrimaryContextPass, fighter_debug);
|
||||
}
|
||||
}
|
||||
|
||||
fn fighter_debug(mut contexts: EguiContexts, query: Query<(&FighterVelocity, &FighterInput, &StateNameDebug)>) -> Result {
|
||||
for (vel, input, name) in query {
|
||||
egui::Window::new("Hello").show(contexts.ctx_mut()?, |ui| {
|
||||
ui.label(format!("State: {}", name.0));
|
||||
ui.label(format!("{:?}", vel));
|
||||
ui.label(format!("{:?}", input.get_last_frame()));
|
||||
});
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
+23
-2
@@ -1,11 +1,12 @@
|
||||
use bevy::{ecs::relationship::RelationshipSourceCollection, prelude::*};
|
||||
use bevy::{prelude::*};
|
||||
|
||||
use crate::{fighter::{FighterAttributes, states}, game_settings::GameSettings, input::player::FighterInput};
|
||||
|
||||
pub enum FighterStateTransition {
|
||||
Wait,
|
||||
Walk,
|
||||
Dash
|
||||
Dash,
|
||||
Run,
|
||||
}
|
||||
|
||||
pub trait FighterState : Component + Sized {
|
||||
@@ -33,9 +34,28 @@ fn create_state(transition: FighterStateTransition, state_context: FighterStateC
|
||||
FighterStateTransition::Wait => ent_cmd.insert(states::wait::WaitState),
|
||||
FighterStateTransition::Walk => ent_cmd.insert(states::walk::WalkState),
|
||||
FighterStateTransition::Dash => ent_cmd.insert(states::dash::DashState::default()),
|
||||
FighterStateTransition::Run => ent_cmd.insert(states::run::RunState),
|
||||
};
|
||||
}
|
||||
|
||||
fn get_state_debug_name(transition: &FighterStateTransition) -> &'static str {
|
||||
match transition {
|
||||
FighterStateTransition::Wait => "wait",
|
||||
FighterStateTransition::Walk => "walk",
|
||||
FighterStateTransition::Dash => "dash",
|
||||
FighterStateTransition::Run => "run",
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct StateNameDebug(pub &'static str);
|
||||
|
||||
impl std::fmt::Display for StateNameDebug {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{}", self.0)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn state_interrupt_system<T: FighterState + Component + std::fmt::Debug>(
|
||||
query: Query<(Entity, &T, &mut FighterInput, &FighterAttributes)>,
|
||||
game_settings: Res<GameSettings>,
|
||||
@@ -51,6 +71,7 @@ pub fn state_interrupt_system<T: FighterState + Component + std::fmt::Debug>(
|
||||
};
|
||||
if let Some(new_state) = state.check_interrupt(&state_context) {
|
||||
commands.entity(ent).remove::<T>();
|
||||
commands.entity(ent).insert(StateNameDebug(get_state_debug_name(&new_state)));
|
||||
create_state(new_state, state_context, &mut commands);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
pub mod wait;
|
||||
pub mod walk;
|
||||
pub mod dash;
|
||||
pub mod run;
|
||||
pub mod grounded;
|
||||
@@ -1,5 +1,10 @@
|
||||
use crate::{
|
||||
fighter::state::{FighterState, FighterStateContext, FighterStateTransition},
|
||||
fighter::{
|
||||
FighterAttributes, FighterVelocity,
|
||||
state::{FighterState, FighterStateContext, FighterStateTransition},
|
||||
states::grounded,
|
||||
},
|
||||
game_settings::GameSettings,
|
||||
input::player::FighterInput,
|
||||
};
|
||||
use bevy::prelude::*;
|
||||
@@ -24,7 +29,8 @@ impl FighterState for DashState {
|
||||
) -> Option<FighterStateTransition> {
|
||||
if self.frames_in_dash > state_context.fighter_attribs.dash_duration {
|
||||
// Dash finish shenanigans
|
||||
super::walk::check_input(state_context)
|
||||
super::run::check_input(state_context)
|
||||
.or_else(|| super::walk::check_input(state_context))
|
||||
.or_else(|| super::wait::check_input(state_context))
|
||||
} else if let Some(last_frame) = state_context.input.get_last_frame() {
|
||||
check_smash_input_with_dir(state_context)
|
||||
@@ -57,9 +63,29 @@ fn check_smash_input_with_dir(state_context: &FighterStateContext) -> Option<f32
|
||||
)
|
||||
}
|
||||
|
||||
pub fn dash_update(dash_query: Query<&mut DashState>) {
|
||||
for mut dash in dash_query {
|
||||
pub fn dash_update(
|
||||
dash_query: Query<(
|
||||
&mut DashState,
|
||||
&FighterAttributes,
|
||||
&FighterInput,
|
||||
&mut FighterVelocity,
|
||||
)>,
|
||||
game_settings: Res<GameSettings>,
|
||||
) {
|
||||
for (mut dash, attributes, input, mut velocity) in dash_query {
|
||||
dash.frames_in_dash += 1;
|
||||
|
||||
if let Some(last_frame) = input.get_last_frame() {
|
||||
let (accel, target_vel) = attributes.get_accel_and_target_dashrun(last_frame);
|
||||
let accel = grounded::compute_ground_accel(
|
||||
accel,
|
||||
target_vel,
|
||||
velocity.x,
|
||||
attributes,
|
||||
game_settings.as_ref(),
|
||||
);
|
||||
velocity.x += accel;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,10 +95,21 @@ pub fn check_input(state_context: &FighterStateContext) -> Option<FighterStateTr
|
||||
|
||||
pub fn on_insert_dash(
|
||||
add: On<Insert, DashState>,
|
||||
query: Query<(Entity, &mut FighterInput), With<DashState>>,
|
||||
query: Query<
|
||||
(
|
||||
Entity,
|
||||
&mut FighterInput,
|
||||
&FighterAttributes,
|
||||
&mut FighterVelocity,
|
||||
),
|
||||
With<DashState>,
|
||||
>,
|
||||
) {
|
||||
for (entity, mut input) in query {
|
||||
for (entity, mut input, attribs, mut velocity) in query {
|
||||
if entity == add.entity {
|
||||
if let Some(last_frame) = input.get_last_frame() {
|
||||
velocity.x = attribs.dash_initial_velocity * last_frame.movement.x.signum();
|
||||
}
|
||||
input.clear_buffer();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
use bevy::log::info;
|
||||
|
||||
use crate::{fighter::FighterAttributes, game_settings::GameSettings, input::player::InputFrame};
|
||||
|
||||
pub fn apply_grounded_friction(friction: f32, ground_vel: f32) -> f32 {
|
||||
if friction.abs() > ground_vel.abs() {
|
||||
-ground_vel
|
||||
} else {
|
||||
(-ground_vel.signum()) * friction.abs()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn move_accelerate() {
|
||||
|
||||
}
|
||||
|
||||
pub fn compute_ground_accel(accel: f32, target_vel: f32, gr_vel: f32, attribs: &FighterAttributes, game_settings: &GameSettings) -> f32 {
|
||||
if target_vel == 0.0 {
|
||||
apply_grounded_friction(attribs.ground_friction, gr_vel)
|
||||
} else {
|
||||
let mut accel = accel;
|
||||
let ground_max_horizontal_velocity =
|
||||
game_settings.figher_common.ground_max_horizontal_velocity;
|
||||
if !(gr_vel * accel < 0.0) {
|
||||
// accelerating, not reversing
|
||||
if accel > 0.0 {
|
||||
if gr_vel + accel > target_vel {
|
||||
accel = -attribs.ground_friction;
|
||||
if gr_vel + accel < target_vel {
|
||||
accel = target_vel - gr_vel;
|
||||
}
|
||||
if gr_vel + accel > ground_max_horizontal_velocity {
|
||||
accel = ground_max_horizontal_velocity - gr_vel;
|
||||
}
|
||||
}
|
||||
} else if gr_vel + accel < target_vel {
|
||||
accel = attribs.ground_friction;
|
||||
if gr_vel + accel > target_vel {
|
||||
accel = target_vel - gr_vel;
|
||||
}
|
||||
if gr_vel + accel < -ground_max_horizontal_velocity {
|
||||
accel = -ground_max_horizontal_velocity - gr_vel;
|
||||
}
|
||||
}
|
||||
}
|
||||
accel
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl FighterAttributes {
|
||||
pub fn get_accel_and_target_dashrun(&self, input: &InputFrame) -> (f32, f32) {
|
||||
let accel = input.movement.x * self.stick_dash_accel;
|
||||
let accel = accel + input.movement.x.signum() * self.base_dash_accel;
|
||||
let target_vel = input.movement.x * self.max_dash_vel;
|
||||
|
||||
info!("accel {:?} target_vel {:?}", accel, target_vel);
|
||||
|
||||
(accel, target_vel)
|
||||
}
|
||||
|
||||
pub fn get_accel_and_target_walk(&self, input: &InputFrame) -> (f32, f32) {
|
||||
let accel = input.movement.x * self.stick_walk_accel;
|
||||
let accel = accel + input.movement.x.signum() * self.base_walk_accel;
|
||||
let target_vel = input.movement.x * self.max_walk_vel;
|
||||
|
||||
(accel, target_vel)
|
||||
}
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
pub mod wait;
|
||||
pub mod walk;
|
||||
pub mod dash;
|
||||
@@ -0,0 +1,44 @@
|
||||
use crate::{
|
||||
fighter::{FighterAttributes, FighterVelocity, state::{FighterStateContext, FighterStateTransition}, states::grounded}, game_settings::GameSettings, input::player::FighterInput, state::FighterState,
|
||||
};
|
||||
use bevy::prelude::*;
|
||||
|
||||
#[derive(Component, Debug)]
|
||||
pub struct RunState;
|
||||
|
||||
pub fn check_input(state_context: &FighterStateContext) -> Option<FighterStateTransition> {
|
||||
if let Some(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(FighterStateTransition::Run);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
impl FighterState for RunState {
|
||||
fn check_interrupt(
|
||||
&self,
|
||||
state_context: &crate::fighter::state::FighterStateContext,
|
||||
) -> Option<crate::fighter::state::FighterStateTransition> {
|
||||
use super::*;
|
||||
|
||||
if let Some(_) = check_input(state_context) {
|
||||
None
|
||||
} else {
|
||||
walk::check_input(state_context).or_else(|| wait::check_input(state_context))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run_update(
|
||||
fighters: Query<(&mut FighterVelocity, &FighterAttributes, &FighterInput), With<RunState>>,
|
||||
game_settings: Res<GameSettings>,
|
||||
) {
|
||||
for ( mut velocity, attributes, input) in fighters {
|
||||
if let Some(last_frame) = input.get_last_frame() {
|
||||
let (accel, target_vel) = attributes.get_accel_and_target_dashrun(last_frame);
|
||||
let accel = grounded::compute_ground_accel(accel, target_vel, velocity.x, attributes, game_settings.as_ref());
|
||||
velocity.x += accel;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
use bevy::prelude::*;
|
||||
use crate::fighter::{state::{FighterState, FighterStateContext, FighterStateTransition}, states::walk};
|
||||
use crate::{fighter::{state::{FighterState, FighterStateContext, FighterStateTransition}, states::{grounded, walk}}, game_settings::GameSettings};
|
||||
use crate::fighter::*;
|
||||
|
||||
#[derive(Component, Debug)]
|
||||
pub struct WaitState;
|
||||
@@ -20,4 +20,15 @@ pub fn check_input(state_context: &FighterStateContext) -> Option<FighterStateTr
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub fn wait_update(query: Query<(&FighterAttributes, &mut FighterVelocity), With<WaitState>>, game_settings: Res<GameSettings>) {
|
||||
for (attribs, mut velocity) in query {
|
||||
let friction = if velocity.x.abs() > attribs.max_walk_vel {
|
||||
attribs.ground_friction * game_settings.figher_common.ground_friction_over_walk_speed_multiplier
|
||||
} else {
|
||||
attribs.ground_friction
|
||||
};
|
||||
velocity.x += grounded::apply_grounded_friction(friction, velocity.x);
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,7 @@
|
||||
use crate::{
|
||||
fighter::{
|
||||
FighterAttributes, FighterVelocity,
|
||||
state::{FighterState, FighterStateContext, FighterStateTransition},
|
||||
states::{dash, wait},
|
||||
},
|
||||
game_settings::GameSettings,
|
||||
input::player::FighterInput,
|
||||
FighterAttributes, FighterVelocity, state::{FighterState, FighterStateContext, FighterStateTransition}, states::{dash, grounded, wait},
|
||||
}, game_settings::GameSettings, input::player::FighterInput,
|
||||
};
|
||||
use bevy::prelude::*;
|
||||
|
||||
@@ -31,53 +27,9 @@ pub fn walk_update(
|
||||
) {
|
||||
for (mut velocity, attributes, input) in fighters {
|
||||
if let Some(last_frame) = input.get_last_frame() {
|
||||
let accel_mul = 1.0;
|
||||
let mut accel = last_frame.movement.x * attributes.stick_walk_accel * accel_mul; // stick-proportional
|
||||
accel += (last_frame.movement.x.signum() * attributes.base_walk_accel) * accel_mul; // constant
|
||||
let target_vel = last_frame.movement.x * attributes.max_walk_vel * accel_mul;
|
||||
let (accel, target_vel) = attributes.get_accel_and_target_walk(last_frame);
|
||||
|
||||
if target_vel != 0.0 {
|
||||
let mult = velocity.x / target_vel;
|
||||
if mult > 0.0 && mult < 1.0 {
|
||||
accel *= (1.0 - mult) * game_settings.figher_common.walk_speed_ease; // 0.5 — linear ease-out toward target
|
||||
}
|
||||
}
|
||||
|
||||
// Friction mode
|
||||
if target_vel == 0.0 {
|
||||
let mut out_friction = velocity.x.signum() * (-attributes.ground_friction);
|
||||
// apply friction
|
||||
if attributes.ground_friction.abs() > velocity.x.abs() {
|
||||
out_friction = -velocity.x;
|
||||
}
|
||||
|
||||
accel = out_friction;
|
||||
} else {
|
||||
let ground_max_horizontal_velocity =
|
||||
game_settings.figher_common.ground_max_horizontal_velocity;
|
||||
if !(velocity.x * accel < 0.0) {
|
||||
// accelerating, not reversing
|
||||
if accel > 0.0 {
|
||||
if velocity.x + accel > target_vel {
|
||||
accel = -attributes.ground_friction;
|
||||
if velocity.x + accel < target_vel {
|
||||
accel = target_vel - velocity.x;
|
||||
}
|
||||
if velocity.x + accel > ground_max_horizontal_velocity {
|
||||
accel = ground_max_horizontal_velocity - velocity.x;
|
||||
}
|
||||
}
|
||||
} else if velocity.x + accel < target_vel {
|
||||
accel = attributes.ground_friction;
|
||||
if velocity.x + accel > target_vel {
|
||||
accel = target_vel - velocity.x;
|
||||
}
|
||||
if velocity.x + accel < -ground_max_horizontal_velocity {
|
||||
accel = -ground_max_horizontal_velocity - velocity.x;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let accel = grounded::compute_ground_accel(accel, target_vel, velocity.x, attributes, &game_settings);
|
||||
|
||||
velocity.x += accel;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user