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/entity.rs | |
parent | 17f63d9859e1c82a30c07bf110cf2b9872e2427e (diff) |
refactor(ecs): use same ID for entities & components
Diffstat (limited to 'ecs/src/entity.rs')
-rw-r--r-- | ecs/src/entity.rs | 37 |
1 files changed, 4 insertions, 33 deletions
diff --git a/ecs/src/entity.rs b/ecs/src/entity.rs index 18f229a..fff66f5 100644 --- a/ecs/src/entity.rs +++ b/ecs/src/entity.rs @@ -1,43 +1,14 @@ -use std::sync::atomic::{AtomicU64, Ordering}; - use linkme::distributed_slice; use crate::World; -static NEXT_UID: AtomicU64 = AtomicU64::new(0); - -/// Unique entity ID. -#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)] -pub struct Uid -{ - inner: u64, -} - -impl Uid -{ - pub fn new(uid: u64) -> Self - { - debug_assert!( - uid < NEXT_UID.load(Ordering::Relaxed), - "Invalid entity UID {uid}" - ); - - Self { inner: uid } - } - - pub fn new_unique() -> Self - { - Self { - inner: NEXT_UID.fetch_add(1, Ordering::Relaxed), - } - } -} - #[macro_export] macro_rules! static_entity { ($visibility: vis $ident: ident, $components: expr) => { - $visibility static $ident: ::std::sync::LazyLock<$crate::entity::Uid> = - ::std::sync::LazyLock::new(|| $crate::entity::Uid::new_unique()); + $visibility static $ident: ::std::sync::LazyLock<$crate::uid::Uid> = + ::std::sync::LazyLock::new(|| { + $crate::uid::Uid::new_unique($crate::uid::Kind::Entity) + }); $crate::private::paste::paste! { mod [<__ecs_ $ident:lower _static_entity_priv>] { |