use crate::ecs::world::CORE;
use crate::ecs::world::World;
use freecs::Entity;
use std::collections::HashMap;
#[derive(Default, Clone)]
pub struct EntityRegistry {
pub names: HashMap<String, Entity>,
pub tags: HashMap<Entity, Vec<String>>,
pub next_guid: u64,
pub guid_index: HashMap<u64, Entity>,
}
pub fn ensure_guid(world: &mut World, entity: Entity) -> crate::ecs::primitives::Guid {
if let Some(existing) = world.get::<crate::ecs::primitives::Guid>(entity).copied() {
world
.res_mut::<crate::ecs::entity_registry::EntityRegistry>()
.guid_index
.entry(existing.0)
.or_insert(entity);
return existing;
}
world
.res_mut::<crate::ecs::entity_registry::EntityRegistry>()
.next_guid = world
.res_mut::<crate::ecs::entity_registry::EntityRegistry>()
.next_guid
.saturating_add(1);
let guid = crate::ecs::primitives::Guid(
world
.res::<crate::ecs::entity_registry::EntityRegistry>()
.next_guid,
);
if !world.ecs.worlds[CORE].entity_has_components(entity, crate::ecs::world::GUID) {
world.ecs.worlds[CORE].add_components(entity, crate::ecs::world::GUID);
}
world.set(entity, guid);
world
.res_mut::<crate::ecs::entity_registry::EntityRegistry>()
.guid_index
.insert(guid.0, entity);
guid
}
pub fn assign_guid(world: &mut World, entity: Entity, guid: crate::ecs::primitives::Guid) {
if !world.ecs.worlds[CORE].entity_has_components(entity, crate::ecs::world::GUID) {
world.ecs.worlds[CORE].add_components(entity, crate::ecs::world::GUID);
}
world.set(entity, guid);
world
.res_mut::<crate::ecs::entity_registry::EntityRegistry>()
.guid_index
.insert(guid.0, entity);
if guid.0
> world
.res::<crate::ecs::entity_registry::EntityRegistry>()
.next_guid
{
world
.res_mut::<crate::ecs::entity_registry::EntityRegistry>()
.next_guid = guid.0;
}
}
pub fn find_entity_by_guid(world: &World, guid: crate::ecs::primitives::Guid) -> Option<Entity> {
world
.res::<crate::ecs::entity_registry::EntityRegistry>()
.guid_index
.get(&guid.0)
.copied()
}