feat(ecs): entity spawning
All checks were successful
Build Zenyx ⚡ / 🧪 Run Cargo Tests (push) Successful in 4m9s
Build Zenyx ⚡ / 🧪 Run Cargo Tests (pull_request) Successful in 4m14s
Build Zenyx ⚡ / 🏗️ Build x86_64-unknown-linux-gnu (push) Successful in 8m12s
Build Zenyx ⚡ / 🏗️ Build aarch64-unknown-linux-gnu (push) Successful in 9m13s
Build Zenyx ⚡ / 🏗️ Build x86_64-pc-windows-msvc (push) Successful in 9m11s
Build Zenyx ⚡ / 🏗️ Build aarch64-pc-windows-msvc (push) Successful in 9m27s
Build Zenyx ⚡ / 🏗️ Build aarch64-pc-windows-msvc (pull_request) Successful in 7m51s
Build Zenyx ⚡ / 🏗️ Build x86_64-pc-windows-msvc (pull_request) Successful in 8m39s
Build Zenyx ⚡ / 🏗️ Build x86_64-unknown-linux-gnu (pull_request) Successful in 8m28s
Build Zenyx ⚡ / 🏗️ Build aarch64-unknown-linux-gnu (pull_request) Successful in 9m7s

This commit is contained in:
Chance 2025-05-01 20:00:06 -04:00
parent 96d44163fc
commit 1b89120b73
Signed by: caznix
GPG key ID: 489D213143D753FD

View file

@ -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::<Self>().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<A = allocator_api2::alloc::Global> {
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 {