use std::hash::Hash;
use naia_shared::{
AuthorityError, EntityAuthStatus, ReplicaMutWrapper, ReplicatedComponent, WorldMutType,
};
use crate::{world::entity_owner::EntityOwner, Client};
use naia_shared::Publicity;
pub struct EntityMut<'s, E: Copy + Eq + Hash + Send + Sync, W: WorldMutType<E>> {
client: &'s mut Client<E>,
world: W,
entity: E,
}
impl<'s, E: Copy + Eq + Hash + Send + Sync, W: WorldMutType<E>> EntityMut<'s, E, W> {
pub(crate) fn new(client: &'s mut Client<E>, world: W, entity: &E) -> Self {
Self {
client,
world,
entity: *entity,
}
}
pub fn id(&self) -> E {
self.entity
}
pub fn despawn(&mut self) {
self.client.despawn_entity(&mut self.world, &self.entity);
}
pub fn has_component<R: ReplicatedComponent>(&self) -> bool {
self.world.has_component::<R>(&self.entity)
}
pub fn component<R: ReplicatedComponent>(&'_ mut self) -> Option<ReplicaMutWrapper<'_, R>> {
self.world.component_mut::<R>(&self.entity)
}
pub fn insert_component<R: ReplicatedComponent>(&mut self, component_ref: R) -> &mut Self {
self.client
.insert_component(&mut self.world, &self.entity, component_ref);
self
}
pub fn remove_component<R: ReplicatedComponent>(&mut self) -> Option<R> {
self.client
.remove_component::<R, W>(&mut self.world, &self.entity)
}
pub fn configure_replication(&mut self, config: Publicity) -> &mut Self {
self.client
.configure_entity_replication(&mut self.world, &self.entity, config);
self
}
pub fn replication_config(&self) -> Option<Publicity> {
self.client.entity_replication_config(&self.entity)
}
pub fn authority(&self) -> Option<EntityAuthStatus> {
self.client.entity_authority_status(&self.entity)
}
pub fn owner(&self) -> EntityOwner {
self.client.entity_owner(&self.entity)
}
pub fn request_authority(&mut self) -> Result<&mut Self, AuthorityError> {
self.client.entity_request_authority(&self.entity)?;
Ok(self)
}
pub fn release_authority(&mut self) -> Result<&mut Self, AuthorityError> {
self.client.entity_release_authority(&self.entity)?;
Ok(self)
}
}
cfg_if! {
if #[cfg(feature = "interior_visibility")] {
use naia_shared::LocalEntity;
impl<'s, E: Copy + Eq + Hash + Send + Sync, W: WorldMutType<E>> EntityMut<'s, E, W> {
pub fn local_entity(&self) -> Option<LocalEntity> {
self.client.world_to_local_entity(&self.entity)
}
}
}
}