aboutsummaryrefslogtreecommitdiff
path: root/azalea/examples
diff options
context:
space:
mode:
authorTheDudeFromCI <thedudefromci@gmail.com>2023-08-21 23:46:54 -0700
committerGitHub <noreply@github.com>2023-08-22 01:46:54 -0500
commit47a280212ab2e3b8c1afb34cad30488a2ea1071f (patch)
tree83a8c1fdc3deead30bdb252b2ec41d95d09bb549 /azalea/examples
parenta81c4c060b9a32d1dccf451158750fac52349acc (diff)
downloadazalea-drasl-47a280212ab2e3b8c1afb34cad30488a2ea1071f.tar.xz
Created nearest_entity system param (#102)
* Created nearest_entity system param Signed-off-by: TheDudeFromCI <thedudefromci@gmail.com> * Added nearby item iterators. Signed-off-by: TheDudeFromCI <thedudefromci@gmail.com> * Export bot.rs (#101) * Removed .vscode settings (#104) Signed-off-by: TheDudeFromCI <thedudefromci@gmail.com> * 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 <git@matdoes.dev> * Created nearest_entity system param Signed-off-by: TheDudeFromCI <thedudefromci@gmail.com> * Added nearby item iterators. Signed-off-by: TheDudeFromCI <thedudefromci@gmail.com> * Applied tweaks from PR review Signed-off-by: TheDudeFromCI <thedudefromci@gmail.com> * Fixed doctests Signed-off-by: TheDudeFromCI <thedudefromci@gmail.com> --------- Signed-off-by: TheDudeFromCI <thedudefromci@gmail.com> Co-authored-by: mat <git@matdoes.dev> Co-authored-by: Luuk van Oijen <lazyluuk.channel@gmail.com>
Diffstat (limited to 'azalea/examples')
-rw-r--r--azalea/examples/nearest_entity.rs73
1 files changed, 73 insertions, 0 deletions
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<Entity, (With<Local>, With<Player>)>,
+ entities: EntityFinder,
+ entity_positions: Query<(&Position, Option<&EyeHeight>)>,
+ mut look_at_event: EventWriter<LookAtEvent>,
+) {
+ 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<Entity, With<Bot>>,
+ entities: EntityFinder<With<ItemItem>>,
+ 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
+ );
+ }
+ }
+}