diff --git a/crates/bevy_ecs/README.md b/crates/bevy_ecs/README.md index 8bdb28fa59cd4..9c8a2c109c40f 100644 --- a/crates/bevy_ecs/README.md +++ b/crates/bevy_ecs/README.md @@ -25,6 +25,9 @@ Bevy ECS is Bevy's implementation of the ECS pattern. Unlike other Rust ECS impl Components are normal Rust structs. They are data stored in a `World` and specific instances of Components correlate to Entities. ```rust +use bevy_ecs::prelude::*; + +#[derive(Component)] struct Position { x: f32, y: f32 } ``` @@ -33,6 +36,8 @@ struct Position { x: f32, y: f32 } Entities, Components, and Resources are stored in a `World`. Worlds, much like Rust std collections like HashSet and Vec, expose operations to insert, read, write, and remove the data they store. ```rust +use bevy_ecs::world::World; + let world = World::default(); ``` @@ -41,6 +46,15 @@ let world = World::default(); Entities are unique identifiers that correlate to zero or more Components. ```rust +use bevy_ecs::prelude::*; + +#[derive(Component)] +struct Position { x: f32, y: f32 } +#[derive(Component)] +struct Velocity { x: f32, y: f32 } + +let mut world = World::new(); + let entity = world.spawn() .insert(Position { x: 0.0, y: 0.0 }) .insert(Velocity { x: 1.0, y: 0.0 }) @@ -56,6 +70,11 @@ let velocity = entity_ref.get::().unwrap(); Systems are normal Rust functions. Thanks to the Rust type system, Bevy ECS can use function parameter types to determine what data needs to be sent to the system. It also uses this "data access" information to determine what Systems can run in parallel with each other. ```rust +use bevy_ecs::prelude::*; + +#[derive(Component)] +struct Position { x: f32, y: f32 } + fn print_position(query: Query<(Entity, &Position)>) { for (entity, position) in query.iter() { println!("Entity {:?} is at position: x {}, y {}", entity, position.x, position.y); @@ -68,11 +87,15 @@ fn print_position(query: Query<(Entity, &Position)>) { Apps often require unique resources, such as asset collections, renderers, audio servers, time, etc. Bevy ECS makes this pattern a first class citizen. `Resource` is a special kind of component that does not belong to any entity. Instead, it is identified uniquely by its type: ```rust +use bevy_ecs::prelude::*; + #[derive(Default)] struct Time { seconds: f32, } +let mut world = World::new(); + world.insert_resource(Time::default()); let time = world.get_resource::