mirror of
https://github.com/bevyengine/bevy
synced 2024-11-10 23:24:44 +00:00
cebb553bff
[RENDERED](https://github.com/NiklasEi/bevy/blob/ecs_readme/crates/bevy_ecs/README.md) Since I am trying to learn more about Bevy ECS at the moment, I thought this issue is a perfect fit. This PR adds a readme to the `bevy_ecs` crate containing a minimal running example of stand alone `bevy_ecs`. Unique features like customizable component storage, Resources or change detection are introduced. For each of these features the readme links to an example in a newly created examples directory inside the `bevy_esc` crate. Resolves #2008 Co-authored-by: Carter Anderson <mcanders1@gmail.com>
50 lines
1.4 KiB
Rust
50 lines
1.4 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.system().label(CounterSystem::Increase));
|
|
update.add_system(print_counter.system().after(CounterSystem::Increase));
|
|
schedule.add_stage("update", update);
|
|
|
|
for iteration in 1..=10 {
|
|
println!("Simulating frame {}/10", iteration);
|
|
schedule.run(&mut world);
|
|
}
|
|
}
|
|
|
|
// Counter resource to be increased and read by systems
|
|
#[derive(Debug)]
|
|
struct Counter {
|
|
pub value: i32,
|
|
}
|
|
|
|
// System label to enforce a run order of our systems
|
|
#[derive(SystemLabel, Debug, Clone, PartialEq, Eq, Hash)]
|
|
enum CounterSystem {
|
|
Increase,
|
|
}
|
|
|
|
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());
|
|
}
|