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

747 lines
26 KiB
Rust
Raw Normal View History

2024-03-03 01:20:38 +00:00
use std::{ptr::{NonNull, self}, alloc::{self, Layout, alloc, dealloc}, mem, collections::HashMap, cell::{RefCell, Ref, RefMut, BorrowError, BorrowMutError}, ops::DerefMut};
2023-05-25 04:11:16 +00:00
use crate::{world::ArchetypeEntityId, bundle::Bundle, component_info::ComponentInfo, DynTypeId, Tick, Entity};
2023-05-25 04:11:16 +00:00
#[derive(Clone)]
2023-11-25 23:43:11 +00:00
pub struct ComponentColumn {
pub data: RefCell<NonNull<u8>>,
pub len: usize,
pub capacity: usize,
2023-11-25 23:43:11 +00:00
pub info: ComponentInfo,
2023-12-26 19:12:53 +00:00
/// The last tick that this component was modified at.
pub entity_ticks: Vec<Tick>,
}
impl Drop for ComponentColumn {
fn drop(&mut self) {
let data = self.data.borrow_mut();
let data = data.as_ptr();
unsafe {
2024-03-03 02:34:20 +00:00
// TODO: trigger drop on the components
// SAFETY: The size of the data buffer is the capcity times the size of a component
let size = self.info.layout().size() * self.capacity;
let layout = Layout::from_size_align_unchecked(size, self.info.layout().align());
dealloc(data, layout);
}
}
2023-05-25 04:11:16 +00:00
}
#[allow(dead_code)]
2023-11-25 23:43:11 +00:00
impl ComponentColumn {
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 {
2024-03-03 02:34:20 +00:00
let data = ComponentColumn::alloc(info.layout(), capacity);
2023-11-25 23:43:11 +00:00
Self {
data: RefCell::new(data),
2023-11-25 23:43:11 +00:00
capacity,
info,
2023-11-27 02:05:35 +00:00
len: 0,
2023-12-26 19:12:53 +00:00
entity_ticks: Vec::new(),
2023-11-25 23:43:11 +00:00
}
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>, tick: Tick) {
2023-11-25 23:43:11 +00:00
assert!(entity_index < self.capacity);
let mut data = self.data.borrow_mut();
let data = data.deref_mut();
2024-03-03 02:34:20 +00:00
let size = self.info.layout().size();
2024-03-03 02:20:19 +00:00
let dest = NonNull::new_unchecked(data.as_ptr().add(entity_index * size));
ptr::copy_nonoverlapping(comp_src.as_ptr(), dest.as_ptr(), size);
2023-12-26 19:12:53 +00:00
// check if a component spot is being set twice and that the entity's tick is
// already stored
if self.entity_ticks.len() > entity_index {
self.entity_ticks[entity_index].tick_to(&tick);
} else {
self.entity_ticks.push(tick);
}
2023-11-25 23:43:11 +00:00
}
/// Inserts an entity and its component at a specific index.
pub unsafe fn insert_entity(&mut self, entity_index: usize, comp_src: NonNull<u8>, tick: Tick) {
assert!(entity_index < self.capacity);
let mut data = self.data.borrow_mut();
let data = data.deref_mut();
let size = self.info.layout().size();
let dest = NonNull::new_unchecked(data.as_ptr().add(entity_index * size));
ptr::copy_nonoverlapping(comp_src.as_ptr(), dest.as_ptr(), size);
// check if a component spot is being set twice and that the entity's tick is
// already stored
self.entity_ticks.push(tick);
self.len += 1;
}
2023-11-25 23:43:11 +00:00
/// 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.
2024-03-03 01:20:38 +00:00
pub unsafe fn get<T>(&self, entity_index: usize) -> Ref<T> {
let data = self.data.borrow();
2024-03-03 01:20:38 +00:00
Ref::map(data, |data| {
let ptr = NonNull::new_unchecked(data.as_ptr()
2024-03-03 02:34:20 +00:00
.add(entity_index * self.info.layout().size()))
2024-03-03 01:20:38 +00:00
.cast();
&*ptr.as_ptr()
})
2023-11-25 23:43:11 +00:00
}
2024-03-03 02:34:20 +00:00
/// Get a mutable borrow to the component at an entities index, ticking the entity.
2023-11-25 23:43:11 +00:00
///
/// # Safety
///
/// This column must have the entity.
2024-03-03 02:34:20 +00:00
pub unsafe fn get_mut<T>(&mut self, entity_index: usize, tick: &Tick) -> RefMut<T> {
self.entity_ticks[entity_index].tick_to(tick);
2023-12-26 19:12:53 +00:00
2024-03-03 02:34:20 +00:00
let data = self.data.borrow_mut();
2024-03-03 02:34:20 +00:00
RefMut::map(data, |data| {
let ptr = NonNull::new_unchecked(data.as_ptr()
.add(entity_index * self.info.layout().size()))
.cast();
&mut *ptr.as_ptr()
})
2023-05-25 04:11:16 +00:00
}
2023-11-27 02:05:35 +00:00
/// Grow the column to fit `new_capacity` amount of components.
///
/// Parameters:
/// * `new_capacity` - The new capacity of components that can fit in this column.
///
2023-12-26 19:12:53 +00:00
/// Note: This does not modify the Tick of this column, since no components were actually modified.
///
2023-11-27 02:05:35 +00:00
/// # Safety
///
/// Will panic if `new_capacity` is less than the current capacity of the column.
pub unsafe fn grow(&mut self, new_capacity: usize) {
assert!(new_capacity > self.capacity);
let mut data = self.data.borrow_mut();
2024-03-03 02:34:20 +00:00
let layout = self.info.layout();
let mut new_ptr = Self::alloc(layout, new_capacity);
2023-11-27 02:05:35 +00:00
if self.len > 0 {
2024-03-03 02:34:20 +00:00
ptr::copy_nonoverlapping(data.as_ptr(), new_ptr.as_ptr(), self.len * layout.size());
}
2023-11-27 02:05:35 +00:00
// dont attempt to free if we weren't able to store anything anyway
if self.capacity != 0 {
2024-03-03 02:34:20 +00:00
// create a layout with the same alignment, but expand the size of the buffer.
let old_layout = Layout::from_size_align_unchecked(
2024-03-03 02:34:20 +00:00
layout.size().checked_mul(self.capacity).unwrap(),
layout.align()
);
mem::swap(data.deref_mut(), &mut new_ptr);
dealloc(new_ptr.as_ptr(), old_layout);
} else {
*data = new_ptr;
}
2023-11-27 02:05:35 +00:00
self.capacity = new_capacity;
}
/// Removes a component from the column, freeing it, and returning the old index of the entity that took its place in the column.
2023-12-26 19:12:53 +00:00
pub unsafe fn remove_component(&mut self, entity_index: usize, tick: &Tick) -> Option<usize> {
let _ = tick; // may be used at some point
debug_assert!(self.len > 0, "There are no entities in the Archetype to remove from!");
2023-12-26 19:12:53 +00:00
let mut data = self.data.borrow_mut();
let data = data.deref_mut();
2024-03-03 02:34:20 +00:00
let size = self.info.layout().size();
let mut old_comp_ptr = NonNull::new_unchecked(data.as_ptr()
2024-03-03 02:20:19 +00:00
.add(entity_index * size));
2023-11-27 02:05:35 +00:00
let moved_index = if entity_index != self.len - 1 {
let moved_index = self.len - 1;
let mut new_comp_ptr = NonNull::new_unchecked(data.as_ptr()
2024-03-03 02:20:19 +00:00
.add(moved_index * size));
2023-11-27 02:05:35 +00:00
2024-03-03 02:20:19 +00:00
ptr::copy_nonoverlapping(new_comp_ptr.as_ptr(), old_comp_ptr.as_ptr(), size);
2023-11-27 02:05:35 +00:00
mem::swap(&mut old_comp_ptr, &mut new_comp_ptr); // new_comp_ptr is now the old ptr
2023-12-26 19:12:53 +00:00
// make sure to keep entity indexes correct in the ticks list as well
self.entity_ticks.swap_remove(entity_index);
2023-12-26 19:12:53 +00:00
2023-11-27 02:05:35 +00:00
Some(moved_index)
} else { None };
self.len -= 1;
moved_index
}
pub fn borrow_ptr(&self) -> Ref<NonNull<u8>> {
self.data.borrow()
}
pub fn borrow_mut_ptr(&self) -> RefMut<NonNull<u8>> {
self.data.borrow_mut()
}
pub fn try_borrow_ptr(&self) -> Result<Ref<NonNull<u8>>, BorrowError> {
self.data.try_borrow()
}
pub fn try_borrow_mut_ptr(&self) -> Result<RefMut<NonNull<u8>>, BorrowMutError> {
self.data.try_borrow_mut()
}
2023-05-25 04:11:16 +00:00
}
2024-03-03 02:20:19 +00:00
/// An id of an Archetype
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Default)]
2023-05-25 04:11:16 +00:00
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)
}
}
2024-03-03 02:20:19 +00:00
/// Stores a group of entities with matching components.
///
/// An Archetype can be thought of as a table, with entities as the rows and the entity's
/// components as each column. This means you can have tightly packed components of entities and
/// quickly iterate through entities with the same components.
#[derive(Clone, Default)]
2023-05-25 04:11:16 +00:00
pub struct Archetype {
2024-03-03 02:20:19 +00:00
id: ArchetypeId,
/// The indexes of the entities in the archetype.
pub(crate) entity_ids: HashMap<Entity, ArchetypeEntityId>,
/// map an Archetype entity id to an entity
2024-03-03 02:20:19 +00:00
//pub(crate) ids_to_entity: HashMap<ArchetypeEntityId, Entity>,
/// The entities in the Archetype.
///
/// Can be used to map `ArchetypeEntityId` to an Entity since `ArchetypeEntityId` has
/// the index that the entity is stored at.
pub(crate) entities: Vec<Entity>,
2023-11-25 23:43:11 +00:00
pub(crate) columns: Vec<ComponentColumn>,
2024-03-03 02:20:19 +00:00
capacity: usize,
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 {
2024-03-03 02:20:19 +00:00
/// Returns the id of the Archetype
pub fn id(&self) -> ArchetypeId {
self.id
}
/// Returns the max amount of Entities that the Archetype can store without reallocating.
pub fn capacity(&self) -> usize {
self.capacity
}
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,
2024-03-03 02:20:19 +00:00
entity_ids: HashMap::new(),
entities: Vec::new(),
2023-05-25 04:11:16 +00:00
columns,
2023-11-27 02:05:35 +00:00
capacity: DEFAULT_CAPACITY,
2023-05-25 04:11:16 +00:00
}
}
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
2023-12-26 19:12:53 +00:00
pub(crate) fn add_entity<B>(&mut self, entity: Entity, bundle: B, tick: &Tick) -> ArchetypeEntityId
2023-11-25 23:43:11 +00:00
where
B: Bundle
{
2024-03-03 02:20:19 +00:00
if self.capacity == self.entity_ids.len() {
2023-11-27 02:05:35 +00:00
let new_cap = self.capacity * 2;
self.grow_columns(new_cap);
self.capacity = new_cap;
}
2024-03-03 02:20:19 +00:00
debug_assert_eq!(self.entity_ids.len(), self.entities.len(), "Somehow the Archetype's entity storage got unsynced");
let entity_index = ArchetypeEntityId(self.entity_ids.len() as u64);
self.entity_ids.insert(entity, entity_index);
self.entities.push(entity);
2023-11-25 23:43:11 +00:00
bundle.take(|data, type_id, _size| {
let col = self.get_column_mut(type_id).unwrap();
unsafe { col.insert_entity(entity_index.0 as usize, data, *tick); }
2023-11-25 23:43:11 +00:00
});
entity_index
2023-05-25 04:11:16 +00:00
}
2024-03-03 02:20:19 +00:00
/// Removes an entity from the Archetype and frees its components. Returns the entity record
/// that took its place in the component column.
2023-12-26 19:12:53 +00:00
pub(crate) fn remove_entity(&mut self, entity: Entity, tick: &Tick) -> Option<(Entity, ArchetypeEntityId)> {
let entity_index = self.entity_ids.remove(&entity)
2023-11-27 02:05:35 +00:00
.expect("The entity is not in this Archetype!");
let mut removed_entity: Option<(Entity, ArchetypeEntityId)> = None;
for c in self.columns.iter_mut() {
2023-12-26 19:12:53 +00:00
let moved_entity = unsafe { c.remove_component(entity_index.0 as usize, tick) };
2023-11-27 02:05:35 +00:00
if let Some(moved_idx) = moved_entity {
2023-11-27 02:05:35 +00:00
if let Some((_, aid)) = removed_entity {
2024-03-03 02:20:19 +00:00
// Make sure that the moved entity is the same as what was moved in other columns.
assert!(moved_idx as u64 == aid.0);
2023-11-27 02:05:35 +00:00
} else {
// This is the first move, so find the Entity that was moved into this index.
let just_removed = self.entities[moved_idx];
removed_entity = Some((just_removed, ArchetypeEntityId(moved_idx as u64)));
2023-11-27 02:05:35 +00:00
}
} else {
2024-03-03 02:20:19 +00:00
// If there wasn't a moved entity, make sure no other columns moved something.
2023-11-27 02:05:35 +00:00
assert!(removed_entity.is_none());
}
}
// safe from the .expect at the start of this method.
//self.entity_ids.remove(&entity).unwrap();
// update the archetype index of the moved entity
if let Some((moved, _old_idx)) = removed_entity {
self.entity_ids.insert(moved, entity_index);
2024-03-03 02:20:19 +00:00
}
let removed = self.entities.swap_remove(entity_index.0 as _);
assert_eq!(removed, entity);
2023-11-27 02:05:35 +00:00
// now change the ArchetypeEntityId to be the index that the moved entity was moved into.
removed_entity.map(|(e, _a)| (e, entity_index))
2023-11-27 02:05:35 +00:00
}
/// Returns a boolean indicating whether this archetype can store the TypeIds given
2023-12-16 16:36:49 +00:00
pub fn is_archetype_for(&self, types: &Vec<DynTypeId>) -> bool {
if types.len() == self.columns.len() {
2024-03-03 02:34:20 +00:00
self.columns.iter().all(|c| types.contains(&c.info.type_id()))
} else { false }
2023-05-25 04:11:16 +00:00
}
2023-11-27 02:05:35 +00:00
2023-12-09 19:39:01 +00:00
/// Returns a boolean indicating whether this archetype has a column for `comp_type`
2024-03-03 02:34:20 +00:00
pub fn has_column<I: Into<DynTypeId>>(&self, comp_type: I) -> bool {
let comp_type = comp_type.into();
self.columns.iter().any(|c| comp_type == c.info.type_id())
2023-12-09 19:39:01 +00:00
}
2023-11-27 02:05:35 +00:00
/// Returns a boolean indicating whether this archetype is empty or not.
pub fn is_empty(&self) -> bool {
2024-03-03 02:20:19 +00:00
self.entity_ids.is_empty()
2023-11-27 02:05:35 +00:00
}
/// Returns the amount of entities that are stored in the archetype.
pub fn len(&self) -> usize {
2024-03-03 02:20:19 +00:00
self.entity_ids.len()
2023-11-27 02:05:35 +00:00
}
/// Grows columns in the archetype
///
/// Parameters:
/// * `new_capacity` - The new capacity of components that can fit in this column.
///
/// # Safety
///
/// Will panic if new_capacity is less than the current capacity
fn grow_columns(&mut self, new_capacity: usize) {
assert!(new_capacity > self.capacity);
for c in self.columns.iter_mut() {
unsafe { c.grow(new_capacity); }
}
self.capacity = new_capacity;
2023-11-27 02:05:35 +00:00
}
2024-03-03 02:20:19 +00:00
/// Attempts to find the column storing components of `type_id`
pub fn get_column<I: Into<DynTypeId>>(&self, type_id: I) -> Option<&ComponentColumn> {
let type_id = type_id.into();
2024-03-03 02:34:20 +00:00
self.columns.iter().find(|c| c.info.type_id() == type_id)
}
2023-12-26 19:12:53 +00:00
/// Returns a mutable borrow to a component column for `type_id`.
///
/// Note: This does not modify the tick for the column!
2024-03-03 02:20:19 +00:00
pub fn get_column_mut<I: Into<DynTypeId>>(&mut self, type_id: I) -> Option<&mut ComponentColumn> {
let type_id = type_id.into();
2024-03-03 02:34:20 +00:00
self.columns.iter_mut().find(|c| c.info.type_id() == type_id)
}
/// Reserves a slot in the columns for an entity and returns the index of that reserved spot
pub fn reserve_one(&mut self, entity: Entity) -> ArchetypeEntityId {
2024-03-03 02:20:19 +00:00
if self.capacity == self.entity_ids.len() {
let new_cap = self.capacity * 2;
self.grow_columns(new_cap);
self.capacity = new_cap;
}
debug_assert_eq!(self.entity_ids.len(), self.entities.len(),
"Somehow the Archetype's entity storage got unsynced");
2024-03-03 02:20:19 +00:00
let entity_index = ArchetypeEntityId(self.entity_ids.len() as u64);
self.entity_ids.insert(entity, entity_index);
self.entities.push(entity);
for col in self.columns.iter_mut() {
col.len += 1;
}
entity_index
}
/// Ensure that the internal entity lists are synced in length
pub(crate) fn ensure_synced(&self) {
debug_assert_eq!(self.entity_ids.len(), self.entities.len(),
"Somehow the Archetype's entity storage got unsynced");
}
/// Moves the entity from this archetype into another one.
///
/// # Safety
2024-03-03 02:20:19 +00:00
/// The entity IS NOT removed from the old archetype. You must manually call [`Archetype::remove_entity`](crate::Archetype).
/// It was done this way because I had some borrow check issues when writing [`World::insert`](crate::World)
/// related to borrowing mutably from self more than once.
2023-12-26 19:12:53 +00:00
/* pub fn move_into<B>(&self, into_arch: &mut Archetype, entity: Entity, new_components: B)
where
B: Bundle
{
let new_index = into_arch.reserve_one(entity);
//let infos: Vec<ComponentInfo> = into_arch.columns.iter().map(|c| c.info).collect();
// move the existing components into the new archetype
for col in self.columns.iter() {
let into_col = into_arch.get_column_mut(col.info.type_id).unwrap();
// copy from the old column into the new column, then remove it from the old one
unsafe {
let ptr = col.borrow_ptr();
let ptr = NonNull::new_unchecked(ptr.as_ptr()
.add(new_index.0 as usize * col.info.layout.size));
into_col.set_at(new_index.0 as _, ptr);
}
}
// now move the new components into the new archetype
new_components.take(|data, type_id, _size| {
let col = into_arch.get_column_mut(type_id).unwrap();
unsafe { col.set_at(new_index.0 as _, data); }
col.len += 1;
});
//self.remove_entity(entity);
2023-12-26 19:12:53 +00:00
} */
2023-12-16 16:36:49 +00:00
2024-03-03 02:20:19 +00:00
/// Returns a borrow to the map used to find the column indices of the entity.
pub fn entity_indexes(&self) -> &HashMap<Entity, ArchetypeEntityId> {
&self.entity_ids
2023-12-16 16:36:49 +00:00
}
2024-03-03 02:20:19 +00:00
/// Returns the Entity that is stored at the column matching `id`.
pub fn entity_at_index(&self, id: ArchetypeEntityId) -> Option<Entity> {
self.entities.get(id.0 as usize).cloned()
}
2024-03-03 01:20:38 +00:00
2024-03-03 02:20:19 +00:00
/// Returns a boolean indicating if the Archetype is storing the entity.
2024-03-03 01:20:38 +00:00
pub fn has_entity(&self, e: Entity) -> bool {
2024-03-03 02:20:19 +00:00
self.entity_ids.contains_key(&e)
2024-03-03 01:20:38 +00:00
}
/// Extend the Archetype by adding more columns.
///
/// In order to extend the Archetype, the archetype needs the components for the entities
/// it already has. These are provided through the `new_columns` parameter. **If the Vec
/// does not have the same amount of bundles in it as the amount of entities in the
/// Archetype, it will panic!**
pub fn extend<B: Bundle>(&mut self, tick: &Tick, new_columns: Vec<B>) {
debug_assert_eq!(new_columns.len(), self.len(), "The amount of provided column does not \
match the amount of entities");
let column_info = new_columns.iter()
.next()
.unwrap()
.info();
for coli in column_info.into_iter() {
let col = unsafe { ComponentColumn::new(coli, self.capacity) };
self.columns.push(col);
}
for (eid, bundle) in new_columns.into_iter().enumerate() {
bundle.take(|ptr, tyid, _size| {
unsafe {
let col = self.get_column_mut(tyid).unwrap();
col.insert_entity(eid, ptr, tick.clone());
}
});
}
}
2023-11-27 02:05:35 +00:00
}
#[cfg(test)]
mod tests {
2024-03-03 01:20:38 +00:00
use std::{alloc::Layout, cell::Ref, ptr::NonNull};
2023-11-27 02:05:35 +00:00
use rand::Rng;
2024-03-03 02:20:19 +00:00
use crate::{bundle::Bundle, tests::{Vec2, Vec3}, ComponentInfo, DynTypeId, DynamicBundle, Entity, EntityId, Tick};
2023-11-27 02:05:35 +00:00
use super::Archetype;
#[test]
fn one_entity_one_component() {
let bundle = (Vec2::new(10.0, 20.0),);
let entity = Entity {
id: EntityId(0),
generation: 0
};
let mut a = Archetype::from_bundle_info(super::ArchetypeId(0), bundle.info());
2023-12-26 19:12:53 +00:00
let entity_arch_id = a.add_entity(entity, bundle, &Tick::default());
2023-11-27 02:05:35 +00:00
let col = a.columns.get(0).unwrap();
2024-03-03 01:20:38 +00:00
let vec2: Ref<Vec2> = unsafe { col.get(entity_arch_id.0 as usize) };
2023-11-27 02:05:35 +00:00
assert_eq!(vec2.clone(), bundle.0);
}
#[test]
fn one_entity_two_component() {
let bundle = (Vec2::new(10.0, 20.0),Vec3::new(15.0, 54.0, 84.0));
let entity = Entity {
id: EntityId(0),
generation: 0
};
let mut a = Archetype::from_bundle_info(super::ArchetypeId(0), bundle.info());
2023-12-26 19:12:53 +00:00
let entity_arch_id = a.add_entity(entity, bundle, &Tick::default());
2023-11-27 02:05:35 +00:00
let col = a.columns.get(0).unwrap();
2024-03-03 01:20:38 +00:00
let vec2: Ref<Vec2> = unsafe { col.get(entity_arch_id.0 as usize) };
2023-11-27 02:05:35 +00:00
assert_eq!(vec2.clone(), bundle.0);
let col = a.columns.get(1).unwrap();
2024-03-03 01:20:38 +00:00
let vec3: Ref<Vec3> = unsafe { col.get(entity_arch_id.0 as usize) };
2023-11-27 02:05:35 +00:00
assert_eq!(vec3.clone(), bundle.1);
}
#[test]
fn two_entity_one_component() {
let b1 = (Vec2::new(10.0, 20.0),);
let e1 = Entity {
id: EntityId(0),
generation: 0
};
let b2 = (Vec2::new(19.0, 43.0),);
let e2 = Entity {
id: EntityId(1),
generation: 0
};
let mut a = Archetype::from_bundle_info(super::ArchetypeId(0), b1.info());
2023-12-26 19:12:53 +00:00
let earch1 = a.add_entity(e1, b1, &Tick::default());
let earch2 = a.add_entity(e2, b2, &Tick::default());
2023-11-27 02:05:35 +00:00
let col = a.columns.get(0).unwrap();
2024-03-03 01:20:38 +00:00
let vec2: Ref<Vec2> = unsafe { col.get(earch1.0 as usize) };
2023-11-27 02:05:35 +00:00
assert_eq!(vec2.clone(), b1.0);
2024-03-03 01:20:38 +00:00
let vec2: Ref<Vec2> = unsafe { col.get(earch2.0 as usize) };
2023-11-27 02:05:35 +00:00
assert_eq!(vec2.clone(), b2.0);
}
#[test]
fn two_entity_two_component() {
let b1 = (Vec2::new(10.0, 20.0), Vec3::new(84.0, 283.0, 28.0));
let e1 = Entity {
id: EntityId(0),
generation: 0
};
let b2 = (Vec2::new(19.0, 43.0), Vec3::new(74.0, 28.0, 93.0));
let e2 = Entity {
id: EntityId(1),
generation: 0
};
let mut a = Archetype::from_bundle_info(super::ArchetypeId(0), b1.info());
2023-12-26 19:12:53 +00:00
let earch1 = a.add_entity(e1, b1, &Tick::default());
let earch2 = a.add_entity(e2, b2, &Tick::default());
2023-11-27 02:05:35 +00:00
let col = a.columns.get(0).unwrap();
2024-03-03 01:20:38 +00:00
let vec2: Ref<Vec2> = unsafe { col.get(earch1.0 as usize) };
2023-11-27 02:05:35 +00:00
assert_eq!(vec2.clone(), b1.0);
2024-03-03 01:20:38 +00:00
let vec2: Ref<Vec2> = unsafe { col.get(earch2.0 as usize) };
2023-11-27 02:05:35 +00:00
assert_eq!(vec2.clone(), b2.0);
let col = a.columns.get(1).unwrap();
2024-03-03 01:20:38 +00:00
let vec3: Ref<Vec3> = unsafe { col.get(earch1.0 as usize) };
2023-11-27 02:05:35 +00:00
assert_eq!(vec3.clone(), b1.1);
2024-03-03 01:20:38 +00:00
let vec3: Ref<Vec3> = unsafe { col.get(earch2.0 as usize) };
2023-11-27 02:05:35 +00:00
assert_eq!(vec3.clone(), b2.1);
}
/// Tests manual growth of archetypes
2023-11-27 02:05:35 +00:00
#[test]
fn archetype_growth() {
let info = (Vec2::new(0.0, 0.0),).info();
let mut a = Archetype::from_bundle_info(super::ArchetypeId(0), info);
a.grow_columns(64);
a.grow_columns(128);
a.grow_columns(256);
}
/// Tests the auto growth of archetypes when adding entities
#[test]
fn auto_archetype_growth() {
2023-11-27 02:05:35 +00:00
let mut rng = rand::thread_rng();
let bundle_count = rng.gen_range(50..150);
let mut bundles: Vec<(Vec2,)> = vec![];
2023-11-27 02:05:35 +00:00
bundles.reserve(bundle_count);
let info = (Vec2::new(0.0, 0.0),).info();
let mut a = Archetype::from_bundle_info(super::ArchetypeId(0), info);
for i in 0..bundle_count {
let c = (Vec2::rand(),);
2023-11-27 02:05:35 +00:00
bundles.push(c);
2023-11-27 02:05:35 +00:00
a.add_entity(
Entity {
id: EntityId(i as u64),
generation: 0
},
2023-12-26 19:12:53 +00:00
c,
&Tick::default()
2023-11-27 02:05:35 +00:00
);
}
println!("Inserted {} entities", bundle_count);
2023-11-27 02:05:35 +00:00
let col = a.columns.get(0).unwrap();
for i in 0..bundle_count {
2024-03-03 01:20:38 +00:00
let vec2: Ref<Vec2> = unsafe { col.get(i) };
2023-11-27 02:05:35 +00:00
assert_eq!(vec2.clone(), bundles[i].0);
}
}
#[test]
fn remove_entity() {
let bundles = vec![Vec2::rand(), Vec2::rand(), Vec2::rand()];
println!("Bundles: {:?}", bundles);
2023-11-27 02:05:35 +00:00
let info = (Vec2::new(0.0, 0.0),).info();
let mut a = Archetype::from_bundle_info(super::ArchetypeId(0), info);
// add the entities to the archetype
for i in 0..bundles.len() {
a.add_entity(
Entity {
id: EntityId(i as u64),
generation: 0
},
(bundles[i],),
2023-12-26 19:12:53 +00:00
&Tick::default()
2023-11-27 02:05:35 +00:00
);
}
// Remove the 'middle' entity in the column
let moved_entity = a.remove_entity(
Entity {
id: EntityId(1u64),
2023-12-26 19:12:53 +00:00
generation: 0,
},
&Tick::default()
2023-11-27 02:05:35 +00:00
).expect("No entity was moved");
// The last entity in the column should have been moved
assert!(moved_entity.0.id.0 == 2);
assert!(moved_entity.1.0 == 1);
// make sure that the entities' component was actually moved in the column
let col = &a.columns[0];
let comp = unsafe { col.get::<Vec2>(1) };
assert_eq!(comp.clone(), bundles[2]);
2023-11-27 02:05:35 +00:00
}
/// This test simulates an archetype that stores types that rust does not know about.
#[test]
fn dynamic_archetype() {
2024-03-03 02:20:19 +00:00
let layout = Layout::new::<u32>();
let info = ComponentInfo::new_unknown(DynTypeId::Unknown(100), "u32", layout);
let infos = vec![info.clone()];
let mut a = Archetype::from_bundle_info(super::ArchetypeId(0), infos);
let mut dynamic_bundle = DynamicBundle::default();
let comp = 50u32;
let ptr = NonNull::from(&comp).cast::<u8>();
dynamic_bundle.push_unknown(ptr, info.clone());
a.add_entity(
Entity {
id: EntityId(0),
generation: 0
2023-12-26 19:12:53 +00:00
},
dynamic_bundle,
&Tick::default()
);
let col = a.columns.iter().next().unwrap();
let ptr = col.borrow_ptr();
assert_eq!(unsafe { *ptr.cast::<u32>().as_ref() }, comp);
assert_eq!(col.info, info);
}
/// Tests removing an entity from the Archetype when it is the only entity in it.
#[test]
fn remove_single_entity() {
let info = (Vec2::new(0.0, 0.0),).info();
let mut a = Archetype::from_bundle_info(super::ArchetypeId(0), info);
let ae = Entity {
id: EntityId(0),
generation: 0
};
a.add_entity(
ae,
Vec2::new(10.0, 50.0),
&Tick::default()
);
a.remove_entity(ae, &Tick::default());
}
2023-05-25 04:11:16 +00:00
}