bevy_yoleck/entity_uuid.rs
1use bevy::prelude::*;
2
3use bevy::platform::collections::HashMap;
4use uuid::Uuid;
5
6/// A UUID automatically added to entity types defined with
7/// [`with_uuid`](crate::YoleckEntityType::with_uuid)
8///
9/// This UUID can be used to refer to the entity in a persistent way - e.g. from a
10/// [`YoleckComponent`](crate::prelude::YoleckComponent) of another entity. The `Entity` ID itself
11/// will change between runs, but the UUID can reliably store the connection between the entities
12/// in the `.yol` file.
13///
14/// To find an entity by UUID use [`YoleckUuidRegistry`].
15#[derive(Component, Debug)]
16pub struct YoleckEntityUuid(pub(crate) Uuid);
17
18impl YoleckEntityUuid {
19 pub fn get(&self) -> Uuid {
20 self.0
21 }
22}
23
24/// Helper registry for finding [`with_uuid`](crate::YoleckEntityType::with_uuid) defined entities
25/// by their UUID.
26///
27/// To find a UUID given the `Entity` - check its [`YoleckEntityUuid`] component.
28#[derive(Resource)]
29pub struct YoleckUuidRegistry(pub(crate) HashMap<Uuid, Entity>);
30
31impl YoleckUuidRegistry {
32 pub fn get(&self, uuid: Uuid) -> Option<Entity> {
33 self.0.get(&uuid).copied()
34 }
35}