mirror of
https://github.com/bevyengine/bevy
synced 2024-11-22 20:53:53 +00:00
e6bce74220
#3457 adds the `doc_markdown` clippy lint, which checks doc comments to make sure code identifiers are escaped with backticks. This causes a lot of lint errors, so this is one of a number of PR's that will fix those lint errors one crate at a time. This PR fixes lints in the `examples` folder.
45 lines
1.2 KiB
Rust
45 lines
1.2 KiB
Rust
use bevy::{ecs::system::SystemParam, prelude::*};
|
|
|
|
/// This example creates a [`SystemParam`] struct that counts the number of players
|
|
fn main() {
|
|
App::new()
|
|
.insert_resource(PlayerCount(0))
|
|
.add_startup_system(spawn)
|
|
.add_system(count_players)
|
|
.run();
|
|
}
|
|
|
|
#[derive(Component)]
|
|
pub struct Player;
|
|
#[derive(Component)]
|
|
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)]
|
|
struct PlayerCounter<'w, 's> {
|
|
players: Query<'w, 's, &'static Player>,
|
|
count: ResMut<'w, PlayerCount>,
|
|
}
|
|
|
|
impl<'w, 's> PlayerCounter<'w, 's> {
|
|
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);
|
|
}
|