refactoring
This commit is contained in:
parent
c9adeeb94f
commit
228380b9f4
124
src/skeleton.rs
124
src/skeleton.rs
|
@ -148,6 +148,11 @@ pub enum Limb {
|
|||
LowerLegLeft,
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
pub enum Animation {
|
||||
HumanFloat,
|
||||
}
|
||||
|
||||
pub fn load(
|
||||
name: &str,
|
||||
entity_commands: &mut EntityCommands,
|
||||
|
@ -160,6 +165,7 @@ pub fn load(
|
|||
entity_commands.with_children(|parent| {
|
||||
parent.spawn((
|
||||
Limb::Base,
|
||||
Animation::HumanFloat,
|
||||
SceneBundle {
|
||||
scene: load_scene_by_path(human.base.as_str(), asset_server),
|
||||
..default()
|
||||
|
@ -173,6 +179,7 @@ pub fn load(
|
|||
};
|
||||
let mut parent_limb = parent.spawn((
|
||||
limb.class,
|
||||
Animation::HumanFloat,
|
||||
SceneBundle {
|
||||
scene: load_scene_by_path(limb.path.as_str(), asset_server),
|
||||
transform: Transform::from_translation(limb.pos).with_rotation(rot),
|
||||
|
@ -192,6 +199,7 @@ pub fn load(
|
|||
};
|
||||
let mut entity_commands = parent.spawn((
|
||||
child_limb.class,
|
||||
Animation::HumanFloat,
|
||||
SceneBundle {
|
||||
scene: load_scene_by_path(child_limb.path.as_str(), asset_server),
|
||||
transform: Transform::from_translation(child_limb.pos).with_rotation(rot),
|
||||
|
@ -241,61 +249,73 @@ pub fn _build_body(
|
|||
|
||||
pub fn animate_skeleton_parts(
|
||||
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();
|
||||
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 m = {|angle| if mirror { 180f32 + angle } else { angle }};
|
||||
let mut rot = {|x: f32, y: f32, z: f32|
|
||||
trans.rotation = Quat::from_euler(EulerRot::XYZ,
|
||||
x.to_radians(), y.to_radians(), z.to_radians())
|
||||
};
|
||||
// x: lean head forward/backward
|
||||
// y: close/open arms together in front of the torso
|
||||
// z: spread legs sidewards
|
||||
match limb {
|
||||
Limb::UpperArmRight => rot(
|
||||
0.0,
|
||||
m(40.0 + 5.0 * (t * 0.5).sin()),
|
||||
20.0 + 10.0 * (t * 0.5).sin(),
|
||||
),
|
||||
Limb::UpperArmLeft => rot(
|
||||
0.0,
|
||||
m(-(40.0 + 5.0 * (t * 0.5).sin())),
|
||||
20.0 + 10.0 * (t * 0.5).sin(),
|
||||
),
|
||||
Limb::LowerArmRight => rot(
|
||||
0.0,
|
||||
m(20.0),
|
||||
-20.0,
|
||||
),
|
||||
Limb::LowerArmLeft => rot(
|
||||
0.0,
|
||||
m(-20.0),
|
||||
-20.0,
|
||||
),
|
||||
Limb::UpperLegRight => rot(
|
||||
-30.0 + 10.0 * (t * 0.5).sin(),
|
||||
0.0,
|
||||
-25.0 + 2.5 * (t * 0.5).cos(),
|
||||
),
|
||||
Limb::UpperLegLeft => rot(
|
||||
-30.0 + 10.0 * (t * 0.5).sin(),
|
||||
0.0,
|
||||
25.0 - 2.5 * (t * 0.5).cos(),
|
||||
),
|
||||
Limb::LowerLegRight => rot(
|
||||
30.0,
|
||||
0.0,
|
||||
0.0,
|
||||
),
|
||||
Limb::LowerLegLeft => rot(
|
||||
30.0,
|
||||
0.0,
|
||||
0.0,
|
||||
),
|
||||
_ => {},
|
||||
match animation {
|
||||
Animation::HumanFloat =>
|
||||
animate_human_float(&mut trans, &limb, mirror, t),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
// y: close/open arms together in front of the torso
|
||||
// z: spread legs sidewards
|
||||
let m = {|angle| if mirror { 180f32 + angle } else { angle }};
|
||||
match limb {
|
||||
Limb::UpperArmRight => rot(&mut trans,
|
||||
0.0,
|
||||
m(40.0 + 5.0 * (t * 0.5).sin()),
|
||||
20.0 + 10.0 * (t * 0.5).sin(),
|
||||
),
|
||||
Limb::UpperArmLeft => rot(&mut trans,
|
||||
0.0,
|
||||
m(-(40.0 + 5.0 * (t * 0.5).sin())),
|
||||
20.0 + 10.0 * (t * 0.5).sin(),
|
||||
),
|
||||
Limb::LowerArmRight => rot(&mut trans,
|
||||
0.0,
|
||||
m(20.0),
|
||||
-20.0,
|
||||
),
|
||||
Limb::LowerArmLeft => rot(&mut trans,
|
||||
0.0,
|
||||
m(-20.0),
|
||||
-20.0,
|
||||
),
|
||||
Limb::UpperLegRight => rot(&mut trans,
|
||||
-30.0 + 10.0 * (t * 0.5).sin(),
|
||||
0.0,
|
||||
-25.0 + 2.5 * (t * 0.5).cos(),
|
||||
),
|
||||
Limb::UpperLegLeft => rot(&mut trans,
|
||||
-30.0 + 10.0 * (t * 0.5).sin(),
|
||||
0.0,
|
||||
25.0 - 2.5 * (t * 0.5).cos(),
|
||||
),
|
||||
Limb::LowerLegRight => rot(&mut trans,
|
||||
30.0,
|
||||
0.0,
|
||||
0.0,
|
||||
),
|
||||
Limb::LowerLegLeft => rot(&mut trans,
|
||||
30.0,
|
||||
0.0,
|
||||
0.0,
|
||||
),
|
||||
_ => {},
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue