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

79 lines
3.4 KiB
Rust
Raw Normal View History

2023-11-25 23:43:11 +00:00
use std::{any::{TypeId, Any}, cell::Ref, ptr::NonNull, mem::size_of};
2023-05-25 04:11:16 +00:00
2023-11-25 23:43:11 +00:00
use crate::{archetype::{ComponentColumn, Archetype}, component::Component, component_info::ComponentInfo};
2023-05-25 04:11:16 +00:00
pub trait Bundle {
// Get a list of type ids that this bundle is storing
fn types(&self) -> Vec<TypeId>;
/// Take components into a list.
/// The return value could be seen as a list of a list of components, but that inner list
/// only contains a single value to make it easier to add it to an archetype.
2023-11-25 23:43:11 +00:00
//fn take_components(self) -> Vec<Box<dyn ComponentColumn>>;
//fn from_component_columns(columns: Vec<&Box<dyn ComponentColumn>>) -> Self;
//fn from_archetype(archetype: &Archetype) -> Self;
fn info(&self) -> Vec<ComponentInfo>;
/// Take the bundle by calling the closure with pointers to each component, its type and size.
/// The closure is expected to take ownership of the pointer.
fn takefn(self, cb: impl FnMut(NonNull<u8>, TypeId, usize));
2023-05-25 04:11:16 +00:00
}
2023-11-25 23:43:11 +00:00
impl<C1: Component> Bundle for (C1,) {
fn types(&self) -> Vec<TypeId> {
vec![self.0.type_id()]
}
fn info(&self) -> Vec<ComponentInfo> {
vec![ComponentInfo::new::<C1>()]
}
fn takefn(self, mut cb: impl FnMut(NonNull<u8>, TypeId, usize)) {
let (c1, ) = self;
cb(NonNull::from(&c1).cast(), TypeId::of::<C1>(), size_of::<C1>());
}
}
/* macro_rules! impl_bundle_tuple {
2023-05-25 04:11:16 +00:00
( $(($name: ident, $index: tt))+ ) => (
impl<$($name: Component),+> Bundle for ($($name,)+) {
2023-05-25 04:11:16 +00:00
fn types(&self) -> Vec<TypeId> {
vec![$(self.$index.type_id()),+]
}
fn take_components(self) -> Vec<Box<dyn ComponentColumn>> {
vec![$(Box::new(vec![self.$index])),+]
}
2023-11-25 23:43:11 +00:00
fn from_component_columns(columns: Vec<&Box<dyn ComponentColumn>>) -> Self {
let mut chains = Vec::new();
for col in columns.iter() {
col.
}
todo!()
}
2023-05-25 04:11:16 +00:00
}
);
2023-11-25 23:43:11 +00:00
} */
2023-05-25 04:11:16 +00:00
// hopefully 16 components in a bundle is enough
2023-11-25 23:43:11 +00:00
//impl_bundle_tuple! { (C1, 0) }
/* impl_bundle_tuple! { (C1, 0) (C2, 1) }
2023-05-25 04:11:16 +00:00
impl_bundle_tuple! { (C1, 0) (C2, 1) (C3, 2) }
impl_bundle_tuple! { (C1, 0) (C2, 1) (C3, 2) (C4, 3) }
impl_bundle_tuple! { (C1, 0) (C2, 1) (C3, 2) (C4, 3) (C5, 4) }
impl_bundle_tuple! { (C1, 0) (C2, 1) (C3, 2) (C4, 3) (C5, 4) (C6, 5) }
impl_bundle_tuple! { (C1, 0) (C2, 1) (C3, 2) (C4, 3) (C5, 4) (C6, 5) (C7, 6) }
impl_bundle_tuple! { (C1, 0) (C2, 1) (C3, 2) (C4, 3) (C5, 4) (C6, 5) (C7, 6) (C8, 7) }
impl_bundle_tuple! { (C1, 0) (C2, 1) (C3, 2) (C4, 3) (C5, 4) (C6, 5) (C7, 6) (C8, 7) (C9, 8) }
impl_bundle_tuple! { (C1, 0) (C2, 1) (C3, 2) (C4, 3) (C5, 4) (C6, 5) (C7, 6) (C8, 7) (C9, 8) (C10, 9) }
impl_bundle_tuple! { (C1, 0) (C2, 1) (C3, 2) (C4, 3) (C5, 4) (C6, 5) (C7, 6) (C8, 7) (C9, 8) (C10, 9) (C11, 10) }
impl_bundle_tuple! { (C1, 0) (C2, 1) (C3, 2) (C4, 3) (C5, 4) (C6, 5) (C7, 6) (C8, 7) (C9, 8) (C10, 9) (C11, 10) (C12, 11) }
impl_bundle_tuple! { (C1, 0) (C2, 1) (C3, 2) (C4, 3) (C5, 4) (C6, 5) (C7, 6) (C8, 7) (C9, 8) (C10, 9) (C11, 10) (C12, 11) (C13, 12) }
impl_bundle_tuple! { (C1, 0) (C2, 1) (C3, 2) (C4, 3) (C5, 4) (C6, 5) (C7, 6) (C8, 7) (C9, 8) (C10, 9) (C11, 10) (C12, 11) (C13, 12) (C14, 13) }
impl_bundle_tuple! { (C1, 0) (C2, 1) (C3, 2) (C4, 3) (C5, 4) (C6, 5) (C7, 6) (C8, 7) (C9, 8) (C10, 9) (C11, 10) (C12, 11) (C13, 12) (C14, 13) (C15, 14) }
2023-11-25 23:43:11 +00:00
*/