mirror of
https://github.com/bevyengine/bevy
synced 2024-12-27 13:33:08 +00:00
e71c4d2802
# Objective - fix new clippy lints before they get stable and break CI ## Solution - run `clippy --fix` to auto-fix machine-applicable lints - silence `clippy::should_implement_trait` for `fn HandleId::default<T: Asset>` ## Changes - always prefer `format!("{inline}")` over `format!("{}", not_inline)` - prefer `Box::default` (or `Box::<T>::default` if necessary) over `Box::new(T::default())`
50 lines
1.3 KiB
Rust
50 lines
1.3 KiB
Rust
use bevy_ecs::prelude::*;
|
|
use rand::Rng;
|
|
use std::ops::Deref;
|
|
|
|
// In this example we add a counter resource and increase it's value in one system,
|
|
// while a different system prints the current count to the console.
|
|
fn main() {
|
|
// Create a world
|
|
let mut world = World::new();
|
|
|
|
// Add the counter resource
|
|
world.insert_resource(Counter { value: 0 });
|
|
|
|
// Create a schedule and a stage
|
|
let mut schedule = Schedule::default();
|
|
let mut update = SystemStage::parallel();
|
|
|
|
// Add systems to increase the counter and to print out the current value
|
|
update.add_system(increase_counter);
|
|
update.add_system(print_counter.after(increase_counter));
|
|
|
|
// Declare a unique label for the stage.
|
|
#[derive(StageLabel)]
|
|
struct Update;
|
|
|
|
// Add the stage to the schedule.
|
|
schedule.add_stage(Update, update);
|
|
|
|
for iteration in 1..=10 {
|
|
println!("Simulating frame {iteration}/10");
|
|
schedule.run(&mut world);
|
|
}
|
|
}
|
|
|
|
// Counter resource to be increased and read by systems
|
|
#[derive(Debug, Resource)]
|
|
struct Counter {
|
|
pub value: i32,
|
|
}
|
|
|
|
fn increase_counter(mut counter: ResMut<Counter>) {
|
|
if rand::thread_rng().gen_bool(0.5) {
|
|
counter.value += 1;
|
|
println!(" Increased counter value");
|
|
}
|
|
}
|
|
|
|
fn print_counter(counter: Res<Counter>) {
|
|
println!(" {:?}", counter.deref());
|
|
}
|