Start integrating asset loading and animations
This commit is contained in:
+57
-15
@@ -1,16 +1,23 @@
|
||||
use std::{collections::HashMap, time::Duration};
|
||||
|
||||
use bevy::{mesh::skinning::SkinnedMesh, prelude::*};
|
||||
|
||||
use crate::{fighter::{FighterECB, FighterTranslation, FighterVisual}, player::Player};
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct FighterAnimations {
|
||||
pub wait: Handle<AnimationClip>
|
||||
}
|
||||
use crate::{
|
||||
fighter::{FighterECB, FighterTranslation, FighterVisual, visual::FighterAnimations}, player::Player,
|
||||
};
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct FighterAnimationPlayerLink(Entity);
|
||||
|
||||
pub fn apply_fighter_translation_to_visuals(query: Query<(&FighterTranslation, &FighterECB, &mut Transform)>) {
|
||||
impl FighterAnimationPlayerLink {
|
||||
pub fn player(&self) -> Entity {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
pub fn apply_fighter_translation_to_visuals(
|
||||
query: Query<(&FighterTranslation, &FighterECB, &mut Transform)>,
|
||||
) {
|
||||
for (translation, ecb, mut transform) in query {
|
||||
let translation = translation.0 + ecb.get_bottom_point();
|
||||
transform.translation = Vec3::new(translation.x.to_num(), translation.y.to_num(), 0.0);
|
||||
@@ -31,18 +38,53 @@ pub fn setup_fighter_animation_player(
|
||||
else {
|
||||
continue;
|
||||
};
|
||||
commands.entity(parent).insert(
|
||||
FighterAnimationPlayerLink(child)
|
||||
);
|
||||
|
||||
commands.entity(child).insert(AnimationTransitions::new());
|
||||
info!("LINK");
|
||||
commands
|
||||
.entity(parent)
|
||||
.insert(FighterAnimationPlayerLink(child));
|
||||
}
|
||||
}
|
||||
|
||||
pub fn apply_animation(
|
||||
children: Query<&mut Transform, With<FighterVisual>>,
|
||||
) {
|
||||
pub fn apply_animation(children: Query<&mut Transform, With<FighterVisual>>) {
|
||||
for mut trf in children {
|
||||
// Melee model scale is in decimeters
|
||||
trf.scale = Vec3::splat(0.1);
|
||||
trf.rotation = Quat::IDENTITY;
|
||||
trf.rotate_local_y(std::f32::consts::PI * 0.5);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
pub fn animation_init(
|
||||
fighters: Query<
|
||||
(
|
||||
&FighterAnimations,
|
||||
&FighterAnimationPlayerLink,
|
||||
),
|
||||
Added<FighterAnimationPlayerLink>
|
||||
>,
|
||||
mut commands: Commands,
|
||||
mut anim_player_query: Query<(&mut AnimationPlayer, &mut AnimationTransitions)>
|
||||
) {
|
||||
for (anim, link) in fighters {
|
||||
commands.entity(link.0).insert(
|
||||
AnimationGraphHandle(anim.graph.clone())
|
||||
);
|
||||
info!("BEGIN! {:?}", fighters);
|
||||
if let Ok((mut player, mut transitions)) = anim_player_query.get_mut(link.0) {
|
||||
info!("DOS");
|
||||
transitions.play(&mut player, anim.clips[&AnimKind::Wait], Duration::ZERO).repeat();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Deserialize)]
|
||||
pub enum AnimKind {
|
||||
Wait,
|
||||
Walk,
|
||||
Run,
|
||||
Dash,
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize, bevy::asset::Asset, bevy::reflect::TypePath)]
|
||||
pub struct AnimManifest(pub HashMap<AnimKind, String>);
|
||||
|
||||
@@ -11,6 +11,7 @@ pub struct FighterAttributes {
|
||||
pub stick_walk_accel: FGi32,
|
||||
pub max_walk_vel: FGi32,
|
||||
|
||||
pub dash_acceleration_duration: u32,
|
||||
pub dash_duration: u32,
|
||||
pub dash_initial_velocity: FGi32,
|
||||
pub base_dash_accel: FGi32,
|
||||
@@ -26,11 +27,13 @@ pub struct FighterAttributes {
|
||||
}
|
||||
|
||||
impl FighterAttributes {
|
||||
pub fn get_accel_and_target_dashrun(&self, input: &FighterInputFrame) -> (FGi32, FGi32) {
|
||||
pub fn get_accel_and_target_dashrun(&self, input: &FighterInputFrame, direction: FGi32) -> (FGi32, FGi32) {
|
||||
let accel = input.movement.x * self.stick_dash_accel;
|
||||
let accel = accel + input.movement.x.signum() * self.base_dash_accel;
|
||||
let accel = accel + direction * self.base_dash_accel;
|
||||
let target_vel = input.movement.x * self.max_dash_vel;
|
||||
|
||||
info!("ACCEL {}", accel);
|
||||
|
||||
(accel, target_vel)
|
||||
}
|
||||
|
||||
|
||||
+105
-52
@@ -1,7 +1,7 @@
|
||||
//! Everything fighter-shaped: the components that describe a fighter, the
|
||||
//! state machine that drives it, and the plugin that schedules it all.
|
||||
|
||||
use bevy::prelude::*;
|
||||
use bevy::{ecs::name::HashedStr, prelude::*};
|
||||
use bevy_egui::EguiPrimaryContextPass;
|
||||
use bevy_ggrs::prelude::*;
|
||||
|
||||
@@ -12,6 +12,7 @@ pub mod debug;
|
||||
pub mod ecb;
|
||||
pub mod motion;
|
||||
pub mod state;
|
||||
pub mod visual;
|
||||
|
||||
// The fighter components are re-exported so the rest of the crate can say
|
||||
// `fighter::FighterVelocity` without caring which file it lives in. Named
|
||||
@@ -21,7 +22,18 @@ pub use attributes::FighterAttributes;
|
||||
pub use ecb::FighterECB;
|
||||
pub use motion::{FighterPreviousTranslation, FighterTranslation, FighterVelocity, Grounded};
|
||||
|
||||
use crate::{fighter::state::FighterState, schedule::GameplaySet, stage::line::StageCollision};
|
||||
use crate::{
|
||||
fighter::{
|
||||
animation::animation_init,
|
||||
state::{FighterState, fall::FallState},
|
||||
visual::FighterAnimations,
|
||||
},
|
||||
input::FighterInput,
|
||||
math::{int::FGi32, vec::FGVec2},
|
||||
player::Player,
|
||||
schedule::GameplaySet,
|
||||
stage::line::StageCollision,
|
||||
};
|
||||
use state::state_interrupt_system;
|
||||
|
||||
/// Owns fighter simulation, the rollback registrations for fighter
|
||||
@@ -32,7 +44,7 @@ pub struct FighterPlugin;
|
||||
#[derive(Component, Copy, Clone, Hash)]
|
||||
pub enum FighterFacingDirection {
|
||||
Left,
|
||||
Right
|
||||
Right,
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
@@ -41,56 +53,97 @@ pub struct FighterVisual;
|
||||
|
||||
impl Plugin for FighterPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app
|
||||
// Every state gets a chance to interrupt itself, in priority order.
|
||||
.add_systems(
|
||||
GgrsSchedule,
|
||||
(
|
||||
ecb::snapshot_fighter_ecb.before(state_interrupt_system),
|
||||
animation::setup_fighter_animation_player,
|
||||
state::state_interrupt_system,
|
||||
)
|
||||
.in_set(GameplaySet::Interrupt),
|
||||
app.add_systems(
|
||||
Update,
|
||||
(animation_init, animation::setup_fighter_animation_player),
|
||||
)
|
||||
// Every state gets a chance to interrupt itself, in priority order.
|
||||
.add_systems(
|
||||
GgrsSchedule,
|
||||
(
|
||||
ecb::snapshot_fighter_ecb.before(state_interrupt_system),
|
||||
state::state_interrupt_system,
|
||||
)
|
||||
.add_systems(
|
||||
GgrsSchedule,
|
||||
(state::state_update_system,).in_set(GameplaySet::StateUpdate),
|
||||
.in_set(GameplaySet::Interrupt),
|
||||
)
|
||||
.add_systems(
|
||||
GgrsSchedule,
|
||||
(state::state_update_system,).in_set(GameplaySet::StateUpdate),
|
||||
)
|
||||
/*.add_systems(
|
||||
GgrsSchedule,
|
||||
(motion::integrate_gravity, motion::apply_grounded_motion)
|
||||
.chain()
|
||||
.in_set(GameplaySet::Physics),
|
||||
)*/
|
||||
.add_systems(
|
||||
GgrsSchedule,
|
||||
state::state_collision_interrupt_system.in_set(GameplaySet::Collision),
|
||||
)
|
||||
.add_systems(
|
||||
GgrsSchedule,
|
||||
(
|
||||
animation::apply_fighter_translation_to_visuals,
|
||||
animation::apply_animation,
|
||||
)
|
||||
/*.add_systems(
|
||||
GgrsSchedule,
|
||||
(motion::integrate_gravity, motion::apply_grounded_motion)
|
||||
.chain()
|
||||
.in_set(GameplaySet::Physics),
|
||||
)*/
|
||||
.add_systems(
|
||||
GgrsSchedule,
|
||||
state::state_collision_interrupt_system.in_set(GameplaySet::Collision),
|
||||
)
|
||||
.add_systems(
|
||||
GgrsSchedule,
|
||||
(
|
||||
animation::apply_fighter_translation_to_visuals,
|
||||
animation::apply_animation,
|
||||
)
|
||||
.chain()
|
||||
.in_set(GameplaySet::Animation),
|
||||
)
|
||||
// Rollback registration lives next to the systems that write these
|
||||
// components; a component simulated here but missing from this list
|
||||
// is a desync waiting to happen.
|
||||
.rollback_component_with_copy::<FighterECB>()
|
||||
.rollback_component_with_copy::<ecb::FighterPreviousECB>()
|
||||
.rollback_component_with_copy::<FighterVelocity>()
|
||||
.rollback_component_with_copy::<FighterTranslation>()
|
||||
.rollback_component_with_copy::<FighterPreviousTranslation>()
|
||||
.rollback_component_with_copy::<FighterAttributes>()
|
||||
.rollback_component_with_copy::<Grounded>()
|
||||
.rollback_resource_with_reflect::<StageCollision>()
|
||||
// Debug views.
|
||||
.add_systems(FixedPostUpdate, debug::debug_draw_ecb)
|
||||
.add_systems(EguiPrimaryContextPass, debug::fighter_debug)
|
||||
.add_systems(EguiPrimaryContextPass, debug::update_config)
|
||||
.rollback_component_with_clone::<FighterState>()
|
||||
.checksum_component_with_hash::<FighterState>();
|
||||
.chain()
|
||||
.in_set(GameplaySet::Animation),
|
||||
)
|
||||
// Rollback registration lives next to the systems that write these
|
||||
// components; a component simulated here but missing from this list
|
||||
// is a desync waiting to happen.
|
||||
.rollback_component_with_copy::<FighterECB>()
|
||||
.rollback_component_with_copy::<ecb::FighterPreviousECB>()
|
||||
.rollback_component_with_copy::<FighterVelocity>()
|
||||
.rollback_component_with_copy::<FighterTranslation>()
|
||||
.rollback_component_with_copy::<FighterPreviousTranslation>()
|
||||
.rollback_component_with_copy::<FighterAttributes>()
|
||||
.rollback_component_with_copy::<Grounded>()
|
||||
.rollback_resource_with_reflect::<StageCollision>()
|
||||
// Debug views.
|
||||
.add_systems(FixedPostUpdate, debug::debug_draw_ecb)
|
||||
.add_systems(EguiPrimaryContextPass, debug::fighter_debug)
|
||||
.add_systems(EguiPrimaryContextPass, debug::update_config)
|
||||
.rollback_component_with_clone::<FighterState>()
|
||||
.checksum_component_with_hash::<FighterState>();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn spawn_fighter(
|
||||
commands: &mut Commands,
|
||||
animations: FighterAnimations,
|
||||
visual_root: WorldAssetRoot,
|
||||
) {
|
||||
commands.spawn((
|
||||
Name("Fighter".into()),
|
||||
Player { handle: 0 },
|
||||
FighterFacingDirection::Right,
|
||||
FighterECB {
|
||||
vertical_half: FGi32::lit("5.0"),
|
||||
horizontal_half: FGi32::lit("2.5"),
|
||||
},
|
||||
FighterVelocity::default(),
|
||||
FighterTranslation(FGVec2::lit("5.0", "12.5")),
|
||||
FighterAttributes {
|
||||
base_walk_accel: FGi32::lit("0.1"),
|
||||
stick_walk_accel: FGi32::lit("0.16"),
|
||||
max_walk_vel: FGi32::lit("0.7"),
|
||||
ground_friction: FGi32::lit("0.09"),
|
||||
dash_acceleration_duration: 13,
|
||||
dash_duration: 24,
|
||||
base_dash_accel: FGi32::lit("0.02"),
|
||||
stick_dash_accel: FGi32::lit("0.065"),
|
||||
max_dash_vel: FGi32::lit("1.1"),
|
||||
jump_vertical_velocity: FGi32::lit("2.5"),
|
||||
dash_initial_velocity: FGi32::lit("1.4"),
|
||||
terminal_velocity: FGi32::lit("2.8"),
|
||||
gravity: FGi32::lit("0.23"),
|
||||
jumpsquat_duration: 4,
|
||||
},
|
||||
visual_root,
|
||||
animations,
|
||||
FighterState::Fall(FallState),
|
||||
FighterVisual,
|
||||
FighterInput::default(),
|
||||
));
|
||||
}
|
||||
|
||||
+24
-12
@@ -1,9 +1,12 @@
|
||||
use std::hash::{Hash, Hasher};
|
||||
use std::time::Duration;
|
||||
|
||||
use bevy::prelude::*;
|
||||
|
||||
use super::{FighterStateImpl, FighterStateContext, ground, run, wait, walk};
|
||||
use crate::fighter::state::ground::GroundedStateCommon;
|
||||
use crate::fighter::animation::AnimKind;
|
||||
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};
|
||||
@@ -45,10 +48,13 @@ impl FighterStateImpl for DashState {
|
||||
state_context: &FighterStateContext,
|
||||
) -> Option<FighterState> {
|
||||
if self.frames_in_dash > state_context.fighter_attribs.dash_duration {
|
||||
// Dash finish shenanigans
|
||||
run::check_input(state_context, &self.ground_common)
|
||||
.or_else(|| walk::check_input(state_context, &self.ground_common))
|
||||
.or_else(|| wait::check_input(state_context, &self.ground_common))
|
||||
} else if self.frames_in_dash > state_context.fighter_attribs.dash_acceleration_duration {
|
||||
// Dash finish shenanigans
|
||||
run::check_input(state_context, &self.ground_common)
|
||||
.or_else(|| walk::check_input(state_context, &self.ground_common))
|
||||
} else {
|
||||
check_smash_input_with_dir(state_context)
|
||||
.filter(|dir| *dir != self.direction)
|
||||
@@ -59,13 +65,21 @@ impl FighterStateImpl for DashState {
|
||||
fn on_enter(&mut self, state_context: &mut FighterStateContext) {
|
||||
state_context.input.clear_buffer();
|
||||
state_context.velocity.x = state_context.fighter_attribs.dash_initial_velocity * self.direction;
|
||||
|
||||
state_context.animation_transitions.play(
|
||||
&mut state_context.animation_player,
|
||||
state_context.animations.clips[&AnimKind::Dash],
|
||||
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);
|
||||
let (accel, target_vel) = state_context.fighter_attribs.get_accel_and_target_dashrun(&last_frame, self.direction);
|
||||
let accel = ground::compute_ground_accel(
|
||||
accel,
|
||||
target_vel,
|
||||
@@ -86,13 +100,12 @@ impl FighterStateImpl for DashState {
|
||||
&mut self,
|
||||
state_context: &mut FighterStateContext,
|
||||
) -> Option<FighterState> {
|
||||
if let Some(res) = collision::air_collide_with_stage(state_context) {
|
||||
// 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
|
||||
};
|
||||
Some(FighterState::Wait(WaitState { grounded_common } ))
|
||||
if let GroundedMotionResult::InAir = ground::collide_with_stage_grounded(
|
||||
state_context,
|
||||
&mut self.ground_common,
|
||||
true
|
||||
) {
|
||||
Some(FighterState::Fall(FallState))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
@@ -110,6 +123,5 @@ fn check_smash_input_with_dir(state_context: &FighterStateContext) -> Option<FGi
|
||||
}
|
||||
|
||||
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())));
|
||||
None
|
||||
check_smash_input_with_dir(state_context).map(|dir| FighterState::Dash(DashState::new(dir, ground_common.clone())))
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
//! ECS types so it stays cheap to reason about and to unit-test — it has to be
|
||||
//! bit-for-bit deterministic for rollback.
|
||||
|
||||
use crate::fighter::state::FighterStateContext;
|
||||
use crate::fighter::state::{FighterState, FighterStateContext};
|
||||
use crate::fighter::{
|
||||
FighterAttributes, FighterECB, FighterPreviousTranslation, FighterTranslation, FighterVelocity,
|
||||
};
|
||||
@@ -10,7 +10,9 @@ use crate::game_settings::GameSettings;
|
||||
use crate::math::int::FGi32;
|
||||
use crate::math::vec::FGVec2;
|
||||
use crate::stage::StagePoly;
|
||||
use crate::stage::line::{StageCollision, StageLineID, StagePolyLineSegment, StagePolyLineSegmentType};
|
||||
use crate::stage::line::{
|
||||
StageCollision, StageLineID, StagePolyLineSegment, StagePolyLineSegmentType,
|
||||
};
|
||||
|
||||
use bevy::prelude::*;
|
||||
use fixed::traits::Fixed;
|
||||
@@ -37,7 +39,7 @@ pub fn compute_ground_accel(
|
||||
attribs: &FighterAttributes,
|
||||
game_settings: &GameSettings,
|
||||
) -> FGi32 {
|
||||
if target_vel == 0.0 {
|
||||
if target_vel == FGi32::ZERO {
|
||||
apply_grounded_friction(attribs.ground_friction, gr_vel)
|
||||
} else {
|
||||
let mut accel = accel;
|
||||
@@ -81,13 +83,13 @@ pub fn apply_grounded_motion(
|
||||
) {
|
||||
// Vertical velocity should be 0 on the ground
|
||||
prev_translation.0 = translation.0;
|
||||
translation.0 += FGVec2::new(velocity.0.x ,FGi32::ZERO);
|
||||
translation.0 += FGVec2::new(velocity.0.x, FGi32::ZERO);
|
||||
}
|
||||
|
||||
pub fn collide_with_stage_grounded(
|
||||
state_context: &mut FighterStateContext,
|
||||
ground_common: &mut GroundedStateCommon,
|
||||
can_walk_off: bool
|
||||
can_walk_off: bool,
|
||||
) -> GroundedMotionResult {
|
||||
let translation = &mut state_context.translation;
|
||||
let ecb = &state_context.ecb;
|
||||
@@ -116,7 +118,8 @@ pub fn collide_with_stage_grounded(
|
||||
}
|
||||
|
||||
let line_dir = segment.segment.direction();
|
||||
let projected_point = segment.segment.point1() + (ecb_bottom_pos - segment.segment.point1()).project_onto(line_dir);
|
||||
let projected_point = segment.segment.point1()
|
||||
+ (ecb_bottom_pos - segment.segment.point1()).project_onto(line_dir);
|
||||
let closest_point = segment.segment.closest_point(ecb_bottom_pos);
|
||||
let distance = closest_point.distance(projected_point);
|
||||
|
||||
@@ -129,7 +132,8 @@ pub fn collide_with_stage_grounded(
|
||||
|
||||
if let Some((bp, new_segment_idx, normal)) = grounded_ecb_bottom_pos {
|
||||
translation.0 = bp - ecb.get_bottom_point();
|
||||
state_context.velocity.0 = state_context.velocity.0 - state_context.velocity.0.project_onto_normalized(normal);
|
||||
state_context.velocity.0 =
|
||||
state_context.velocity.0 - state_context.velocity.0.project_onto_normalized(normal);
|
||||
ground_common.current_line_id.segment = new_segment_idx;
|
||||
GroundedMotionResult::InGround
|
||||
} else {
|
||||
@@ -139,3 +143,10 @@ pub fn collide_with_stage_grounded(
|
||||
GroundedMotionResult::InAir
|
||||
}
|
||||
}
|
||||
|
||||
pub fn grounded_movement_common_interrupts(
|
||||
state_context: &FighterStateContext,
|
||||
ground_common: &GroundedStateCommon
|
||||
) -> Option<FighterState> {
|
||||
super::jump::check_input(state_context, ground_common)
|
||||
}
|
||||
|
||||
@@ -99,8 +99,7 @@ pub fn check_input(
|
||||
state_context
|
||||
.input
|
||||
.has_command(FighterCommands::Jump)
|
||||
.then_some(FighterState::JumpSquat(JumpSquatState::create(
|
||||
.then(|| FighterState::JumpSquat(JumpSquatState::create(
|
||||
ground_common.clone(),
|
||||
)));
|
||||
None
|
||||
)))
|
||||
}
|
||||
|
||||
+143
-70
@@ -2,28 +2,27 @@
|
||||
//! implements, the context they inspect, and the generic system that runs the
|
||||
//! interrupt check. One state per submodule.
|
||||
|
||||
use bevy::ecs::component::Mutable;
|
||||
use bevy::ecs::system::ScheduleSystem;
|
||||
use bevy::ecs::query::QueryData;
|
||||
use bevy::prelude::*;
|
||||
use bevy_ggrs::{GgrsSchedule, RollbackApp};
|
||||
|
||||
use crate::fighter::animation::FighterAnimationPlayerLink;
|
||||
use crate::fighter::ecb::FighterPreviousECB;
|
||||
use crate::fighter::{FighterAttributes, FighterECB, FighterPreviousTranslation, FighterTranslation, FighterVelocity};
|
||||
use crate::fighter::visual::FighterAnimations;
|
||||
use crate::fighter::{
|
||||
FighterAttributes, FighterECB, FighterPreviousTranslation, FighterTranslation, FighterVelocity,
|
||||
};
|
||||
use crate::game_settings::GameSettings;
|
||||
use crate::input::FighterInput;
|
||||
use crate::math::int::FGi32;
|
||||
use crate::schedule::GameplaySet;
|
||||
use crate::stage::StagePoly;
|
||||
use crate::stage::line::StageCollision;
|
||||
|
||||
pub mod air;
|
||||
pub mod dash;
|
||||
pub mod fall;
|
||||
pub mod ground;
|
||||
pub mod jump;
|
||||
pub mod run;
|
||||
pub mod wait;
|
||||
pub mod walk;
|
||||
pub mod fall;
|
||||
pub mod air;
|
||||
pub mod jump;
|
||||
|
||||
macro_rules! fighter_states {
|
||||
($($variant:ident => $ty:ty),* $(,)?) => {
|
||||
@@ -37,6 +36,10 @@ macro_rules! fighter_states {
|
||||
pub fn check_interrupt(&self, ctx: &FighterStateContext) -> Option<FighterState> {
|
||||
match self { $(Self::$variant(s) => s.check_interrupt(ctx)),* }
|
||||
}
|
||||
|
||||
pub fn play_animation(&self, transitions: &mut AnimationTransitions, player: &mut AnimationPlayer, anims: &FighterAnimations) {
|
||||
match self { $(Self::$variant(s) => s.play_animation(transitions, player, anims)),* }
|
||||
}
|
||||
pub fn update(&mut self, ctx: &mut FighterStateContext) {
|
||||
match self { $(Self::$variant(s) => s.update(ctx)),* }
|
||||
}
|
||||
@@ -57,11 +60,15 @@ macro_rules! fighter_states {
|
||||
|
||||
pub trait FighterStateImpl: Sized {
|
||||
const NAME: &'static str;
|
||||
fn check_interrupt(
|
||||
&self,
|
||||
state_context: &FighterStateContext,
|
||||
) -> Option<FighterState>;
|
||||
fn check_interrupt(&self, state_context: &FighterStateContext) -> Option<FighterState>;
|
||||
|
||||
fn play_animation(
|
||||
&self,
|
||||
_transitions: &mut AnimationTransitions,
|
||||
_player: &mut AnimationPlayer,
|
||||
_anims: &FighterAnimations,
|
||||
) {
|
||||
}
|
||||
fn on_enter(&mut self, _state_context: &mut FighterStateContext) {}
|
||||
fn update(&mut self, _state_context: &mut FighterStateContext);
|
||||
|
||||
@@ -73,7 +80,7 @@ pub trait FighterStateImpl: Sized {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct FighterStateContext<'a, 'w, 's> {
|
||||
pub struct FighterStateContext<'a> {
|
||||
pub translation: &'a mut FighterTranslation,
|
||||
pub prev_translation: &'a mut FighterPreviousTranslation,
|
||||
pub input: &'a mut FighterInput,
|
||||
@@ -82,9 +89,10 @@ pub struct FighterStateContext<'a, 'w, 's> {
|
||||
pub prev_ecb: &'a mut FighterPreviousECB,
|
||||
pub fighter_attribs: &'a FighterAttributes,
|
||||
pub game_settings: &'a GameSettings,
|
||||
pub entity: Entity,
|
||||
pub stage_collision: &'a StageCollision,
|
||||
pub gizmos: Option<&'a mut Gizmos<'w, 's>>,
|
||||
pub animations: &'a FighterAnimations,
|
||||
pub animation_player: &'a mut AnimationPlayer,
|
||||
pub animation_transitions: &'a mut AnimationTransitions,
|
||||
}
|
||||
|
||||
fighter_states! {
|
||||
@@ -97,6 +105,54 @@ fighter_states! {
|
||||
Jump => jump::JumpState
|
||||
}
|
||||
|
||||
#[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 velocity: &'static mut FighterVelocity,
|
||||
pub ecb: &'static mut FighterECB,
|
||||
pub prev_ecb: &'static mut FighterPreviousECB,
|
||||
pub attributes: &'static FighterAttributes,
|
||||
}
|
||||
|
||||
impl<'w, 's> FighterFrameQueryItem<'w, 's> {
|
||||
fn context<'a>(
|
||||
&'a mut self,
|
||||
game_settings: &'a GameSettings,
|
||||
stage_collision: &'a StageCollision,
|
||||
animations: &'a FighterAnimations,
|
||||
animation_player: &'a mut AnimationPlayer,
|
||||
animation_transitions: &'a mut AnimationTransitions,
|
||||
) -> FighterStateContext<'a> {
|
||||
FighterStateContext {
|
||||
translation: &mut self.translation,
|
||||
prev_translation: &mut self.prev_translation,
|
||||
input: &mut self.input,
|
||||
velocity: &mut self.velocity,
|
||||
ecb: &mut self.ecb,
|
||||
prev_ecb: &mut self.prev_ecb,
|
||||
fighter_attribs: self.attributes,
|
||||
game_settings,
|
||||
stage_collision,
|
||||
animations,
|
||||
animation_player,
|
||||
animation_transitions,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(QueryData)]
|
||||
#[query_data(mutable)]
|
||||
pub struct StatefulFighterQuery {
|
||||
pub entity: Entity,
|
||||
pub state: &'static mut FighterState,
|
||||
pub frame: FighterFrameQuery,
|
||||
pub animations: &'static FighterAnimations,
|
||||
pub animation_player_link: &'static FighterAnimationPlayerLink,
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct StateNameDebug(pub &'static str);
|
||||
|
||||
@@ -107,93 +163,110 @@ impl std::fmt::Display for StateNameDebug {
|
||||
}
|
||||
|
||||
pub fn state_update_system(
|
||||
query: Query<(Entity, &mut FighterState, &mut FighterTranslation, &mut FighterPreviousTranslation, &mut FighterInput, &mut FighterVelocity, &mut FighterECB, &mut FighterPreviousECB, &FighterAttributes)>,
|
||||
mut query: Query<StatefulFighterQuery>,
|
||||
mut animation_players: Query<(&mut AnimationPlayer, &mut AnimationTransitions)>,
|
||||
stage_collision: Res<StageCollision>,
|
||||
game_settings: Res<GameSettings>,
|
||||
mut gizmos: Gizmos,
|
||||
) {
|
||||
for (ent, mut state, mut translation, mut prev_translation, mut input, mut velocity, mut ecb, mut prev_ecb, attribs) in query {
|
||||
let mut update_ctx = FighterStateContext {
|
||||
translation: &mut translation,
|
||||
prev_translation: &mut prev_translation,
|
||||
input: &mut input,
|
||||
velocity: &mut velocity,
|
||||
ecb: &mut ecb,
|
||||
prev_ecb: &mut prev_ecb,
|
||||
fighter_attribs: &attribs,
|
||||
game_settings: &game_settings,
|
||||
entity: ent,
|
||||
stage_collision: &stage_collision,
|
||||
gizmos: Some(&mut gizmos),
|
||||
for fighter in &mut query {
|
||||
let StatefulFighterQueryItem {
|
||||
mut state,
|
||||
mut frame,
|
||||
animations,
|
||||
animation_player_link,
|
||||
..
|
||||
} = fighter;
|
||||
let Ok((mut animation_player, mut animation_transitions)) =
|
||||
animation_players.get_mut(animation_player_link.player())
|
||||
else {
|
||||
continue;
|
||||
};
|
||||
let mut update_ctx = frame.context(
|
||||
&game_settings,
|
||||
&stage_collision,
|
||||
animations,
|
||||
&mut animation_player,
|
||||
&mut animation_transitions,
|
||||
);
|
||||
state.update(&mut update_ctx);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn state_interrupt_system (
|
||||
query: Query<(Entity, &FighterState, &mut FighterTranslation, &mut FighterPreviousTranslation, &mut FighterInput, &mut FighterVelocity, &mut FighterECB, &mut FighterPreviousECB, &FighterAttributes)>,
|
||||
pub fn state_interrupt_system(
|
||||
mut query: Query<StatefulFighterQuery>,
|
||||
mut animation_players: Query<(&mut AnimationPlayer, &mut AnimationTransitions)>,
|
||||
stage_collision: Res<StageCollision>,
|
||||
game_settings: Res<GameSettings>,
|
||||
mut commands: Commands,
|
||||
) {
|
||||
for (ent, state, mut translation, mut prev_translation, mut input, mut velocity, mut ecb, mut prev_ecb, attribs) in query {
|
||||
let mut state_context = FighterStateContext {
|
||||
translation: &mut translation,
|
||||
prev_translation: &mut prev_translation,
|
||||
input: &mut input,
|
||||
velocity: &mut velocity,
|
||||
ecb: &mut ecb,
|
||||
prev_ecb: &mut prev_ecb,
|
||||
fighter_attribs: &attribs,
|
||||
game_settings: &game_settings,
|
||||
entity: ent,
|
||||
stage_collision: &stage_collision,
|
||||
gizmos: None,
|
||||
for fighter in &mut query {
|
||||
let StatefulFighterQueryItem {
|
||||
entity,
|
||||
state,
|
||||
mut frame,
|
||||
animations,
|
||||
animation_player_link,
|
||||
} = fighter;
|
||||
let Ok((mut animation_player, mut animation_transitions)) =
|
||||
animation_players.get_mut(animation_player_link.player())
|
||||
else {
|
||||
continue;
|
||||
};
|
||||
let mut state_context = frame.context(
|
||||
&game_settings,
|
||||
&stage_collision,
|
||||
animations,
|
||||
&mut animation_player,
|
||||
&mut animation_transitions,
|
||||
);
|
||||
if let Some(new_state) = state.check_interrupt(&state_context) {
|
||||
let mut new_state = new_state;
|
||||
let old_state_name = state.name();
|
||||
let new_state_name = new_state.name();
|
||||
new_state.on_enter(&mut state_context);
|
||||
commands.entity(ent).insert((
|
||||
new_state,
|
||||
StateNameDebug(new_state_name)
|
||||
));
|
||||
commands
|
||||
.entity(entity)
|
||||
.insert((new_state, StateNameDebug(new_state_name)));
|
||||
info!("State {} -> {}", old_state_name, new_state_name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn state_collision_interrupt_system(
|
||||
query: Query<(Entity, &mut FighterState, &mut FighterTranslation, &mut FighterPreviousTranslation, &mut FighterInput, &mut FighterVelocity, &mut FighterECB, &mut FighterPreviousECB, &FighterAttributes)>,
|
||||
mut query: Query<StatefulFighterQuery>,
|
||||
mut animation_players: Query<(&mut AnimationPlayer, &mut AnimationTransitions)>,
|
||||
stage_collision: Res<StageCollision>,
|
||||
game_settings: Res<GameSettings>,
|
||||
mut commands: Commands,
|
||||
mut gizmos: Gizmos,
|
||||
) {
|
||||
for (ent, mut state, mut translation, mut prev_translation, mut input, mut velocity, mut ecb, mut prev_ecb, attribs) in query {
|
||||
let mut state_context = FighterStateContext {
|
||||
translation: &mut translation,
|
||||
prev_translation: &mut prev_translation,
|
||||
input: &mut input,
|
||||
velocity: &mut velocity,
|
||||
ecb: &mut ecb,
|
||||
prev_ecb: &mut prev_ecb,
|
||||
fighter_attribs: &attribs,
|
||||
game_settings: &game_settings,
|
||||
entity: ent,
|
||||
stage_collision: &stage_collision,
|
||||
gizmos: Some(&mut gizmos),
|
||||
for fighter in &mut query {
|
||||
let StatefulFighterQueryItem {
|
||||
entity,
|
||||
mut state,
|
||||
mut frame,
|
||||
animations,
|
||||
animation_player_link,
|
||||
} = fighter;
|
||||
let Ok((mut animation_player, mut animation_transitions)) =
|
||||
animation_players.get_mut(animation_player_link.player())
|
||||
else {
|
||||
continue;
|
||||
};
|
||||
let mut state_context = frame.context(
|
||||
&game_settings,
|
||||
&stage_collision,
|
||||
animations,
|
||||
&mut animation_player,
|
||||
&mut animation_transitions,
|
||||
);
|
||||
if let Some(new_state) = state.check_collision_interrupt(&mut state_context) {
|
||||
let mut new_state = new_state;
|
||||
let old_state_name = state.name();
|
||||
let new_state_name = new_state.name();
|
||||
new_state.on_enter(&mut state_context);
|
||||
commands.entity(ent).insert((
|
||||
new_state,
|
||||
StateNameDebug(new_state_name)
|
||||
));
|
||||
commands
|
||||
.entity(entity)
|
||||
.insert((new_state, StateNameDebug(new_state_name)));
|
||||
info!("State {} -> {}", old_state_name, new_state_name);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,16 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use bevy::prelude::*;
|
||||
|
||||
use super::{FighterStateImpl, FighterStateContext, ground, 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::{FighterAttributes, FighterVelocity};
|
||||
use crate::game_settings::GameSettings;
|
||||
use crate::input::FighterInput;
|
||||
use crate::math::int::FGi32;
|
||||
|
||||
#[derive(Component, Debug, Clone, Hash)]
|
||||
pub struct RunState {
|
||||
@@ -27,11 +31,17 @@ impl FighterStateImpl for RunState {
|
||||
}
|
||||
}
|
||||
|
||||
fn on_enter(&mut self, _state_context: &mut super::FighterStateContext) {}
|
||||
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();
|
||||
}
|
||||
|
||||
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);
|
||||
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,
|
||||
|
||||
+43
-29
@@ -1,38 +1,50 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use bevy::prelude::*;
|
||||
|
||||
use super::{FighterStateImpl, FighterStateContext, ground, walk};
|
||||
use crate::fighter::state::FighterState;
|
||||
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};
|
||||
use crate::fighter::state::ground::{
|
||||
GroundedMotionResult, GroundedStateCommon, grounded_movement_common_interrupts,
|
||||
};
|
||||
use crate::fighter::{FighterAttributes, FighterVelocity};
|
||||
use crate::game_settings::GameSettings;
|
||||
use crate::stage::line::StageLineID;
|
||||
|
||||
#[derive(Debug, Clone, Hash)]
|
||||
pub struct WaitState {
|
||||
pub grounded_common: GroundedStateCommon
|
||||
pub grounded_common: GroundedStateCommon,
|
||||
}
|
||||
|
||||
impl FighterStateImpl for WaitState {
|
||||
const NAME: &'static str = "Wait";
|
||||
fn check_interrupt(
|
||||
&self,
|
||||
state_context: &FighterStateContext,
|
||||
) -> Option<FighterState> {
|
||||
walk::check_input(state_context, &self.grounded_common)
|
||||
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)))
|
||||
}
|
||||
|
||||
fn on_enter(&mut self, _state_context: &mut super::FighterStateContext) {}
|
||||
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
fn update(&mut self, state_context: &mut super::FighterStateContext) {
|
||||
let friction = if state_context.velocity.x.abs() > state_context.fighter_attribs.max_walk_vel {
|
||||
state_context.fighter_attribs.ground_friction
|
||||
* state_context.game_settings
|
||||
.figher_common
|
||||
.ground_friction_over_walk_speed_multiplier
|
||||
} else {
|
||||
state_context.fighter_attribs.ground_friction
|
||||
};
|
||||
let friction =
|
||||
if state_context.velocity.x.abs() > state_context.fighter_attribs.max_walk_vel {
|
||||
state_context.fighter_attribs.ground_friction
|
||||
* state_context
|
||||
.game_settings
|
||||
.figher_common
|
||||
.ground_friction_over_walk_speed_multiplier
|
||||
} else {
|
||||
state_context.fighter_attribs.ground_friction
|
||||
};
|
||||
|
||||
let ground_velocity = state_context.velocity.x;
|
||||
|
||||
@@ -44,16 +56,14 @@ impl FighterStateImpl for WaitState {
|
||||
state_context.prev_translation,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
fn check_collision_interrupt(
|
||||
&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
|
||||
@@ -61,12 +71,16 @@ impl FighterStateImpl for WaitState {
|
||||
}
|
||||
}
|
||||
|
||||
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.stick_deadzone {
|
||||
return Some(FighterState::Wait(WaitState { grounded_common: ground_state_common.clone() }));
|
||||
return Some(FighterState::Wait(WaitState {
|
||||
grounded_common: ground_state_common.clone(),
|
||||
}));
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
|
||||
+38
-21
@@ -1,6 +1,9 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use bevy::prelude::*;
|
||||
|
||||
use super::{FighterStateImpl, FighterStateContext, dash, ground, wait};
|
||||
use super::{FighterStateContext, FighterStateImpl, dash, ground, wait};
|
||||
use crate::fighter::animation::AnimKind;
|
||||
use crate::fighter::state::fall::FallState;
|
||||
use crate::fighter::state::ground::{GroundedMotionResult, GroundedStateCommon};
|
||||
use crate::fighter::state::{self, FighterState};
|
||||
@@ -10,26 +13,37 @@ use crate::input::FighterInput;
|
||||
|
||||
#[derive(Debug, Clone, Hash)]
|
||||
pub struct WalkState {
|
||||
pub grounded_common: GroundedStateCommon
|
||||
pub grounded_common: GroundedStateCommon,
|
||||
}
|
||||
|
||||
impl FighterStateImpl for WalkState {
|
||||
const NAME: &'static str = "Walk";
|
||||
fn check_interrupt(
|
||||
&self,
|
||||
state_context: &FighterStateContext,
|
||||
) -> Option<FighterState> {
|
||||
wait::check_input(state_context, &self.grounded_common).or_else(|| dash::check_input(state_context, &self.grounded_common))
|
||||
fn check_interrupt(&self, state_context: &FighterStateContext) -> Option<FighterState> {
|
||||
wait::check_input(state_context, &self.grounded_common)
|
||||
.or_else(|| dash::check_input(state_context, &self.grounded_common))
|
||||
}
|
||||
|
||||
fn on_enter(&mut self, _state_context: &mut FighterStateContext) {}
|
||||
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
fn update(&mut self, state_context: &mut FighterStateContext) {
|
||||
let last_frame = state_context.input.get_last_frame();
|
||||
let (accel, target_vel) = state_context.fighter_attribs.get_accel_and_target_walk(&last_frame);
|
||||
let (accel, target_vel) = state_context
|
||||
.fighter_attribs
|
||||
.get_accel_and_target_walk(&last_frame);
|
||||
|
||||
let accel =
|
||||
ground::compute_ground_accel(accel, target_vel, state_context.velocity.x, state_context.fighter_attribs, &state_context.game_settings);
|
||||
let accel = ground::compute_ground_accel(
|
||||
accel,
|
||||
target_vel,
|
||||
state_context.velocity.x,
|
||||
state_context.fighter_attribs,
|
||||
&state_context.game_settings,
|
||||
);
|
||||
|
||||
state_context.velocity.x += accel;
|
||||
|
||||
@@ -39,16 +53,14 @@ impl FighterStateImpl for WalkState {
|
||||
state_context.prev_translation,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
fn check_collision_interrupt(
|
||||
&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
|
||||
@@ -56,11 +68,16 @@ impl FighterStateImpl for WalkState {
|
||||
}
|
||||
}
|
||||
|
||||
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.stick_deadzone {
|
||||
return Some(FighterState::Walk(WalkState { grounded_common: ground_state_common.clone() }));
|
||||
return Some(FighterState::Walk(WalkState {
|
||||
grounded_common: ground_state_common.clone(),
|
||||
}));
|
||||
}
|
||||
|
||||
None
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
use bevy::gltf::Gltf;
|
||||
use bevy::prelude::*;
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::{AppState, fighter::{
|
||||
animation::{AnimKind, AnimManifest},
|
||||
spawn_fighter,
|
||||
}};
|
||||
|
||||
// One entry per character being loaded
|
||||
pub struct PendingCharacter {
|
||||
pub id: String,
|
||||
pub gltf: Handle<Gltf>,
|
||||
pub manifest: Handle<AnimManifest>,
|
||||
pub resolved: bool,
|
||||
}
|
||||
|
||||
#[derive(Resource, Default)]
|
||||
pub struct CharacterLoadQueue(pub Vec<PendingCharacter>);
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct FighterAnimations {
|
||||
pub graph: Handle<AnimationGraph>,
|
||||
pub clips: HashMap<AnimKind, AnimationNodeIndex>,
|
||||
}
|
||||
|
||||
pub fn start_loading(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||
let queue = vec![PendingCharacter {
|
||||
id: "jigglypuff".to_owned(),
|
||||
gltf: asset_server.load(format!("fighters/jiggs/jigglypuff-normal.glb")),
|
||||
manifest: asset_server.load(format!("fighters/jiggs/jigglypuff-anims.ron")),
|
||||
resolved: false,
|
||||
}];
|
||||
|
||||
commands.insert_resource(CharacterLoadQueue(queue));
|
||||
}
|
||||
|
||||
pub fn resolve_character_assets(
|
||||
mut commands: Commands,
|
||||
mut queue: ResMut<CharacterLoadQueue>,
|
||||
gltf_assets: Res<Assets<Gltf>>,
|
||||
manifest_assets: Res<Assets<AnimManifest>>,
|
||||
mut graphs: ResMut<Assets<AnimationGraph>>,
|
||||
anim_clips: Res<Assets<AnimationClip>>,
|
||||
mut next_state: ResMut<NextState<AppState>>,
|
||||
) {
|
||||
for pending in queue.0.iter_mut().filter(|p| !p.resolved) {
|
||||
let (Some(gltf), Some(manifest)) = (
|
||||
gltf_assets.get(&pending.gltf),
|
||||
manifest_assets.get(&pending.manifest),
|
||||
) else {
|
||||
continue; // still loading, try again next frame
|
||||
};
|
||||
|
||||
let mut graph = AnimationGraph::new();
|
||||
let mut clips = HashMap::new();
|
||||
let mut missing = Vec::new();
|
||||
|
||||
for (kind, clip_name) in manifest.0.iter() {
|
||||
match gltf.named_animations.get(clip_name.as_str()) {
|
||||
Some(clip_handle) => {
|
||||
let animation = anim_clips.get(clip_handle);
|
||||
info!("{clip_name}, {}", animation.unwrap().duration());
|
||||
let node = graph.add_clip(clip_handle.clone(), 1.0, graph.root);
|
||||
clips.insert(*kind, node);
|
||||
}
|
||||
None => missing.push(clip_name.clone()),
|
||||
}
|
||||
}
|
||||
|
||||
if !missing.is_empty() {
|
||||
panic!(
|
||||
"character '{}': manifest references animations not found in glb: {:?}",
|
||||
pending.id, missing
|
||||
);
|
||||
}
|
||||
|
||||
let graph_handle = graphs.add(graph);
|
||||
|
||||
|
||||
spawn_fighter(
|
||||
&mut commands,
|
||||
FighterAnimations {
|
||||
graph: graph_handle,
|
||||
clips,
|
||||
},
|
||||
WorldAssetRoot(gltf.scenes[0].clone()),
|
||||
);
|
||||
|
||||
pending.resolved = true;
|
||||
}
|
||||
|
||||
// All done? Move on.
|
||||
if queue.0.iter().all(|p| p.resolved) {
|
||||
next_state.set(AppState::InGame);
|
||||
commands.remove_resource::<CharacterLoadQueue>();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user