use bevy::prelude::*;
use iyes_loopless::prelude::*;
use rand::prelude::*;
use std::time::Duration;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_fixed_timestep(
Duration::from_millis(250),
"my_fixed_update",
)
.add_fixed_timestep_child_stage("my_fixed_update")
.add_fixed_timestep_system("my_fixed_update", 0, debug_fixed_timestep)
.add_fixed_timestep_system("my_fixed_update", 0, spawn_entities)
.add_fixed_timestep_system("my_fixed_update", 1, reposition_entities)
.add_startup_system(setup_camera)
.add_system(debug_new_count)
.add_system(random_hiccups)
.add_system(kbd_control_timestep)
.add_system(clear_entities)
.run();
}
#[derive(Component)]
struct MySprite;
fn spawn_entities(mut commands: Commands) {
let mut rng = thread_rng();
commands.spawn((SpriteBundle {
sprite: Sprite {
color: Color::rgba(rng.gen(), rng.gen(), rng.gen(), 0.5),
custom_size: Some(Vec2::new(64., 64.)),
..Default::default()
},
transform: Transform::from_xyz(0.0, 0.0, rng.gen_range(0.0..100.0)),
..Default::default()
}, MySprite));
}
fn reposition_entities(mut q: Query<&mut Transform, With<MySprite>>) {
let mut rng = thread_rng();
for mut transform in q.iter_mut() {
transform.translation.x = rng.gen_range(-420.0..420.0);
transform.translation.y = rng.gen_range(-420.0..420.0);
}
}
fn debug_fixed_timestep(timesteps: Res<FixedTimesteps>) {
let info = timesteps.get_current().unwrap();
println!("Fixed timestep duration: {:?} ({} Hz).", info.timestep(), info.rate());
println!("Overstepped by {:.2?} ({:.2}%).", info.remaining(), info.overstep() * 100.0);
}
fn debug_new_count(q: Query<(), Added<MySprite>>) {
let new = q.iter().count();
if new > 0 {
println!("{:?} new sprites spawned this frame", new);
println!();
}
}
fn random_hiccups() {
let mut rng = rand::thread_rng();
if rng.gen::<u8>() == 0 {
std::thread::sleep(Duration::from_millis(500));
}
if rng.gen::<u8>() == 255 {
std::thread::sleep(Duration::from_millis(1000));
}
}
fn kbd_control_timestep(
kbd: Res<Input<KeyCode>>,
mut timesteps: ResMut<FixedTimesteps>,
) {
let info = timesteps.get_mut("my_fixed_update").unwrap();
if kbd.any_just_pressed([KeyCode::Minus, KeyCode::Underline]) {
info.step = Duration::from_secs_f32(info.step.as_secs_f32() * 0.75);
}
if kbd.any_just_pressed([KeyCode::Plus, KeyCode::Equals]) {
info.step = Duration::from_secs_f32(info.step.as_secs_f32() * 1.25);
}
if kbd.just_pressed(KeyCode::Space) {
info.toggle_pause();
}
}
fn clear_entities(
mut commands: Commands,
kbd: Res<Input<KeyCode>>,
q: Query<Entity, With<MySprite>>
) {
if kbd.any_just_pressed([KeyCode::Delete, KeyCode::Back]) {
for e in q.iter() {
commands.entity(e).despawn();
}
}
}
fn setup_camera(mut commands: Commands) {
commands.spawn(Camera2dBundle::default());
}