1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use std::hash::Hash;

use naia_shared::{EntityAuthStatus, ReplicaMutWrapper, Replicate, WorldMutType};

use crate::{Client, ReplicationConfig};

// EntityMut
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 {
        EntityMut {
            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);
    }

    // Components

    pub fn has_component<R: Replicate>(&self) -> bool {
        self.world.has_component::<R>(&self.entity)
    }

    pub fn component<R: Replicate>(&mut self) -> Option<ReplicaMutWrapper<R>> {
        self.world.component_mut::<R>(&self.entity)
    }

    pub fn insert_component<R: Replicate>(&mut self, component_ref: R) -> &mut Self {
        self.client
            .insert_component(&mut self.world, &self.entity, component_ref);

        self
    }

    pub fn remove_component<R: Replicate>(&mut self) -> Option<R> {
        self.client
            .remove_component::<R, W>(&mut self.world, &self.entity)
    }

    // Authority / Config

    pub fn configure_replication(&mut self, config: ReplicationConfig) -> &mut Self {
        self.client
            .configure_entity_replication(&mut self.world, &self.entity, config);

        self
    }

    pub fn replication_config(&self) -> Option<ReplicationConfig> {
        self.client.entity_replication_config(&self.entity)
    }

    pub fn authority(&self) -> Option<EntityAuthStatus> {
        self.client.entity_authority_status(&self.entity)
    }

    pub fn request_authority(&mut self) -> &mut Self {
        self.client.entity_request_authority(&self.entity);

        self
    }

    pub fn release_authority(&mut self) -> &mut Self {
        self.client.entity_release_authority(&self.entity);

        self
    }
}