use nightshade::prelude::*;
pub fn tag(world: &mut World, entity: Entity, label: &str) {
let tags = world
.res_mut::<nightshade::ecs::entity_registry::EntityRegistry>()
.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
.res_mut::<nightshade::ecs::entity_registry::EntityRegistry>()
.tags
.get_mut(&entity)
{
tags.retain(|existing| existing != label);
}
}
pub fn has_tag(world: &World, entity: Entity, label: &str) -> bool {
world
.res::<nightshade::ecs::entity_registry::EntityRegistry>()
.tags
.get(&entity)
.is_some_and(|tags| tags.iter().any(|existing| existing == label))
}
pub fn tagged(world: &World, label: &str) -> Vec<Entity> {
world
.res::<nightshade::ecs::entity_registry::EntityRegistry>()
.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)) {
for (entity, global_transform) in world
.query_ref::<&nightshade::ecs::transform::components::GlobalTransform>()
.with::<nightshade::ecs::transform::components::LocalTransform>()
.iter()
{
action(entity, global_transform.translation());
}
}