feat(ecs): add basic component storage for ECS
Some checks failed
Build Zenyx ⚡ / 🏗️ Build aarch64-pc-windows-msvc (push) Has been cancelled
Build Zenyx ⚡ / 🏗️ Build aarch64-unknown-linux-gnu (push) Has been cancelled
Build Zenyx ⚡ / 🏗️ Build x86_64-pc-windows-msvc (push) Has been cancelled
Build Zenyx ⚡ / 🏗️ Build x86_64-unknown-linux-gnu (push) Has been cancelled
Build Zenyx ⚡ / 🧪 Run Cargo Tests (push) Has been cancelled
Build Zenyx ⚡ / 🧪 Run Cargo Tests (pull_request) Successful in 2m48s
Build Zenyx ⚡ / 🏗️ Build aarch64-pc-windows-msvc (pull_request) Successful in 8m37s
Build Zenyx ⚡ / 🏗️ Build x86_64-pc-windows-msvc (pull_request) Successful in 8m41s
Build Zenyx ⚡ / 🏗️ Build aarch64-unknown-linux-gnu (pull_request) Successful in 8m55s
Build Zenyx ⚡ / 🏗️ Build x86_64-unknown-linux-gnu (pull_request) Successful in 5m5s
Some checks failed
Build Zenyx ⚡ / 🏗️ Build aarch64-pc-windows-msvc (push) Has been cancelled
Build Zenyx ⚡ / 🏗️ Build aarch64-unknown-linux-gnu (push) Has been cancelled
Build Zenyx ⚡ / 🏗️ Build x86_64-pc-windows-msvc (push) Has been cancelled
Build Zenyx ⚡ / 🏗️ Build x86_64-unknown-linux-gnu (push) Has been cancelled
Build Zenyx ⚡ / 🧪 Run Cargo Tests (push) Has been cancelled
Build Zenyx ⚡ / 🧪 Run Cargo Tests (pull_request) Successful in 2m48s
Build Zenyx ⚡ / 🏗️ Build aarch64-pc-windows-msvc (pull_request) Successful in 8m37s
Build Zenyx ⚡ / 🏗️ Build x86_64-pc-windows-msvc (pull_request) Successful in 8m41s
Build Zenyx ⚡ / 🏗️ Build aarch64-unknown-linux-gnu (pull_request) Successful in 8m55s
Build Zenyx ⚡ / 🏗️ Build x86_64-unknown-linux-gnu (pull_request) Successful in 5m5s
This commit is contained in:
parent
ca715d1d67
commit
96d44163fc
3 changed files with 179 additions and 6 deletions
137
src/ecs/mod.rs
Normal file
137
src/ecs/mod.rs
Normal file
|
@ -0,0 +1,137 @@
|
|||
use core::{any::TypeId, num::NonZeroU64, sync::atomic::AtomicU64};
|
||||
use std::{collections::BTreeMap, sync::RwLock};
|
||||
|
||||
use allocator_api2::alloc::Allocator;
|
||||
use bytemuck::Contiguous;
|
||||
|
||||
use crate::collections::SparseSet;
|
||||
|
||||
pub type ECS = EntityComponentSystem;
|
||||
|
||||
#[repr(transparent)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub struct Entity(u64);
|
||||
|
||||
static COMPONENT_ID_CREATOR: AtomicU64 = AtomicU64::new(1);
|
||||
static COMPONENT_IDS: RwLock<BTreeMap<TypeId, ComponentId>> = RwLock::new(BTreeMap::new());
|
||||
|
||||
fn create_component_id<T>() -> ComponentId
|
||||
where
|
||||
T: 'static + Sized,
|
||||
{
|
||||
let type_id = core::any::TypeId::of::<T>();
|
||||
{
|
||||
// unwrap-justification: this only errors if RwLock is poisoned
|
||||
let component_read = COMPONENT_IDS.read().unwrap();
|
||||
if let Some(id) = component_read.get(&type_id) {
|
||||
return *id;
|
||||
}
|
||||
}
|
||||
let new_id = ComponentId(
|
||||
NonZeroU64::new(COMPONENT_ID_CREATOR.fetch_add(1, core::sync::atomic::Ordering::Relaxed))
|
||||
.unwrap(),
|
||||
);
|
||||
{
|
||||
// unwrap-justification: see above
|
||||
let mut component_write = COMPONENT_IDS.write().unwrap();
|
||||
component_write.insert(type_id, new_id);
|
||||
}
|
||||
new_id
|
||||
}
|
||||
|
||||
pub trait ComponentAllocator: Allocator {
|
||||
fn new() -> Self;
|
||||
}
|
||||
|
||||
pub trait Component: core::fmt::Debug + Send + Sized + 'static {
|
||||
type Allocator: ComponentAllocator;
|
||||
|
||||
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);
|
||||
if current_id == 0 {
|
||||
current_id = create_component_id::<Self>().0.into_integer();
|
||||
COMPONENT_ID.store(current_id, core::sync::atomic::Ordering::SeqCst);
|
||||
}
|
||||
ComponentId(NonZeroU64::new(current_id).unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
pub trait ComponentStorage: core::fmt::Debug + 'static {}
|
||||
|
||||
impl<T, SparseAlloc> ComponentStorage for SparseSet<T, T::Allocator, SparseAlloc>
|
||||
where
|
||||
T: Component,
|
||||
SparseAlloc: Allocator + Clone + 'static,
|
||||
{
|
||||
}
|
||||
|
||||
#[repr(transparent)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub struct ComponentId(NonZeroU64);
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ComponentSet<A = allocator_api2::alloc::Global> {
|
||||
sets: SparseSet<Box<dyn ComponentStorage>>,
|
||||
cold_alloc: A,
|
||||
}
|
||||
|
||||
pub struct EntityComponentSystem {
|
||||
components: ComponentSet,
|
||||
}
|
||||
|
||||
impl ComponentSet {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
sets: SparseSet::new(),
|
||||
cold_alloc: allocator_api2::alloc::Global,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<A> ComponentSet<A>
|
||||
where
|
||||
A: Allocator + Clone + 'static,
|
||||
{
|
||||
fn new_in(alloc: A) -> Self {
|
||||
Self {
|
||||
sets: SparseSet::new(),
|
||||
cold_alloc: alloc,
|
||||
}
|
||||
}
|
||||
|
||||
fn get_component_set<T: Component>(&self) -> Option<&SparseSet<T, T::Allocator, A>> {
|
||||
let set = self.sets.get(T::id().0.into_integer() as usize)?;
|
||||
(set as &dyn core::any::Any).downcast_ref()
|
||||
}
|
||||
|
||||
fn get_component_set_mut<T: Component>(
|
||||
&mut self,
|
||||
) -> Option<&mut SparseSet<T, T::Allocator, A>> {
|
||||
let set = self.sets.get_mut(T::id().0.into_integer() as usize)?;
|
||||
(set as &mut dyn core::any::Any).downcast_mut()
|
||||
}
|
||||
|
||||
fn insert_component_set<T: Component>(&mut self) -> &mut SparseSet<T, T::Allocator, A> {
|
||||
if self.sets.contains(T::id().0.into_integer() as usize) {
|
||||
self.get_component_set_mut::<T>().unwrap()
|
||||
} else {
|
||||
let set = SparseSet::<T, _, _>::new_in(T::Allocator::new(), self.cold_alloc.clone());
|
||||
self.sets
|
||||
.insert(T::id().0.into_integer() as usize, Box::new(set) as Box<_>);
|
||||
self.get_component_set_mut::<T>().unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
fn add_to_entity<T: Component>(&mut self, entity: Entity, data: T) -> Option<T> {
|
||||
let set = self.insert_component_set::<T>();
|
||||
set.insert(entity.0 as usize, data)
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for ComponentSet {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue