naia_client/world/
entity_mut.rs

1use std::hash::Hash;
2
3use naia_shared::{EntityAuthStatus, ReplicaMutWrapper, ReplicatedComponent, WorldMutType};
4
5use crate::{Client, ReplicationConfig};
6
7// EntityMut
8pub struct EntityMut<'s, E: Copy + Eq + Hash + Send + Sync, W: WorldMutType<E>> {
9    client: &'s mut Client<E>,
10    world: W,
11    entity: E,
12}
13
14impl<'s, E: Copy + Eq + Hash + Send + Sync, W: WorldMutType<E>> EntityMut<'s, E, W> {
15    pub(crate) fn new(client: &'s mut Client<E>, world: W, entity: &E) -> Self {
16        EntityMut {
17            client,
18            world,
19            entity: *entity,
20        }
21    }
22
23    pub fn id(&self) -> E {
24        self.entity
25    }
26
27    pub fn despawn(&mut self) {
28        self.client.despawn_entity(&mut self.world, &self.entity);
29    }
30
31    // Components
32
33    pub fn has_component<R: ReplicatedComponent>(&self) -> bool {
34        self.world.has_component::<R>(&self.entity)
35    }
36
37    pub fn component<R: ReplicatedComponent>(&mut self) -> Option<ReplicaMutWrapper<R>> {
38        self.world.component_mut::<R>(&self.entity)
39    }
40
41    pub fn insert_component<R: ReplicatedComponent>(&mut self, component_ref: R) -> &mut Self {
42        self.client
43            .insert_component(&mut self.world, &self.entity, component_ref);
44
45        self
46    }
47
48    pub fn remove_component<R: ReplicatedComponent>(&mut self) -> Option<R> {
49        self.client
50            .remove_component::<R, W>(&mut self.world, &self.entity)
51    }
52
53    // Authority / Config
54
55    pub fn configure_replication(&mut self, config: ReplicationConfig) -> &mut Self {
56        self.client
57            .configure_entity_replication(&mut self.world, &self.entity, config);
58
59        self
60    }
61
62    pub fn replication_config(&self) -> Option<ReplicationConfig> {
63        self.client.entity_replication_config(&self.entity)
64    }
65
66    pub fn authority(&self) -> Option<EntityAuthStatus> {
67        self.client.entity_authority_status(&self.entity)
68    }
69
70    pub fn request_authority(&mut self) -> &mut Self {
71        self.client.entity_request_authority(&self.entity);
72
73        self
74    }
75
76    pub fn release_authority(&mut self) -> &mut Self {
77        self.client.entity_release_authority(&self.entity);
78
79        self
80    }
81}