use bevy::prelude::*;
use iyes_loopless::prelude::*;
use rand::prelude::*;
use std::time::Duration;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, StageLabel)]
struct MyFixedUpdate;
fn main() {
let mut fixed_spawn_stage = SystemStage::parallel();
fixed_spawn_stage.add_system(spawn_entities);
let mut post_fixed_spawn_stage = SystemStage::parallel();
post_fixed_spawn_stage.add_system(reposition_entities);
post_fixed_spawn_stage.add_system(debug_fixed_timestep);
App::new()
.add_plugins(DefaultPlugins)
.add_stage_before(
CoreStage::Update,
MyFixedUpdate,
FixedTimestepStage::new(Duration::from_millis(250))
.with_stage(fixed_spawn_stage)
.with_stage(post_fixed_spawn_stage),
)
.add_startup_system(setup_camera)
.add_system(debug_new_count)
.add_system(random_hiccups)
.run();
}
#[derive(Component)]
struct MySprite;
fn debug_fixed_timestep(info: Res<FixedTimestepInfo>) {
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 spawn_entities(mut commands: Commands) {
let mut rng = thread_rng();
commands
.spawn_bundle(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()
})
.insert(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 setup_camera(mut commands: Commands) {
commands.spawn_bundle(OrthographicCameraBundle::new_2d());
}