diff options
author | HampusM <hampus@hampusmat.com> | 2024-11-11 00:11:22 +0100 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2024-11-11 00:11:22 +0100 |
commit | daf0bc236df25c0e9f44bc3e30839c16cda3f638 (patch) | |
tree | 7475e4e58686dd34366e641ec32f5a9374d66533 /ecs/src/lib.rs | |
parent | 17f63d9859e1c82a30c07bf110cf2b9872e2427e (diff) |
refactor(ecs): use same ID for entities & components
Diffstat (limited to 'ecs/src/lib.rs')
-rw-r--r-- | ecs/src/lib.rs | 24 |
1 files changed, 14 insertions, 10 deletions
diff --git a/ecs/src/lib.rs b/ecs/src/lib.rs index 6725d81..a643bec 100644 --- a/ecs/src/lib.rs +++ b/ecs/src/lib.rs @@ -10,8 +10,8 @@ use std::sync::Arc; use crate::actions::Action; use crate::component::storage::Storage as ComponentStorage; -use crate::component::{Component, Id as ComponentId, Sequence as ComponentSequence}; -use crate::entity::{Uid as EntityUid, CREATE_STATIC_ENTITIES}; +use crate::component::{Component, Sequence as ComponentSequence}; +use crate::entity::CREATE_STATIC_ENTITIES; use crate::event::component::{ create_added_id as create_component_added_event_id, create_removed_id as create_component_removed_event_id, @@ -27,6 +27,7 @@ use crate::stats::Stats; use crate::system::{System, TypeErased as TypeErasedSystem}; use crate::tuple::Reduce as TupleReduce; use crate::type_name::TypeName; +use crate::uid::{Kind as UidKind, Uid}; pub mod actions; pub mod component; @@ -41,6 +42,7 @@ pub mod stats; pub mod system; pub mod tuple; pub mod type_name; +pub mod uid; #[doc(hidden)] pub mod private; @@ -78,12 +80,12 @@ impl World /// /// # Panics /// Will panic if mutable internal lock cannot be acquired. - pub fn create_entity<Comps>(&mut self, components: Comps) -> EntityUid + pub fn create_entity<Comps>(&mut self, components: Comps) -> Uid where Comps: ComponentSequence + TupleReduce<TypeTransformComponentsToAddedEvents>, Comps::Out: EventSequence, { - let entity_uid = EntityUid::new_unique(); + let entity_uid = Uid::new_unique(UidKind::Entity); self.create_entity_with_uid(components, entity_uid); @@ -91,11 +93,13 @@ impl World } #[cfg_attr(feature = "debug", tracing::instrument(skip_all))] - pub fn create_entity_with_uid<Comps>(&self, components: Comps, entity_uid: EntityUid) + pub fn create_entity_with_uid<Comps>(&self, components: Comps, entity_uid: Uid) where Comps: ComponentSequence + TupleReduce<TypeTransformComponentsToAddedEvents>, Comps::Out: EventSequence, { + debug_assert_eq!(entity_uid.kind(), UidKind::Entity); + #[allow(unused_variables)] if let Err(err) = self .data @@ -206,12 +210,12 @@ impl World let component_ids = components .iter() - .map(|component| component.id()) + .map(|component| component.self_id()) .collect::<Vec<_>>(); #[allow(unused_variables)] if let Err(err) = component_storage_lock - .push_entity(EntityUid::new_unique(), components) + .push_entity(Uid::new_unique(UidKind::Entity), components) { #[cfg(feature = "debug")] tracing::error!("Failed to create entity: {err}"); @@ -236,7 +240,7 @@ impl World let component_ids = components .iter() - .map(|component| component.id()) + .map(|component| component.self_id()) .collect::<Vec<_>>(); component_storage_lock @@ -375,7 +379,7 @@ pub struct WorldData #[non_exhaustive] pub struct EntityComponent { - pub id: ComponentId, + pub id: Uid, pub name: &'static str, pub component: Lock<Box<dyn Component>>, } @@ -385,7 +389,7 @@ impl From<Box<dyn Component>> for EntityComponent fn from(component: Box<dyn Component>) -> Self { Self { - id: component.id(), + id: component.self_id(), name: component.type_name(), component: Lock::new(component), } |