aboutsummaryrefslogtreecommitdiff
path: root/azalea/examples/testbot.rs
diff options
context:
space:
mode:
authormat <27899617+mat-1@users.noreply.github.com>2023-07-15 04:39:43 -0500
committerGitHub <noreply@github.com>2023-07-15 04:39:43 -0500
commitcde7e35046b726b07bf3e067c080b85a12b2fd74 (patch)
tree9d517911cbaf14f007958a92392101f24ec14118 /azalea/examples/testbot.rs
parent148f20381750be3e2c38a6bdaf8d339113da1b39 (diff)
downloadazalea-drasl-cde7e35046b726b07bf3e067c080b85a12b2fd74.tar.xz
Attacking (#96)
* add Client::attack * partially implement attack cooldowns * attack speed modifiers * don't care clippy --------- Co-authored-by: mat <git@matdoes.dev>
Diffstat (limited to 'azalea/examples/testbot.rs')
-rw-r--r--azalea/examples/testbot.rs52
1 files changed, 52 insertions, 0 deletions
diff --git a/azalea/examples/testbot.rs b/azalea/examples/testbot.rs
index 337ac6ec..75a6ca67 100644
--- a/azalea/examples/testbot.rs
+++ b/azalea/examples/testbot.rs
@@ -11,6 +11,8 @@ use azalea::pathfinder::BlockPosGoal;
use azalea::protocol::packets::game::ClientboundGamePacket;
use azalea::{prelude::*, swarm::prelude::*, BlockPos, GameProfileComponent, WalkDirection};
use azalea::{Account, Client, Event};
+use azalea_core::Vec3;
+use azalea_world::{InstanceName, MinecraftEntityId};
use std::time::Duration;
#[derive(Default, Clone, Component)]
@@ -220,6 +222,56 @@ async fn handle(mut bot: Client, event: Event, _state: State) -> anyhow::Result<
println!("no container found");
}
}
+ "attack" => {
+ let mut nearest_entity = None;
+ let mut nearest_distance = f64::INFINITY;
+ let mut nearest_pos = Vec3::default();
+ let bot_position = bot.position();
+ let bot_entity = bot.entity;
+ let bot_instance_name = bot.component::<InstanceName>();
+ {
+ let mut ecs = bot.ecs.lock();
+ let mut query = ecs.query_filtered::<(
+ azalea::ecs::entity::Entity,
+ &MinecraftEntityId,
+ &Position,
+ &InstanceName,
+ &EyeHeight,
+ ), With<MinecraftEntityId>>(
+ );
+ for (entity, &entity_id, position, instance_name, eye_height) in
+ query.iter(&ecs)
+ {
+ if entity == bot_entity {
+ continue;
+ }
+ if instance_name != &bot_instance_name {
+ continue;
+ }
+
+ let distance = bot_position.distance_to(position);
+ if distance < 4.0 && distance < nearest_distance {
+ nearest_entity = Some(entity_id);
+ nearest_distance = distance;
+ nearest_pos = position.up(**eye_height as f64);
+ }
+ }
+ }
+ if let Some(nearest_entity) = nearest_entity {
+ bot.look_at(nearest_pos);
+ bot.attack(nearest_entity);
+ bot.chat("attacking");
+ let mut ticks = bot.get_tick_broadcaster();
+ while ticks.recv().await.is_ok() {
+ if !bot.has_attack_cooldown() {
+ break;
+ }
+ }
+ bot.chat("finished attacking");
+ } else {
+ bot.chat("no entities found");
+ }
+ }
_ => {}
}
}