Skip to main content

moonshine_object/
tags.rs

1use bevy_ecs::relationship::Relationship;
2use moonshine_kind::prelude::*;
3use moonshine_tag::Tags;
4
5use crate::{Object, ObjectRef, ObjectWorldRef};
6
7/// [`Object`] methods related to accessing [`Tags`].
8pub trait ObjectTags {
9    /// Returns the [`Tags`] of this [`Object`].
10    ///
11    /// For convenience, if the object has no tags, [`Tags::static_empty`] is returned instead.
12    fn tags(&self) -> &Tags;
13}
14
15impl<T: Kind, R: Relationship> ObjectTags for Object<'_, '_, '_, T, R> {
16    fn tags(&self) -> &Tags {
17        self.nametags
18            .get(self.entity())
19            .ok()
20            .and_then(|(_name, tags)| tags)
21            .unwrap_or(Tags::static_empty())
22    }
23}
24
25impl<T: Kind, R: Relationship> ObjectTags for ObjectRef<'_, '_, '_, T, R> {
26    fn tags(&self) -> &Tags {
27        self.1.tags()
28    }
29}
30
31impl<T: Kind, R: Relationship> ObjectTags for ObjectWorldRef<'_, T, R> {
32    fn tags(&self) -> &Tags {
33        self.world
34            .get(self.entity())
35            .unwrap_or(Tags::static_empty())
36    }
37}