pub mod shared_impls;
use std::fmt::Debug;
use azalea_entity::EntityKindComponent;
use azalea_registry::builtin::EntityKind;
use bevy_ecs::{
component::Component,
entity::Entity,
query::{QueryData, QueryEntityError, QueryItem},
};
use parking_lot::MappedRwLockReadGuard;
use crate::Client;
#[derive(Clone)]
pub struct EntityRef {
client: Client,
entity: Entity,
}
impl EntityRef {
pub fn new(client: Client, entity: Entity) -> Self {
Self { client, entity }
}
pub fn id(&self) -> Entity {
self.entity
}
pub fn component<T: Component>(&self) -> MappedRwLockReadGuard<'_, T> {
self.client.entity_component(self.entity)
}
pub fn get_component<T: Component>(&self) -> Option<MappedRwLockReadGuard<'_, T>> {
self.client.get_entity_component(self.entity)
}
pub fn query_self<D: QueryData, R>(&self, f: impl FnOnce(QueryItem<D>) -> R) -> R {
self.client.query_entity(self.entity, f)
}
pub fn try_query_self<D: QueryData, R>(
&self,
f: impl FnOnce(QueryItem<D>) -> R,
) -> Result<R, QueryEntityError> {
self.client.try_query_entity(self.entity, f)
}
}
impl Debug for EntityRef {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("EntityRef")
.field("client", &self.client.entity)
.field("entity", &self.entity)
.finish()
}
}
impl EntityRef {
pub fn kind(&self) -> EntityKind {
**self.component::<EntityKindComponent>()
}
}
impl EntityRef {
pub fn attack(&self) {
self.client.attack(self.entity);
}
pub fn interact(&self) {
self.client.entity_interact(self.entity);
}
pub fn look_at(&self) {
self.client.look_at(self.eye_position());
}
pub fn distance_to_client(&self) -> f64 {
self.position().distance_to(self.client.position())
}
}