refactoring
This commit is contained in:
parent
c9adeeb94f
commit
228380b9f4
1 changed files with 72 additions and 52 deletions
124
src/skeleton.rs
124
src/skeleton.rs
|
@ -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,61 +249,73 @@ 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())
|
|
||||||
};
|
|
||||||
// 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,
|
|
||||||
),
|
|
||||||
_ => {},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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…
Add table
Reference in a new issue