bevy-playground/src/movement.rs

38 lines
1 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)>) {
2023-12-20 22:08:40 -06:00
let (mut velocity, speed, heading) = query.single_mut();
**velocity = heading.normalize_or_zero() * (**speed);
}
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?
app.add_systems(Update, (mover_movement, update_position));
2023-12-20 13:36:34 -06:00
}
}