use bevy_ecs::prelude::*;
use bevy_ecs::query::QueryFilter;
use bevy_reflect::Reflect;
#[derive(Component, Default)]
pub struct NeedsAssetCollection;
#[derive(Clone, Debug)]
pub struct AssetPart {
pub entity: Entity,
pub part_of: AssetPartOf,
}
impl AssetPart {
pub fn new(entity: Entity, part_of: AssetPartOf) -> Self {
Self { entity, part_of }
}
}
impl From<(Entity, &AssetPartOf)> for AssetPart {
fn from((entity, part_of): (Entity, &AssetPartOf)) -> Self {
Self::new(entity, part_of.clone())
}
}
#[derive(Reflect, Eq, PartialEq, Hash, Clone, Debug, Component)]
#[reflect(Component, Clone, Debug, PartialEq, Hash)]
pub struct AssetPartOf {
pub item: Entity,
pub root: Entity,
pub layer: Entity,
pub name: Option<Name>,
}
impl AssetPartOf {
pub fn new(item: Entity, root: Entity, layer: Entity) -> Self {
Self {
item,
root,
layer,
name: None,
}
}
pub fn with_name_from_queries<F: QueryFilter, P: QueryFilter>(
mut self,
entity: Entity,
q_name: &Query<&Name, F>,
q_parent: &Query<&ChildOf, P>,
) -> Self {
self.name = q_name
.get(self.item)
.or_else(|_| q_parent.get(entity).and_then(|x| q_name.get(x.parent())))
.or_else(|_| q_name.get(entity))
.ok()
.or(self.name.as_ref())
.cloned();
self
}
}