1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
use bevy_ecs::prelude::*;
use moonshine_kind::{prelude::*, Any};

use crate::{Object, ObjectRef};

pub trait ObjectInstance<T: Kind = Any> {
    /// Returns the [`Instance`] of this object.
    ///
    /// # Example
    /// ```
    /// # use bevy::prelude::*;
    /// # use moonshine_object::prelude::*;
    ///
    /// let mut app = App::new();
    /// // ...
    /// app.add_systems(Update, print_instances);
    ///
    /// fn print_instances(objects: Objects) {
    ///     for object in objects.iter() {
    ///         println!("{:?}", object.instance());
    ///     }
    /// }
    /// ```
    fn instance(&self) -> Instance<T>;

    /// Returns the [`Entity`] of this object.
    fn entity(&self) -> Entity {
        self.instance().entity()
    }
}

impl<'w, 's, 'a, T: Kind> ObjectInstance<T> for Object<'w, 's, 'a, T> {
    fn instance(&self) -> Instance<T> {
        self.instance
    }
}

impl<'w, 's, 'a, T: Kind> ObjectInstance<T> for ObjectRef<'w, 's, 'a, T> {
    fn instance(&self) -> Instance<T> {
        self.1.instance()
    }
}