source2_demo/entity/
mod.rs

1mod baseline;
2mod class;
3mod container;
4
5pub(crate) use baseline::*;
6pub(crate) mod field;
7pub use class::*;
8pub use container::*;
9
10use crate::error::EntityError;
11use crate::field::{FieldPath, FieldState};
12use crate::FieldValue;
13use std::rc::Rc;
14
15#[derive(Debug, Clone, Copy, Eq, PartialEq)]
16pub enum EntityEvents {
17    Created,
18    Updated,
19    Deleted,
20}
21
22impl EntityEvents {
23    #[inline]
24    pub(crate) fn from_cmd(cmd: u32) -> Self {
25        match cmd {
26            0 => EntityEvents::Updated,
27            2 => EntityEvents::Created,
28            3 => EntityEvents::Deleted,
29            _ => unreachable!(),
30        }
31    }
32}
33
34#[derive(Clone)]
35pub struct Entity {
36    pub(crate) index: u32,
37    pub(crate) serial: u32,
38    pub(crate) class: Rc<Class>,
39    pub(crate) state: FieldState,
40}
41
42impl Default for Entity {
43    fn default() -> Self {
44        Entity {
45            index: u32::MAX,
46            serial: 0,
47            class: Class::default().into(),
48            state: FieldState::default(),
49        }
50    }
51}
52
53impl Entity {
54    pub fn index(&self) -> u32 {
55        self.index
56    }
57
58    pub fn serial(&self) -> u32 {
59        self.serial
60    }
61
62    pub fn handle(&self) -> u32 {
63        self.serial << 14 | self.index
64    }
65
66    pub fn class(&self) -> &Class {
67        &self.class
68    }
69
70    /// Returns [`FieldValue`] for a given property name. You can also use
71    /// [`property!`] and [`try_property!`] macros.
72    ///
73    /// # Examples
74    ///
75    /// ```
76    /// use source2_demo::prelude::*;
77    ///
78    /// #[derive(Default)]
79    /// struct MyObs;
80    ///
81    /// impl Observer for MyObs {
82    ///     fn on_entity(
83    ///         &mut self,
84    ///         ctx: &Context,
85    ///         event: EntityEvents,
86    ///         entity: &Entity,
87    ///     ) -> ObserverResult {
88    ///         let x: u8 = entity
89    ///             .get_property_by_name("CBodyComponent.m_cellX")?
90    ///             .try_into()?;
91    ///
92    ///         let y: u8 = property!(entity, "CBodyComponent.m_cellY");
93    ///
94    ///         let z = property!(entity, u8, "CBodyComponent.m_cellY");
95    ///
96    ///         Ok(())
97    ///     }
98    /// }
99    /// ```
100    ///
101    /// [`property!`]: crate::property
102    /// [`try_property!`]: crate::try_property
103    pub fn get_property_by_name(&self, name: &str) -> Result<&FieldValue, EntityError> {
104        self.get_property_by_field_path(&self.class.serializer.get_field_path_for_name(name)?)
105    }
106
107    pub(crate) fn get_property_by_field_path(
108        &self,
109        fp: &FieldPath,
110    ) -> Result<&FieldValue, EntityError> {
111        self.state.get_value(fp).ok_or_else(|| {
112            EntityError::PropertyNameNotFound(
113                self.class.serializer.get_name_for_field_path(fp),
114                self.class.name().to_string(),
115                format!("{}", fp),
116            )
117        })
118    }
119}