Skip to main content

naia_shared/world/update/
component_update.rs

1use naia_serde::{BitReader, OwnedBitReader};
2
3use crate::{
4    world::component::component_kinds::ComponentKind,
5    world::component::replicate::SplitUpdateResult,
6    ComponentKinds,
7    LocalEntityAndGlobalEntityConverter,
8};
9
10/// A serialised component-field update payload together with its [`ComponentKind`] tag.
11pub struct ComponentUpdate {
12    /// The kind of component this update applies to.
13    pub kind: ComponentKind,
14    buffer: OwnedBitReader,
15}
16
17impl ComponentUpdate {
18    /// Creates a new `ComponentUpdate` wrapping `buffer` for the given `kind`.
19    pub fn new(kind: ComponentKind, buffer: OwnedBitReader) -> Self {
20        Self { kind, buffer }
21    }
22
23    /// Borrows the payload buffer as a [`BitReader`] for field deserialization.
24    pub fn reader(&'_ self) -> BitReader<'_> {
25        self.buffer.borrow()
26    }
27
28    pub(crate) fn split_into_waiting_and_ready(
29        self,
30        converter: &dyn LocalEntityAndGlobalEntityConverter,
31        component_kinds: &ComponentKinds,
32    ) -> SplitUpdateResult {
33        let kind = self.kind;
34        component_kinds.split_update(converter, &kind, self)
35    }
36}
37
38/// A single serialised field update payload for a component, identified by field index.
39pub struct ComponentFieldUpdate {
40    id: u8,
41    buffer: OwnedBitReader,
42}
43
44impl ComponentFieldUpdate {
45    /// Creates a `ComponentFieldUpdate` for field `id` with the given serialized `buffer`.
46    pub fn new(id: u8, buffer: OwnedBitReader) -> Self {
47        Self { id, buffer }
48    }
49
50    /// Returns the field index this update targets.
51    pub fn field_id(&self) -> u8 {
52        self.id
53    }
54
55    /// Borrows the field payload as a [`BitReader`] for deserialization.
56    pub fn reader(&'_ self) -> BitReader<'_> {
57        self.buffer.borrow()
58    }
59}