forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resources.rs
42 lines (34 loc) · 1.12 KB
/
resources.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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();
// Add systems to increase the counter and to print out the current value
schedule.add_system(increase_counter);
schedule.add_system(print_counter.after(increase_counter));
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());
}