mirror of
https://github.com/mat-1/azalea.git
synced 2025-08-02 14:26:04 +00:00
try to make entity_by work
well it does work but i couldn't figure out how to make it look not terrible. Will hopefully change in the future
This commit is contained in:
parent
a971a30fd1
commit
8921ae4887
3 changed files with 73 additions and 55 deletions
|
@ -1,5 +1,11 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use azalea_world::entity::Entity;
|
||||
use bevy_ecs::query::{ReadOnlyWorldQuery, WorldQuery};
|
||||
use bevy_ecs::{
|
||||
prelude::Component,
|
||||
query::{QueryItem, ROQueryItem, ReadOnlyWorldQuery, WorldQuery},
|
||||
};
|
||||
use parking_lot::Mutex;
|
||||
|
||||
use crate::Client;
|
||||
|
||||
|
@ -14,44 +20,51 @@ impl Client {
|
|||
.expect("Our client is missing a required component.")
|
||||
}
|
||||
|
||||
// /// Return a lightweight [`Entity`] for the entity that matches the given
|
||||
// /// predicate function.
|
||||
// ///
|
||||
// /// You can then use [`Self::map_entity`] to get components from this
|
||||
// /// entity.
|
||||
// pub fn entity_by<'a, F: ReadOnlyWorldQuery, Q: ReadOnlyWorldQuery>(
|
||||
// &mut self,
|
||||
// mut predicate: impl EntityPredicate<'a, Q>,
|
||||
// ) -> Option<Entity> {
|
||||
// let mut ecs = self.ecs.lock();
|
||||
// let mut query = ecs.query_filtered::<(Entity, Q), F>();
|
||||
// let entity = query
|
||||
// .iter_mut(&mut ecs)
|
||||
// .find(|(_, q)| predicate.matches(q))
|
||||
// .map(|(entity, _)| entity);
|
||||
// entity
|
||||
// }
|
||||
/// Return a lightweight [`Entity`] for the entity that matches the given
|
||||
/// predicate function.
|
||||
///
|
||||
/// You can then use [`Self::map_entity`] to get components from this
|
||||
/// entity.
|
||||
pub fn entity_by<F: ReadOnlyWorldQuery, Q: ReadOnlyWorldQuery>(
|
||||
&mut self,
|
||||
predicate: impl EntityPredicate<Q, F>,
|
||||
) -> Option<Entity> {
|
||||
predicate.find(self.ecs.clone())
|
||||
}
|
||||
}
|
||||
|
||||
pub trait EntityPredicate<'a, Q: ReadOnlyWorldQuery> {
|
||||
fn matches(&self, components: &<Q as WorldQuery>::Item<'a>) -> bool;
|
||||
pub trait EntityPredicate<Q: ReadOnlyWorldQuery, Filter: ReadOnlyWorldQuery> {
|
||||
fn find(&self, ecs_lock: Arc<Mutex<bevy_ecs::world::World>>) -> Option<Entity>;
|
||||
}
|
||||
// impl<'a, F, Q> EntityPredicate<'a, Q> for F
|
||||
// where
|
||||
// F: Fn(Q) -> bool,
|
||||
// Q: ReadOnlyWorldQuery,
|
||||
// {
|
||||
// fn matches(&self, query: &<Q as WorldQuery>::Item<'a>) -> bool {
|
||||
// (self)(query)
|
||||
// }
|
||||
// }
|
||||
impl<'a, F, Q, Filter> EntityPredicate<(Q,), Filter> for F
|
||||
where
|
||||
F: Fn(&ROQueryItem<Q>) -> bool,
|
||||
Q: ReadOnlyWorldQuery,
|
||||
Filter: ReadOnlyWorldQuery,
|
||||
{
|
||||
fn find(&self, ecs_lock: Arc<Mutex<bevy_ecs::world::World>>) -> Option<Entity> {
|
||||
let mut ecs = ecs_lock.lock();
|
||||
let mut query = ecs.query_filtered::<(Entity, Q), Filter>();
|
||||
let entity = query.iter(&ecs).find(|(_, q)| (self)(q)).map(|(e, _)| e);
|
||||
|
||||
entity.clone()
|
||||
}
|
||||
}
|
||||
|
||||
// impl<'a, F, Q1, Q2> EntityPredicate<'a, (Q1, Q2)> for F
|
||||
// where
|
||||
// F: Fn(Q1, Q2) -> bool,
|
||||
// Q1: WorldQuery<Item<'a> = Q1>,
|
||||
// Q2: WorldQuery<Item<'a> = Q2>,
|
||||
// F: Fn(&<Q1 as WorldQuery>::Item<'_>, &<Q2 as WorldQuery>::Item<'_>) ->
|
||||
// bool, Q1: ReadOnlyWorldQuery,
|
||||
// Q2: ReadOnlyWorldQuery,
|
||||
// {
|
||||
// fn matches(&self, query: <(Q1, Q2) as WorldQuery>::Item<'_>) -> bool {
|
||||
// (self)(query.0, query.1)
|
||||
// fn find(&self, ecs: &mut bevy_ecs::world::World) -> Option<Entity> {
|
||||
// // (self)(query)
|
||||
// let mut query = ecs.query_filtered::<(Entity, Q1, Q2), ()>();
|
||||
// let entity = query
|
||||
// .iter(ecs)
|
||||
// .find(|(_, q1, q2)| (self)(q1, q2))
|
||||
// .map(|(e, _, _)| e);
|
||||
|
||||
// entity
|
||||
// }
|
||||
// }
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
#![allow(incomplete_features)]
|
||||
#![feature(trait_upcasting)]
|
||||
#![feature(error_generic_member_access)]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
||||
mod account;
|
||||
mod chat;
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
#![feature(type_alias_impl_trait)]
|
||||
|
||||
use azalea::ecs::query::With;
|
||||
use azalea::entity::metadata::Player;
|
||||
use azalea::entity::Position;
|
||||
use azalea::pathfinder::BlockPosGoal;
|
||||
// use azalea::ClientInformation;
|
||||
use azalea::{prelude::*, BlockPos, Swarm, SwarmEvent, WalkDirection};
|
||||
|
@ -92,7 +95,7 @@ async fn handle(mut bot: Client, event: Event, _state: State) -> anyhow::Result<
|
|||
let Some(sender) = m.username() else {
|
||||
return Ok(())
|
||||
};
|
||||
let mut ecs = bot.ecs.lock();
|
||||
// let mut ecs = bot.ecs.lock();
|
||||
// let entity = bot
|
||||
// .ecs
|
||||
// .lock()
|
||||
|
@ -100,27 +103,28 @@ async fn handle(mut bot: Client, event: Event, _state: State) -> anyhow::Result<
|
|||
// .iter(&mut ecs)
|
||||
// .find(|e| e.name() == Some(sender));
|
||||
// let entity = bot.entity_by::<With<Player>>(|name: &Name| name == sender);
|
||||
let entity = bot.entity_by(|name: &Name| name == sender);
|
||||
// let entity = bot.entity_by(|name: &Name| name == sender);
|
||||
let entity = bot.entity_by::<With<Player>, (&Position,)>(|pos: &&Position| true);
|
||||
if let Some(entity) = entity {
|
||||
if m.content() == "goto" {
|
||||
let target_pos_vec3 = entity.pos();
|
||||
let target_pos: BlockPos = target_pos_vec3.into();
|
||||
bot.goto(BlockPosGoal::from(target_pos));
|
||||
} else if m.content() == "look" {
|
||||
let target_pos_vec3 = entity.pos();
|
||||
let target_pos: BlockPos = target_pos_vec3.into();
|
||||
println!("target_pos: {:?}", target_pos);
|
||||
bot.look_at(target_pos.center());
|
||||
} else if m.content() == "jump" {
|
||||
bot.set_jumping(true);
|
||||
} else if m.content() == "walk" {
|
||||
bot.walk(WalkDirection::Forward);
|
||||
} else if m.content() == "stop" {
|
||||
bot.set_jumping(false);
|
||||
bot.walk(WalkDirection::None);
|
||||
} else if m.content() == "lag" {
|
||||
std::thread::sleep(Duration::from_millis(1000));
|
||||
}
|
||||
// if m.content() == "goto" {
|
||||
// let target_pos_vec3 = entity.pos();
|
||||
// let target_pos: BlockPos = target_pos_vec3.into();
|
||||
// bot.goto(BlockPosGoal::from(target_pos));
|
||||
// } else if m.content() == "look" {
|
||||
// let target_pos_vec3 = entity.pos();
|
||||
// let target_pos: BlockPos = target_pos_vec3.into();
|
||||
// println!("target_pos: {:?}", target_pos);
|
||||
// bot.look_at(target_pos.center());
|
||||
// } else if m.content() == "jump" {
|
||||
// bot.set_jumping(true);
|
||||
// } else if m.content() == "walk" {
|
||||
// bot.walk(WalkDirection::Forward);
|
||||
// } else if m.content() == "stop" {
|
||||
// bot.set_jumping(false);
|
||||
// bot.walk(WalkDirection::None);
|
||||
// } else if m.content() == "lag" {
|
||||
// std::thread::sleep(Duration::from_millis(1000));
|
||||
// }
|
||||
}
|
||||
}
|
||||
Event::Death(_) => {
|
||||
|
|
Loading…
Add table
Reference in a new issue