use nightshade::prelude::*;
pub fn tag(world: &mut World, entity: Entity, label: &str) {
let tags = world.resources.entities.tags.entry(entity).or_default();
if !tags.iter().any(|existing| existing == label) {
tags.push(label.to_string());
}
}
pub fn untag(world: &mut World, entity: Entity, label: &str) {
if let Some(tags) = world.resources.entities.tags.get_mut(&entity) {
tags.retain(|existing| existing != label);
}
}
pub fn has_tag(world: &World, entity: Entity, label: &str) -> bool {
world
.resources
.entities
.tags
.get(&entity)
.is_some_and(|tags| tags.iter().any(|existing| existing == label))
}
pub fn tagged(world: &World, label: &str) -> Vec<Entity> {
world
.resources
.entities
.tags
.iter()
.filter(|(_, tags)| tags.iter().any(|existing| existing == label))
.map(|(&entity, _)| entity)
.collect()
}
pub fn for_each_tagged(world: &mut World, label: &str, mut action: impl FnMut(&mut World, Entity)) {
let entities = tagged(world, label);
for entity in entities {
action(world, entity);
}
}
pub fn for_each_with_position(world: &World, mut action: impl FnMut(Entity, Vec3)) {
world
.core
.query()
.with(LOCAL_TRANSFORM | GLOBAL_TRANSFORM)
.iter(|entity, table, index| {
action(entity, table.global_transform[index].translation());
});
}