Skip to main content

naia_shared/world/
entity_event.rs

1use crate::{
2    ComponentKind, EntityAuthStatus, EntityMessageType, GlobalEntity, RemoteEntity, Replicate, Tick,
3};
4
5/// ECS-level event produced by the replication system when the remote world state changes.
6pub enum EntityEvent {
7    /// A new entity was spawned by the remote.
8    Spawn(GlobalEntity),
9    /// An existing entity was despawned by the remote.
10    Despawn(GlobalEntity),
11    /// A component was added to an entity.
12    InsertComponent(GlobalEntity, ComponentKind),
13    /// A component was removed from an entity; carries the last known component value.
14    RemoveComponent(GlobalEntity, Box<dyn Replicate>),
15    /// A component on an entity was updated at the given tick.
16    UpdateComponent(Tick, GlobalEntity, ComponentKind),
17
18    /// Entity was published (made visible to other users).
19    Publish(GlobalEntity),
20    /// Entity publication was retracted.
21    Unpublish(GlobalEntity),
22    /// Authority delegation was enabled for an entity.
23    EnableDelegation(GlobalEntity),
24    /// Authority delegation was disabled for an entity.
25    DisableDelegation(GlobalEntity),
26    /// Authority status for a delegated entity was updated.
27    SetAuthority(GlobalEntity, EntityAuthStatus),
28
29    /// Client is requesting authority over an entity.
30    RequestAuthority(GlobalEntity),
31    /// Client is releasing authority over an entity.
32    ReleaseAuthority(GlobalEntity),
33    /// Client acknowledged that delegation is now enabled.
34    EnableDelegationResponse(GlobalEntity),
35    /// An entity migrated; carries the new remote entity ID.
36    MigrateResponse(GlobalEntity, RemoteEntity),
37}
38
39impl EntityEvent {
40    /// Returns the [`EntityMessageType`] discriminant for this event, or `None` for `UpdateComponent` (which has no wire type).
41    pub fn to_type(&self) -> Option<EntityMessageType> {
42        match self {
43            Self::Spawn(_) => Some(EntityMessageType::Spawn),
44            Self::Despawn(_) => Some(EntityMessageType::Despawn),
45            Self::InsertComponent(_, _) => Some(EntityMessageType::InsertComponent),
46            Self::RemoveComponent(_, _) => Some(EntityMessageType::RemoveComponent),
47            Self::Publish(_) => Some(EntityMessageType::Publish),
48            Self::Unpublish(_) => Some(EntityMessageType::Unpublish),
49            Self::EnableDelegation(_) => Some(EntityMessageType::EnableDelegation),
50            Self::EnableDelegationResponse(_) => Some(EntityMessageType::EnableDelegationResponse),
51            Self::DisableDelegation(_) => Some(EntityMessageType::DisableDelegation),
52            Self::RequestAuthority(_) => Some(EntityMessageType::RequestAuthority),
53            Self::ReleaseAuthority(_) => Some(EntityMessageType::ReleaseAuthority),
54            Self::SetAuthority(_, _) => Some(EntityMessageType::SetAuthority),
55            Self::MigrateResponse(_, _) => Some(EntityMessageType::MigrateResponse),
56            Self::UpdateComponent(_, _, _) => None, // UpdateComponent is not a message type
57        }
58    }
59
60    /// Returns the [`GlobalEntity`] this event refers to.
61    pub fn entity(&self) -> GlobalEntity {
62        match self {
63            Self::Spawn(entity) => *entity,
64            Self::Despawn(entity) => *entity,
65            Self::InsertComponent(entity, _) => *entity,
66            Self::RemoveComponent(entity, _) => *entity,
67            Self::UpdateComponent(_, entity, _) => *entity,
68            Self::Publish(entity) => *entity,
69            Self::Unpublish(entity) => *entity,
70            Self::EnableDelegation(entity) => *entity,
71            Self::EnableDelegationResponse(entity) => *entity,
72            Self::DisableDelegation(entity) => *entity,
73            Self::RequestAuthority(entity) => *entity,
74            Self::ReleaseAuthority(entity) => *entity,
75            Self::SetAuthority(entity, _) => *entity,
76            Self::MigrateResponse(entity, _) => *entity,
77        }
78    }
79
80    /// Returns a human-readable string describing this event, suitable for debug logging.
81    pub fn log(&self) -> String {
82        let entity = self.entity();
83        if let Some(ev_type) = self.to_type() {
84            format!("{:?} {:?}", ev_type, entity)
85        } else {
86            format!("UpdateComponent {:?}", entity)
87        }
88    }
89}