WIP: feat(ecs): add basic sparse set per component ECS #17

Draft
bitsyndicate wants to merge 2 commits from ecs into main
Showing only changes of commit 1b89120b73 - Show all commits

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 {