mirror of
https://github.com/bevyengine/bevy
synced 2024-11-10 15:14:50 +00:00
81b53d15d4
Resolves #1253 #1562 This makes the Commands apis consistent with World apis. This moves to a "type state" pattern (like World) where the "current entity" is stored in an `EntityCommands` builder. In general this tends to cuts down on indentation and line count. It comes at the cost of needing to type `commands` more and adding more semicolons to terminate expressions. I also added `spawn_bundle` to Commands because this is a common enough operation that I think its worth providing a shorthand.
43 lines
1.1 KiB
Rust
43 lines
1.1 KiB
Rust
use bevy::{ecs::system::SystemParam, prelude::*};
|
|
|
|
/// This example creates a SystemParam struct that counts the number of players
|
|
fn main() {
|
|
App::build()
|
|
.insert_resource(PlayerCount(0))
|
|
.add_startup_system(spawn.system())
|
|
.add_system(count_players.system())
|
|
.run();
|
|
}
|
|
|
|
pub struct Player;
|
|
pub struct PlayerCount(usize);
|
|
|
|
/// The SystemParam struct can contain any types that can also be included in a
|
|
/// system function signature.
|
|
///
|
|
/// In this example, it includes a query and a mutable resource.
|
|
#[derive(SystemParam)]
|
|
pub struct PlayerCounter<'a> {
|
|
players: Query<'a, &'static Player>,
|
|
count: ResMut<'a, PlayerCount>,
|
|
}
|
|
|
|
impl<'a> PlayerCounter<'a> {
|
|
fn count(&mut self) {
|
|
self.count.0 = self.players.iter().len();
|
|
}
|
|
}
|
|
|
|
/// Spawn some players to count
|
|
fn spawn(mut commands: Commands) {
|
|
commands.spawn().insert(Player);
|
|
commands.spawn().insert(Player);
|
|
commands.spawn().insert(Player);
|
|
}
|
|
|
|
/// The SystemParam can be used directly in a system argument.
|
|
fn count_players(mut counter: PlayerCounter) {
|
|
counter.count();
|
|
|
|
println!("{} players in the game", counter.count.0);
|
|
}
|