From 47a280212ab2e3b8c1afb34cad30488a2ea1071f Mon Sep 17 00:00:00 2001 From: TheDudeFromCI Date: Mon, 21 Aug 2023 23:46:54 -0700 Subject: Created nearest_entity system param (#102) * Created nearest_entity system param Signed-off-by: TheDudeFromCI * Added nearby item iterators. Signed-off-by: TheDudeFromCI * Export bot.rs (#101) * Removed .vscode settings (#104) Signed-off-by: TheDudeFromCI * raycasting not raytracing * don't panic if TranslatableComponent::to_string fails * Food/saturation component support (#97) * modified for food stuff * moved food/saturation to a separate file * hunger component * simplify some logic --------- Co-authored-by: mat * Created nearest_entity system param Signed-off-by: TheDudeFromCI * Added nearby item iterators. Signed-off-by: TheDudeFromCI * Applied tweaks from PR review Signed-off-by: TheDudeFromCI * Fixed doctests Signed-off-by: TheDudeFromCI --------- Signed-off-by: TheDudeFromCI Co-authored-by: mat Co-authored-by: Luuk van Oijen --- azalea/examples/nearest_entity.rs | 73 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 azalea/examples/nearest_entity.rs (limited to 'azalea/examples') diff --git a/azalea/examples/nearest_entity.rs b/azalea/examples/nearest_entity.rs new file mode 100644 index 00000000..9ac6331b --- /dev/null +++ b/azalea/examples/nearest_entity.rs @@ -0,0 +1,73 @@ +use azalea::nearest_entity::EntityFinder; +use azalea::ClientBuilder; +use azalea::{Bot, LookAtEvent}; +use azalea_client::Account; +use azalea_entity::metadata::{ItemItem, Player}; +use azalea_entity::{EyeHeight, Local, Position}; +use bevy_app::{FixedUpdate, Plugin}; +use bevy_ecs::{ + prelude::{Entity, EventWriter}, + query::With, + system::Query, +}; + +#[tokio::main] +async fn main() { + let account = Account::offline("bot"); + + ClientBuilder::new() + .add_plugins(LookAtStuffPlugin) + .start(account, "localhost") + .await + .unwrap(); +} + +pub struct LookAtStuffPlugin; +impl Plugin for LookAtStuffPlugin { + fn build(&self, app: &mut bevy_app::App) { + app.add_systems(FixedUpdate, (look_at_everything, log_nearby_item_drops)); + } +} + +fn look_at_everything( + bots: Query, With)>, + entities: EntityFinder, + entity_positions: Query<(&Position, Option<&EyeHeight>)>, + mut look_at_event: EventWriter, +) { + for bot_id in bots.iter() { + let Some(entity) = entities.nearest_to_entity(bot_id, 16.0) else { + continue; + }; + + let (position, eye_height) = entity_positions.get(entity).unwrap(); + + let mut look_target = **position; + if let Some(eye_height) = eye_height { + look_target.y += **eye_height as f64; + } + + look_at_event.send(LookAtEvent { + entity: bot_id, + position: look_target, + }); + } +} + +fn log_nearby_item_drops( + bots: Query>, + entities: EntityFinder>, + item_drops: Query<&ItemItem>, +) { + for bot_id in bots.iter() { + for (entity, distance) in entities.nearby_entities_to_entity(bot_id, 8.0) { + let item_drop = item_drops.get(entity).unwrap(); + let kind = item_drop.kind(); + + println!( + "Bot {:?} can see an {:?} {:.1} meters away.", + bot_id, kind, distance + ); + } + } +} -- cgit v1.2.3