azalea/swarm/
events.rs

1use azalea_client::InstanceHolder;
2use azalea_world::MinecraftEntityId;
3use bevy_app::{App, Plugin, Update};
4use bevy_ecs::prelude::*;
5use derive_more::{Deref, DerefMut};
6
7pub struct SwarmPlugin;
8impl Plugin for SwarmPlugin {
9    fn build(&self, app: &mut App) {
10        app.add_event::<SwarmReadyEvent>()
11            .add_systems(Update, check_ready)
12            .init_resource::<IsSwarmReady>();
13    }
14}
15
16/// All the bots from the swarm are now in the world.
17#[derive(Event)]
18pub struct SwarmReadyEvent;
19
20#[derive(Default, Resource, Deref, DerefMut)]
21struct IsSwarmReady(bool);
22
23fn check_ready(
24    query: Query<Option<&MinecraftEntityId>, With<InstanceHolder>>,
25    mut is_swarm_ready: ResMut<IsSwarmReady>,
26    mut ready_events: EventWriter<SwarmReadyEvent>,
27) {
28    // if we already know the swarm is ready, do nothing
29    if **is_swarm_ready {
30        return;
31    }
32    // if all the players are in the world, we're ready
33    for entity_id in query.iter() {
34        if entity_id.is_none() {
35            return;
36        }
37    }
38
39    // all the players are in the world, so we're ready
40    **is_swarm_ready = true;
41    ready_events.send(SwarmReadyEvent);
42}