bevy-playground/src/movement.rs

51 lines
1.3 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
2023-12-20 22:08:40 -06:00
fn update_position(mut query: Query<(&Velocity, &mut Transform)>, time: Res<Time>) {
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
}
}
2023-12-22 22:00:14 -06:00
fn mover_movement(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);
}
}
fn ysort(mut q: Query<&mut Transform>) {
q.iter_mut()
.for_each(|mut tf| tf.translation.z = -tf.translation.y)
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
}
2023-12-20 22:08:40 -06:00
pub struct Movement;
2023-12-20 13:36:34 -06:00
2023-12-20 22:08:40 -06:00
impl Plugin for Movement {
fn build(&self, app: &mut App) {
// TODO: these systems probably needs ordering?
2023-12-24 21:30:18 -06:00
app.add_systems(
Update,
2024-07-29 14:33:14 -05:00
(
update_position,
mover_movement.after(update_position),
ysort.after(mover_movement),
),
2023-12-24 21:30:18 -06:00
);
2023-12-20 13:36:34 -06:00
}
}