diff --git a/src/ecs/mod.rs b/src/ecs/mod.rs index bb2500a..4069d90 100644 --- a/src/ecs/mod.rs +++ b/src/ecs/mod.rs @@ -27,6 +27,7 @@ where return *id; } } + let new_id = ComponentId( NonZeroU64::new(COMPONENT_ID_CREATOR.fetch_add(1, core::sync::atomic::Ordering::Relaxed)) .unwrap(), @@ -48,11 +49,10 @@ pub trait Component: core::fmt::Debug + Send + Sized + 'static { fn id() -> ComponentId { static COMPONENT_ID: AtomicU64 = AtomicU64::new(0); - // TODO: reevaluate ordering later - let mut current_id = COMPONENT_ID.load(core::sync::atomic::Ordering::SeqCst); + let mut current_id = COMPONENT_ID.load(core::sync::atomic::Ordering::Relaxed); if current_id == 0 { current_id = create_component_id::().0.into_integer(); - COMPONENT_ID.store(current_id, core::sync::atomic::Ordering::SeqCst); + COMPONENT_ID.store(current_id, core::sync::atomic::Ordering::Relaxed); } ComponentId(NonZeroU64::new(current_id).unwrap()) } @@ -79,6 +79,16 @@ pub struct ComponentSet { pub struct EntityComponentSystem { components: ComponentSet, + next_id: AtomicU64, +} + +impl EntityComponentSystem { + fn spawn(&mut self) -> Entity { + let entity_id = self + .next_id + .fetch_add(1, core::sync::atomic::Ordering::Relaxed); + Entity(entity_id) + } } impl ComponentSet {