use bevy::prelude::*;
pub struct Race;
impl Plugin for Race {
fn build(&self, app: &mut App) {
app.add_plugins(MinimalPlugins)
.insert_resource(RoundTimer(Timer::from_seconds(1., true)))
.add_startup_system(add_racers)
.add_system(racers_movement_system);
}
}
#[derive(Component)]
struct Racer(usize);
#[derive(Component)]
struct Velocity(f32);
#[derive(Component)]
struct Position(f32);
#[derive(Component)]
struct Acceleration(f32);
fn add_racers(mut commands: Commands) {
commands
.spawn()
.insert(Racer(1))
.insert(Position(0.))
.insert(Velocity(0.))
.insert(Acceleration(2.));
commands
.spawn()
.insert(Racer(2))
.insert(Position(0.))
.insert(Velocity(0.))
.insert(Acceleration(4.));
commands
.spawn()
.insert(Racer(3))
.insert(Position(0.))
.insert(Velocity(0.))
.insert(Acceleration(0.5));
}
struct RoundTimer(Timer);
fn racers_movement_system(
time: Res<Time>,
mut timer: ResMut<RoundTimer>,
mut query: Query<(&Racer, &mut Position, &mut Velocity, &Acceleration)>,
) {
if timer.0.tick(time.delta()).just_finished() {
for (r, mut p, mut v, a) in query.iter_mut() {
print!("Racer {} from {:?} ", r.0, (p.0, v.0, a.0));
v.0 = v.0 + a.0;
p.0 = p.0 + v.0;
println!("to {:?}", (p.0, v.0, a.0));
}
println!("")
}
}