lyra-engine/lyra-ecs/src/archetype.rs

144 lines
4.3 KiB
Rust
Raw Normal View History

use std::{any::TypeId, ptr::{NonNull, self}, alloc::{self, Layout, alloc}};
2023-05-25 04:11:16 +00:00
use crate::{world::{Entity, ArchetypeEntityId}, bundle::Bundle, component_info::ComponentInfo};
2023-05-25 04:11:16 +00:00
2023-11-25 23:43:11 +00:00
pub struct ComponentColumn {
pub data: NonNull<u8>,
pub capacity: usize,
pub info: ComponentInfo,
pub entry_size: usize,
2023-05-25 04:11:16 +00:00
}
#[allow(dead_code)]
2023-11-25 23:43:11 +00:00
impl ComponentColumn {
/// Creates an invalid component column. Do not attempt to use it.
pub unsafe fn dangling() -> Self {
ComponentColumn {
data: NonNull::dangling(),
capacity: 0,
info: ComponentInfo::new::<()>(),
entry_size: 0,
}
2023-05-25 04:11:16 +00:00
}
2023-11-25 23:43:11 +00:00
pub unsafe fn alloc(component_layout: Layout, capacity: usize) -> NonNull<u8> {
let new_layout = Layout::from_size_align(
component_layout.size().checked_mul(capacity).unwrap(),
component_layout.align()
).unwrap();
if let Some(data) = NonNull::new(alloc(new_layout)) {
data
} else {
alloc::handle_alloc_error(new_layout)
}
2023-05-25 04:11:16 +00:00
}
2023-11-25 23:43:11 +00:00
pub unsafe fn new(info: ComponentInfo, capacity: usize) -> Self {
let data = ComponentColumn::alloc(info.layout, capacity);
let size = info.layout.size();
Self {
data,
capacity,
info,
entry_size: size,
}
2023-05-25 04:11:16 +00:00
}
2023-11-25 23:43:11 +00:00
/// Set a component from pointer at an entity index.
///
/// # Safety
///
/// This column must have space to fit the component, if it does not have room it will panic.
pub unsafe fn set_at(&mut self, entity_index: usize, comp_src: NonNull<u8>) {
assert!(entity_index < self.capacity);
let dest = NonNull::new_unchecked(self.data.as_ptr().add(entity_index * self.entry_size));
ptr::copy_nonoverlapping(comp_src.as_ptr(), dest.as_ptr(), self.entry_size);
}
/// Get a component at an entities index.
///
/// # Safety
///
/// This column MUST have the entity. If it does not, it WILL NOT panic and will cause UB.
pub unsafe fn get<T>(&self, entity_index: usize) -> &T {
let p = self.data.as_ptr()
.cast::<T>()
.add(entity_index * self.entry_size);
&*p
}
/// Get a component at an entities index.
///
/// # Safety
///
/// This column must have the entity.
pub unsafe fn get_mut<T>(&mut self, entity_index: usize) -> &mut T {
let p = self.data.as_ptr()
.cast::<T>()
.add(entity_index * self.entry_size);
&mut *p
2023-05-25 04:11:16 +00:00
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct ArchetypeId(pub u64);
impl ArchetypeId {
/// Increments the id and returns a new id with the value it was before incrementing.
pub(crate) fn increment(&mut self) -> Self {
let v = self.0;
self.0 += 1;
ArchetypeId(v)
}
}
pub struct Archetype {
pub(crate) id: ArchetypeId,
2023-11-25 23:43:11 +00:00
pub(crate) entities: Vec<Entity>,
pub(crate) columns: Vec<ComponentColumn>,
2023-05-25 04:11:16 +00:00
}
2023-11-25 23:43:11 +00:00
/// The default capacity of the columns
const DEFAULT_CAPACITY: usize = 32;
2023-05-25 04:11:16 +00:00
impl Archetype {
2023-11-25 23:43:11 +00:00
pub fn from_bundle_info(new_id: ArchetypeId, bundle_info: Vec<ComponentInfo>) -> Archetype {
let columns = bundle_info.into_iter().map(|i| {
unsafe { ComponentColumn::new(i, DEFAULT_CAPACITY) }
}).collect();
2023-05-25 04:11:16 +00:00
Archetype {
id: new_id,
entities: Vec::new(),
columns,
}
}
2023-11-25 23:43:11 +00:00
/// Add an entity and its component bundle to the Archetype
///
/// # Safety:
///
/// Archetype must contain all of the components
pub(crate) fn add_entity<B>(&mut self, entity: Entity, bundle: B) -> ArchetypeEntityId
where
B: Bundle
{
let entity_index = self.entities.len();
self.entities.push(entity);
bundle.take(|data, type_id, _size| {
2023-11-25 23:43:11 +00:00
let col = self.columns.iter_mut().find(|c| c.info.type_id == type_id).unwrap();
unsafe { col.set_at(entity_index, data); }
});
ArchetypeEntityId(entity_index as u64)
2023-05-25 04:11:16 +00:00
}
/// Returns a boolean indicating whether this archetype can store the TypeIds given
2023-05-25 04:11:16 +00:00
pub(crate) fn is_archetype_for(&self, types: Vec<TypeId>) -> bool {
2023-11-25 23:43:11 +00:00
self.columns.iter().all(|c| types.contains(&c.info.type_id))
2023-05-25 04:11:16 +00:00
}
}