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
#[cfg(feature = "bevy")]
use bevy::ecs::entity::Entity;
use bincode::{Decode, Encode};
use de_types::player::Player;

/// Bevy ECS Entity derived identification of an entity.
#[derive(Clone, Copy, Debug, Encode, Decode, Hash, PartialEq, Eq)]
pub struct EntityNet {
    player: Player,
    index: NetEntityIndex,
}

impl EntityNet {
    /// # Arguments
    ///
    /// * `player` - the human player executing the entity simulating game
    ///   instance.
    ///
    /// * `index` - locally unique index of the entity.
    pub fn new(player: Player, index: NetEntityIndex) -> Self {
        Self { player, index }
    }

    pub fn player(&self) -> Player {
        self.player
    }

    pub fn index(&self) -> NetEntityIndex {
        self.index
    }
}

#[derive(Clone, Copy, Debug, Encode, Decode, Hash, PartialEq, Eq)]
pub struct NetEntityIndex(u32);

impl From<NetEntityIndex> for u32 {
    fn from(index: NetEntityIndex) -> u32 {
        index.0
    }
}

#[cfg(feature = "bevy")]
impl From<Entity> for NetEntityIndex {
    fn from(entity: Entity) -> Self {
        Self(entity.index())
    }
}