Skip to main content

naia_shared/world/entity/
entity_message_receiver.rs

1use std::{fmt::Debug, hash::Hash};
2
3use crate::{
4    messages::channels::receivers::reliable_receiver::ReliableReceiver,
5    world::sync::{HostEngine, RemoteEngine},
6    EntityMessage, HostEntity, MessageIndex,
7};
8
9/// Stateless helper that routes incoming entity messages into per-entity ordered queues.
10pub struct EntityMessageReceiver;
11
12impl EntityMessageReceiver {
13    /// Buffer a read [`EntityMessage`] so that it can be processed later
14    pub fn buffer_message<E: Copy + Hash + Eq + Debug>(
15        receiver: &mut ReliableReceiver<EntityMessage<E>>,
16        message_index: MessageIndex,
17        message: EntityMessage<E>,
18    ) {
19        receiver.buffer_message(message_index, message);
20    }
21
22    /// Read all buffered [`EntityMessage`] inside the `receiver` and process them.
23    ///
24    /// Outputs the list of [`EntityMessage`] that can be executed now, buffer the rest
25    /// into each entity's `EntityChannelReceiver`
26    pub fn remote_take_incoming_messages<E: Copy + Hash + Eq + Debug>(
27        remote_engine: &mut RemoteEngine<E>,
28        incoming_messages: Vec<(MessageIndex, EntityMessage<E>)>,
29    ) -> Vec<EntityMessage<E>> {
30        for (message_index, message) in incoming_messages {
31            remote_engine.receive_message(message_index, message);
32        }
33        remote_engine.take_incoming_events()
34    }
35
36    /// Feeds `incoming_messages` into `host_engine` and returns all events now ready to apply.
37    // TODO: refactor this to use a generic type for the engine
38    pub fn host_take_incoming_events(
39        host_engine: &mut HostEngine,
40        incoming_messages: Vec<(MessageIndex, EntityMessage<HostEntity>)>,
41    ) -> Vec<EntityMessage<HostEntity>> {
42        for (message_index, message) in incoming_messages {
43            host_engine.receive_message(message_index, message);
44        }
45        host_engine.take_incoming_events()
46    }
47}