diff options
| author | HampusM <hampus@hampusmat.com> | 2026-09-20 19:05:03 +0200 |
|---|---|---|
| committer | HampusM <hampus@hampusmat.com> | 2026-09-20 19:05:03 +0200 |
| commit | fd28c16e6455aeff206543b1d1b6655729aaed07 (patch) | |
| tree | b606fe2ce433abd20116fa553ceedb027ea18cd1 | |
| parent | bc1aab63bae307fab30d5c04d947ff95ead9eb0f (diff) | |
refactor(engine-ecs): move & rename component::Sequence to bundle::Bundle
| -rw-r--r-- | engine-ecs/src/actions.rs | 26 | ||||
| -rw-r--r-- | engine-ecs/src/bundle.rs | 40 | ||||
| -rw-r--r-- | engine-ecs/src/component.rs | 62 | ||||
| -rw-r--r-- | engine-ecs/src/extension.rs | 17 | ||||
| -rw-r--r-- | engine-ecs/src/lib.rs | 32 |
5 files changed, 78 insertions, 99 deletions
diff --git a/engine-ecs/src/actions.rs b/engine-ecs/src/actions.rs index 9ca9198..c367f6e 100644 --- a/engine-ecs/src/actions.rs +++ b/engine-ecs/src/actions.rs @@ -1,11 +1,7 @@ use std::borrow::Cow; -use crate::component::{ - Component, - IntoParts, - Parts as ComponentParts, - Sequence as ComponentSequence, -}; +use crate::bundle::Bundle; +use crate::component::{Component, IntoParts, Parts as ComponentParts}; use crate::entity::Name as EntityName; use crate::event::component::Removed; use crate::pair::Pair; @@ -25,11 +21,11 @@ impl Actions<'_> { /// Queues up a entity to spawn at the end of the current tick, returning the [`Uid`] /// that the entity will have. - pub fn spawn<Comps: ComponentSequence>(&mut self, components: Comps) -> Uid + pub fn spawn<BundleT: Bundle>(&mut self, bundle: BundleT) -> Uid { let new_entity_uid = Uid::new_unique(); - let components_parts = components.into_parts_array(); + let components_parts = bundle.into_parts_array(); if let Some(comp_parts) = components_parts .as_ref() @@ -53,15 +49,15 @@ impl Actions<'_> /// /// In addition to the given components, the entity will have a [`EntityName`] /// component containing `name`. - pub fn spawn_named<Comps: ComponentSequence>( + pub fn spawn_named<BundleT: Bundle>( &mut self, name: impl Into<Cow<'static, str>>, - components: Comps, + bundle: BundleT, ) -> Uid { let new_entity_uid = Uid::new_unique(); - let components_parts = components.into_parts_array(); + let components_parts = bundle.into_parts_array(); if let Some(comp_parts) = components_parts .as_ref() @@ -119,17 +115,15 @@ impl Actions<'_> } /// Queues up adding component(s) to a entity at the end of the current tick. - pub fn add_components<Comps>(&mut self, entity_uid: Uid, components: Comps) - where - Comps: ComponentSequence, + pub fn add_components<BundleT: Bundle>(&mut self, entity_uid: Uid, bundle: BundleT) { debug_assert!(!entity_uid.is_pair()); - if components.cnt() == 0 { + if bundle.cnt() == 0 { return; } - let components_parts = components.into_parts_array(); + let components_parts = bundle.into_parts_array(); if let Some(comp_parts) = components_parts .as_ref() diff --git a/engine-ecs/src/bundle.rs b/engine-ecs/src/bundle.rs new file mode 100644 index 0000000..89e0cc4 --- /dev/null +++ b/engine-ecs/src/bundle.rs @@ -0,0 +1,40 @@ +use seq_macro::seq; + +use crate::component::{IntoParts, Parts}; +use crate::util::Array; + +pub trait Bundle +{ + type PartsArray: Array<Parts>; + + fn cnt(&self) -> usize; + + fn into_parts_array(self) -> Self::PartsArray; +} + +macro_rules! gen_tuple_impls { + ($c: tt) => { + seq!(I in 0..$c { + impl<#(IntoCompParts~I: IntoParts,)*> Bundle for (#(IntoCompParts~I,)*) + { + type PartsArray = [Parts; $c]; + + fn cnt(&self) -> usize + { + $c + } + + fn into_parts_array(self) -> Self::PartsArray + { + [#({ + self.I.into_parts() + },)*] + } + } + }); + }; +} + +seq!(C in 0..17 { + gen_tuple_impls!(C); +}); diff --git a/engine-ecs/src/component.rs b/engine-ecs/src/component.rs index 847ecbd..c76c48e 100644 --- a/engine-ecs/src/component.rs +++ b/engine-ecs/src/component.rs @@ -3,7 +3,6 @@ use std::fmt::Debug; use std::ops::{Deref, DerefMut}; use engine_ecs_macros::Component; -use seq_macro::seq; use crate::event::component::Changed; use crate::event::Submitter as EventSubmitter; @@ -18,7 +17,6 @@ use crate::pair::{MemberMetadata as PairMemberMetadata, Pair}; use crate::reflection::Type as TypeReflection; use crate::system::Input as SystemInput; use crate::uid::Uid; -use crate::util::Array; use crate::{EntityComponentRef, World}; pub mod local; @@ -66,16 +64,6 @@ impl Debug for dyn Component } } -/// A sequence of components. -pub trait Sequence -{ - type PartsArray: Array<Parts>; - - fn cnt(&self) -> usize; - - fn into_parts_array(self) -> Self::PartsArray; -} - #[derive(Debug)] pub struct Handle<'a, DataT: ?Sized + 'static> { @@ -240,48 +228,6 @@ pub enum HandleError #[error("Failed to acquire component lock")] pub struct AcquireLockError(#[source] LockError); -macro_rules! inner { - ($c: tt) => { - seq!(I in 0..$c { - impl<#(IntoCompParts~I: IntoParts,)*> Sequence for (#(IntoCompParts~I,)*) - { - type PartsArray = [Parts; $c]; - - fn cnt(&self) -> usize - { - $c - } - - fn into_parts_array(self) -> Self::PartsArray - { - [#({ - self.I.into_parts() - },)*] - } - } - }); - }; -} - -seq!(C in 0..17 { - inner!(C); -}); - -impl<const LEN: usize> Sequence for [Parts; LEN] -{ - type PartsArray = Self; - - fn cnt(&self) -> usize - { - self.len() - } - - fn into_parts_array(self) -> Self::PartsArray - { - self - } -} - pub trait IntoParts { fn into_parts(self) -> Parts; @@ -322,6 +268,14 @@ impl Parts } } +impl IntoParts for Parts +{ + fn into_parts(self) -> Parts + { + self + } +} + #[derive(Debug)] pub struct PartsBuilder { diff --git a/engine-ecs/src/extension.rs b/engine-ecs/src/extension.rs index ac989c9..2efaac0 100644 --- a/engine-ecs/src/extension.rs +++ b/engine-ecs/src/extension.rs @@ -1,6 +1,6 @@ use std::borrow::Cow; -use crate::component::Sequence as ComponentSequence; +use crate::bundle::Bundle; use crate::entity::Declaration as EntityDeclaration; use crate::sole::Sole; use crate::system::observer::Observer; @@ -48,22 +48,19 @@ impl<'world> Collector<'world> } /// Adds a entity to the [`World`]. - pub fn spawn<Comps>(&mut self, components: Comps) - where - Comps: ComponentSequence, + pub fn spawn<BundleT: Bundle>(&mut self, bundle: BundleT) { - self.world.spawn(components); + self.world.spawn(bundle); } /// Adds a entity to the [`World`]. - pub fn spawn_named<Comps>( + pub fn spawn_named<BundleT: Bundle>( &mut self, name: impl Into<Cow<'static, str>>, - components: Comps, - ) where - Comps: ComponentSequence, + bundle: BundleT, + ) { - self.world.spawn_named(name, components); + self.world.spawn_named(name, bundle); } /// Adds a declared entity to the [`World`]. diff --git a/engine-ecs/src/lib.rs b/engine-ecs/src/lib.rs index 450940a..53e49df 100644 --- a/engine-ecs/src/lib.rs +++ b/engine-ecs/src/lib.rs @@ -9,6 +9,7 @@ use std::sync::atomic::{AtomicBool, Ordering}; use std::time::Duration; use crate::actions::Action; +use crate::bundle::Bundle; use crate::component::storage::archetype::EntityComponent as ArchetypeEntityComponent; use crate::component::storage::{ EntityAlreadyExistsError, @@ -20,7 +21,6 @@ use crate::component::{ Info as ComponentInfo, IntoParts as IntoComponentParts, Parts as ComponentParts, - Sequence as ComponentSequence, }; use crate::entity::{ Declaration as EntityDeclaration, @@ -66,6 +66,7 @@ use crate::system::{ use crate::uid::Uid; pub mod actions; +pub mod bundle; pub mod component; pub mod entity; pub mod error; @@ -137,13 +138,11 @@ impl World /// Creates a entity with the given components. A new unique [`Uid`] will be generated /// for this entity. - pub fn spawn<Comps>(&mut self, components: Comps) -> Uid - where - Comps: ComponentSequence, + pub fn spawn<BundleT: Bundle>(&mut self, bundle: BundleT) -> Uid { let entity_uid = Uid::new_unique(); - self.spawn_with_uid(entity_uid, components); + self.spawn_with_uid(entity_uid, bundle); entity_uid } @@ -153,17 +152,15 @@ impl World /// /// In addition to the given components, the entity will have a [`EntityName`] /// component containing `name`. - pub fn spawn_named<Comps>( + pub fn spawn_named<BundleT: Bundle>( &mut self, name: impl Into<Cow<'static, str>>, - components: Comps, + bundle: BundleT, ) -> Uid - where - Comps: ComponentSequence, { let entity_uid = Uid::new_unique(); - self.spawn_named_with_uid(entity_uid, name, components); + self.spawn_named_with_uid(entity_uid, name, bundle); entity_uid } @@ -171,11 +168,9 @@ impl World /// Creates a entity with the given components. The entity will have the specified /// [`Uid`]. #[tracing::instrument(skip_all)] - pub fn spawn_with_uid<Comps>(&mut self, entity_uid: Uid, components: Comps) - where - Comps: ComponentSequence, + pub fn spawn_with_uid<BundleT: Bundle>(&mut self, entity_uid: Uid, bundle: BundleT) { - let components_parts = components.into_parts_array(); + let components_parts = bundle.into_parts_array(); if let Some(comp_parts) = components_parts .as_ref() @@ -197,15 +192,14 @@ impl World /// In addition to the given components, the entity will have a [`EntityName`] /// component containing `name`. #[tracing::instrument(skip_all)] - pub fn spawn_named_with_uid<Comps>( + pub fn spawn_named_with_uid<BundleT: Bundle>( &mut self, entity_uid: Uid, name: impl Into<Cow<'static, str>>, - components: Comps, - ) where - Comps: ComponentSequence, + bundle: BundleT, + ) { - let components_parts = components.into_parts_array(); + let components_parts = bundle.into_parts_array(); if let Some(comp_parts) = components_parts .as_ref() |
