moonshine_object/
instance.rs

1use bevy_ecs::prelude::*;
2use moonshine_kind::{prelude::*, Any};
3
4use crate::{Object, ObjectRef};
5
6pub trait ObjectInstance<T: Kind = Any> {
7    /// Returns the [`Instance`] of this object.
8    ///
9    /// # Example
10    /// ```
11    /// # use bevy::prelude::*;
12    /// # use moonshine_object::prelude::*;
13    ///
14    /// let mut app = App::new();
15    /// // ...
16    /// app.add_systems(Update, print_instances);
17    ///
18    /// fn print_instances(objects: Objects) {
19    ///     for object in objects.iter() {
20    ///         println!("{:?}", object.instance());
21    ///     }
22    /// }
23    /// ```
24    fn instance(&self) -> Instance<T>;
25
26    /// Returns the [`Entity`] of this object.
27    fn entity(&self) -> Entity {
28        self.instance().entity()
29    }
30}
31
32impl<T: Kind> ObjectInstance<T> for Object<'_, '_, '_, T> {
33    fn instance(&self) -> Instance<T> {
34        self.instance
35    }
36}
37
38impl<T: Kind> ObjectInstance<T> for ObjectRef<'_, '_, '_, T> {
39    fn instance(&self) -> Instance<T> {
40        self.1.instance()
41    }
42}