naia_client/world/
entity_mut.rs1use std::hash::Hash;
2
3use naia_shared::{
4 AuthorityError, EntityAuthStatus, ReplicaMutWrapper, ReplicatedComponent, WorldMutType,
5};
6
7use crate::{world::entity_owner::EntityOwner, Client};
8use naia_shared::Publicity;
9
10pub struct EntityMut<'s, E: Copy + Eq + Hash + Send + Sync, W: WorldMutType<E>> {
19 client: &'s mut Client<E>,
20 world: W,
21 entity: E,
22}
23
24impl<'s, E: Copy + Eq + Hash + Send + Sync, W: WorldMutType<E>> EntityMut<'s, E, W> {
25 pub(crate) fn new(client: &'s mut Client<E>, world: W, entity: &E) -> Self {
26 Self {
27 client,
28 world,
29 entity: *entity,
30 }
31 }
32
33 pub fn id(&self) -> E {
35 self.entity
36 }
37
38 pub fn despawn(&mut self) {
40 self.client.despawn_entity(&mut self.world, &self.entity);
41 }
42
43 pub fn has_component<R: ReplicatedComponent>(&self) -> bool {
47 self.world.has_component::<R>(&self.entity)
48 }
49
50 pub fn component<R: ReplicatedComponent>(&'_ mut self) -> Option<ReplicaMutWrapper<'_, R>> {
53 self.world.component_mut::<R>(&self.entity)
54 }
55
56 pub fn insert_component<R: ReplicatedComponent>(&mut self, component_ref: R) -> &mut Self {
58 self.client
59 .insert_component(&mut self.world, &self.entity, component_ref);
60
61 self
62 }
63
64 pub fn remove_component<R: ReplicatedComponent>(&mut self) -> Option<R> {
66 self.client
67 .remove_component::<R, W>(&mut self.world, &self.entity)
68 }
69
70 pub fn configure_replication(&mut self, config: Publicity) -> &mut Self {
75 self.client
76 .configure_entity_replication(&mut self.world, &self.entity, config);
77
78 self
79 }
80
81 pub fn replication_config(&self) -> Option<Publicity> {
84 self.client.entity_replication_config(&self.entity)
85 }
86
87 pub fn authority(&self) -> Option<EntityAuthStatus> {
90 self.client.entity_authority_status(&self.entity)
91 }
92
93 pub fn owner(&self) -> EntityOwner {
96 self.client.entity_owner(&self.entity)
97 }
98
99 pub fn request_authority(&mut self) -> Result<&mut Self, AuthorityError> {
108 self.client.entity_request_authority(&self.entity)?;
109 Ok(self)
110 }
111
112 pub fn release_authority(&mut self) -> Result<&mut Self, AuthorityError> {
119 self.client.entity_release_authority(&self.entity)?;
120 Ok(self)
121 }
122}
123
124cfg_if! {
125 if #[cfg(feature = "interior_visibility")] {
126
127 use naia_shared::LocalEntity;
128
129 impl<'s, E: Copy + Eq + Hash + Send + Sync, W: WorldMutType<E>> EntityMut<'s, E, W> {
130
131 pub fn local_entity(&self) -> Option<LocalEntity> {
136 self.client.world_to_local_entity(&self.entity)
137 }
138 }
139 }
140}