use bevy::{ecs::system::SystemParam, prelude::*};
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);
#[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();
}
}
fn spawn(mut commands: Commands) {
commands.spawn().insert(Player);
commands.spawn().insert(Player);
commands.spawn().insert(Player);
}
fn count_players(mut counter: PlayerCounter) {
counter.count();
println!("{} players in the game", counter.count.0);
}