aboutsummaryrefslogtreecommitdiff
path: root/azalea/src/swarm/events.rs
blob: e5f7f6e04add8e374e20b4cc86259a862eaf7014 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use azalea_client::local_player::WorldHolder;
use azalea_core::{entity_id::MinecraftEntityId, tick::GameTick};
use bevy_app::{App, Plugin, Update};
use bevy_ecs::prelude::*;
use derive_more::{Deref, DerefMut};

use crate::swarm::{Swarm, SwarmEvent};

pub struct SwarmPlugin;
impl Plugin for SwarmPlugin {
    fn build(&self, app: &mut App) {
        app.add_message::<SwarmReadyEvent>()
            .add_systems(Update, check_ready)
            .add_systems(GameTick, send_tick_event)
            .init_resource::<IsSwarmReady>();
    }
}

/// All the bots from the swarm are now in the world.
#[derive(Message)]
pub struct SwarmReadyEvent;

#[derive(Default, Deref, DerefMut, Resource)]
struct IsSwarmReady(bool);

fn check_ready(
    query: Query<Option<&MinecraftEntityId>, With<WorldHolder>>,
    mut is_swarm_ready: ResMut<IsSwarmReady>,
    mut ready_events: MessageWriter<SwarmReadyEvent>,
) {
    // if we already know the swarm is ready, do nothing
    if **is_swarm_ready {
        return;
    }
    // if all the players are in the world, we're ready
    for entity_id in query.iter() {
        if entity_id.is_none() {
            return;
        }
    }

    // all the players are in the world, so we're ready
    **is_swarm_ready = true;
    ready_events.write(SwarmReadyEvent);
}

fn send_tick_event(swarm: Option<Res<Swarm>>) {
    if let Some(swarm) = swarm {
        swarm.swarm_tx.send(SwarmEvent::Tick).unwrap();
    }
}