refactoring

This commit is contained in:
yuni 2024-04-22 23:19:42 +02:00
parent c9adeeb94f
commit 228380b9f4

View file

@ -148,6 +148,11 @@ pub enum Limb {
LowerLegLeft, LowerLegLeft,
} }
#[derive(Component)]
pub enum Animation {
HumanFloat,
}
pub fn load( pub fn load(
name: &str, name: &str,
entity_commands: &mut EntityCommands, entity_commands: &mut EntityCommands,
@ -160,6 +165,7 @@ pub fn load(
entity_commands.with_children(|parent| { entity_commands.with_children(|parent| {
parent.spawn(( parent.spawn((
Limb::Base, Limb::Base,
Animation::HumanFloat,
SceneBundle { SceneBundle {
scene: load_scene_by_path(human.base.as_str(), asset_server), scene: load_scene_by_path(human.base.as_str(), asset_server),
..default() ..default()
@ -173,6 +179,7 @@ pub fn load(
}; };
let mut parent_limb = parent.spawn(( let mut parent_limb = parent.spawn((
limb.class, limb.class,
Animation::HumanFloat,
SceneBundle { SceneBundle {
scene: load_scene_by_path(limb.path.as_str(), asset_server), scene: load_scene_by_path(limb.path.as_str(), asset_server),
transform: Transform::from_translation(limb.pos).with_rotation(rot), transform: Transform::from_translation(limb.pos).with_rotation(rot),
@ -192,6 +199,7 @@ pub fn load(
}; };
let mut entity_commands = parent.spawn(( let mut entity_commands = parent.spawn((
child_limb.class, child_limb.class,
Animation::HumanFloat,
SceneBundle { SceneBundle {
scene: load_scene_by_path(child_limb.path.as_str(), asset_server), scene: load_scene_by_path(child_limb.path.as_str(), asset_server),
transform: Transform::from_translation(child_limb.pos).with_rotation(rot), transform: Transform::from_translation(child_limb.pos).with_rotation(rot),
@ -241,56 +249,69 @@ pub fn _build_body(
pub fn animate_skeleton_parts( pub fn animate_skeleton_parts(
time: Res<Time>, time: Res<Time>,
mut q_limb: Query<(&mut Transform, &Limb, Option<&MirroredLimb>)>, mut q_limb: Query<(&mut Transform, &Limb, &Animation, Option<&MirroredLimb>)>,
) { ) {
let t = time.elapsed_seconds(); let t = time.elapsed_seconds();
for (mut trans, limb, mirror) in &mut q_limb { for (mut trans, limb, animation, mirror) in &mut q_limb {
let mirror = mirror.is_some(); let mirror = mirror.is_some();
let m = {|angle| if mirror { 180f32 + angle } else { angle }}; match animation {
let mut rot = {|x: f32, y: f32, z: f32| Animation::HumanFloat =>
trans.rotation = Quat::from_euler(EulerRot::XYZ, animate_human_float(&mut trans, &limb, mirror, t),
x.to_radians(), y.to_radians(), z.to_radians()) }
}; }
}
fn rot(trans: &mut Transform, x: f32, y: f32, z: f32) {
trans.rotation = Quat::from_euler(
EulerRot::XYZ,
x.to_radians(),
y.to_radians(),
z.to_radians()
);
}
pub fn animate_human_float(mut trans: &mut Transform, limb: &Limb, mirror: bool, t: f32) {
// x: lean head forward/backward // x: lean head forward/backward
// y: close/open arms together in front of the torso // y: close/open arms together in front of the torso
// z: spread legs sidewards // z: spread legs sidewards
let m = {|angle| if mirror { 180f32 + angle } else { angle }};
match limb { match limb {
Limb::UpperArmRight => rot( Limb::UpperArmRight => rot(&mut trans,
0.0, 0.0,
m(40.0 + 5.0 * (t * 0.5).sin()), m(40.0 + 5.0 * (t * 0.5).sin()),
20.0 + 10.0 * (t * 0.5).sin(), 20.0 + 10.0 * (t * 0.5).sin(),
), ),
Limb::UpperArmLeft => rot( Limb::UpperArmLeft => rot(&mut trans,
0.0, 0.0,
m(-(40.0 + 5.0 * (t * 0.5).sin())), m(-(40.0 + 5.0 * (t * 0.5).sin())),
20.0 + 10.0 * (t * 0.5).sin(), 20.0 + 10.0 * (t * 0.5).sin(),
), ),
Limb::LowerArmRight => rot( Limb::LowerArmRight => rot(&mut trans,
0.0, 0.0,
m(20.0), m(20.0),
-20.0, -20.0,
), ),
Limb::LowerArmLeft => rot( Limb::LowerArmLeft => rot(&mut trans,
0.0, 0.0,
m(-20.0), m(-20.0),
-20.0, -20.0,
), ),
Limb::UpperLegRight => rot( Limb::UpperLegRight => rot(&mut trans,
-30.0 + 10.0 * (t * 0.5).sin(), -30.0 + 10.0 * (t * 0.5).sin(),
0.0, 0.0,
-25.0 + 2.5 * (t * 0.5).cos(), -25.0 + 2.5 * (t * 0.5).cos(),
), ),
Limb::UpperLegLeft => rot( Limb::UpperLegLeft => rot(&mut trans,
-30.0 + 10.0 * (t * 0.5).sin(), -30.0 + 10.0 * (t * 0.5).sin(),
0.0, 0.0,
25.0 - 2.5 * (t * 0.5).cos(), 25.0 - 2.5 * (t * 0.5).cos(),
), ),
Limb::LowerLegRight => rot( Limb::LowerLegRight => rot(&mut trans,
30.0, 30.0,
0.0, 0.0,
0.0, 0.0,
), ),
Limb::LowerLegLeft => rot( Limb::LowerLegLeft => rot(&mut trans,
30.0, 30.0,
0.0, 0.0,
0.0, 0.0,
@ -298,4 +319,3 @@ pub fn animate_skeleton_parts(
_ => {}, _ => {},
} }
} }
}