use bevy::{ecs::system::SystemParam, prelude::*};
fn main() {
App::new()
.insert_resource(PlayerCount(0))
.add_startup_system(spawn)
.add_system(count_players)
.run();
}
#[derive(Component)]
pub struct Player;
#[derive(Resource)]
pub struct PlayerCount(usize);
#[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();
}
}
fn spawn(mut commands: Commands) {
commands.spawn(Player);
commands.spawn(Player);
commands.spawn(Player);
}
fn count_players(mut counter: PlayerCounter) {
counter.count();
println!("{} players in the game", counter.count.0);
}