aboutsummaryrefslogtreecommitdiff
path: root/azalea/examples/todo/pvp.rs
blob: 2b82fdfce4244ff38a65cc4d775e6a206e1d3d58 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
use std::time::Duration;

use azalea::{
    Account, Client, Event, GameProfileComponent, ecs::query::With, entity::metadata::Player,
    pathfinder, prelude::*, swarm::prelude::*,
};

#[tokio::main]
async fn main() -> AppExit {
    let mut accounts = Vec::new();
    let mut states = Vec::new();

    for i in 0..10 {
        accounts.push(Account::offline(&format!("bot{i}")));
        states.push(State::default());
    }

    SwarmBuilder::new()
        .add_accounts(accounts.clone())
        .set_handler(handle)
        .set_swarm_handler(swarm_handle)
        .join_delay(Duration::from_millis(1000))
        .start("localhost")
        .await
}

#[derive(Clone, Component, Default)]
struct State {}

#[derive(Clone, Default, Resource)]
struct SwarmState {}

async fn handle(bot: Client, event: Event, state: State) -> eyre::Result<()> {
    Ok(())
}
async fn swarm_handle(swarm: Swarm, event: SwarmEvent, state: SwarmState) -> eyre::Result<()> {
    match event {
        SwarmEvent::Tick => {
            if let Some(target_entity) =
                swarm.entity_by::<With<Player>>(|profile: &&GameProfileComponent| {
                    profile.name == "Herobrine"
                })
            {
                let target_bounding_box =
                    swarm.map_entity(target_entity, |bb: &BoundingBox| bb.clone());

                for (bot, bot_state) in swarm {
                    bot.tick_goto_goal(pathfinder::Goals::Reach(target_bounding_box));
                    // if target.bounding_box.distance(bot.eyes) < bot.reach_distance() {
                    if bot.can_reach(target_bounding_box) {
                        bot.swing();
                    }
                    if !bot.using_held_item() && bot.hunger() <= 17 {
                        bot.hold(azalea::ItemGroup::Food);
                        tokio::task::spawn(bot.use_held_item());
                    }
                }
            }
        }
        _ => {}
    }

    Ok(())
}