bevy-playground/src/movement.rs

41 lines
1.2 KiB
Rust
Raw Normal View History

2023-12-20 13:36:34 -06:00
use bevy::prelude::*;
2023-12-20 22:08:40 -06:00
#[derive(Component, Deref, DerefMut, Debug, Default)]
2023-12-20 19:35:59 -06:00
pub struct Velocity(pub Vec2);
2023-12-20 13:36:34 -06:00
2023-12-20 22:08:40 -06:00
#[derive(Component, Deref, DerefMut, Debug, Default)]
2023-12-20 19:35:59 -06:00
pub struct Heading(pub Vec2);
2023-12-20 22:08:40 -06:00
#[derive(Component, Deref, DerefMut, Debug, Default)]
2023-12-20 19:35:59 -06:00
pub struct Speed(pub f32);
2023-12-20 13:36:34 -06:00
#[derive(Component, Deref, DerefMut, Debug, Default)]
pub struct Height(pub f32);
2024-07-31 15:37:11 -05:00
#[derive(Component, Debug, Default)]
2024-07-29 16:54:43 -05:00
pub struct YSortable;
2024-07-29 16:29:39 -05:00
pub fn resolve_velocity(mut query: Query<(&Velocity, &mut Transform)>, time: Res<Time>) {
2023-12-20 22:08:40 -06:00
for (velocity, mut transform) in query.iter_mut() {
transform.translation += (velocity.0 * time.delta_seconds()).extend(0.);
2023-12-20 13:36:34 -06:00
}
}
2024-07-29 16:29:39 -05:00
pub fn update_velocity_by_heading(mut query: Query<(&mut Velocity, &Speed, &Heading)>) {
2024-07-29 14:33:14 -05:00
if let Ok((mut velocity, speed, heading)) = query.get_single_mut() {
**velocity = heading.normalize_or_zero() * (**speed);
}
}
pub fn ysort(mut q: Query<(&mut Transform, Option<&Height>), With<YSortable>>) {
2024-07-29 14:33:14 -05:00
q.iter_mut()
.for_each(|(mut tf, z)| tf.translation.z = -tf.translation.y - z.map(|z| z.0).unwrap_or(0.))
2023-12-20 22:08:40 -06:00
}
2023-12-20 13:36:34 -06:00
#[derive(Bundle, Debug, Default)]
2023-12-20 22:08:40 -06:00
pub struct Mover {
2023-12-20 13:36:34 -06:00
pub velocity: Velocity,
2023-12-20 19:35:59 -06:00
pub heading: Heading,
pub speed: Speed,
2023-12-20 13:36:34 -06:00
}