diff options
Diffstat (limited to 'engine-ecs')
| -rw-r--r-- | engine-ecs/src/actions.rs | 12 | ||||
| -rw-r--r-- | engine-ecs/src/component.rs | 96 | ||||
| -rw-r--r-- | engine-ecs/src/lib.rs | 19 | ||||
| -rw-r--r-- | engine-ecs/src/pair.rs | 7 |
4 files changed, 71 insertions, 63 deletions
diff --git a/engine-ecs/src/actions.rs b/engine-ecs/src/actions.rs index bbc19c6..f4967df 100644 --- a/engine-ecs/src/actions.rs +++ b/engine-ecs/src/actions.rs @@ -36,11 +36,11 @@ impl Actions<'_> let components_parts = bundle.iter_component_parts().collect::<Vec<_>>(); - if let Some(ComponentParts { name: sole_name, .. }) = components_parts + if let Some(ComponentParts { id: sole_id, .. }) = components_parts .iter() .find(|comp_parts| comp_parts.is_sole) { - panic!("Cannot spawn entity with sole component '{sole_name}'"); + panic!("Cannot spawn entity with sole component {sole_id}"); } self.action_queue @@ -70,11 +70,11 @@ impl Actions<'_> .chain([EntityName { name: name.into() }.into_parts()]) .collect::<Vec<_>>(); - if let Some(ComponentParts { name: sole_name, .. }) = components_parts + if let Some(ComponentParts { id: sole_id, .. }) = components_parts .iter() .find(|comp_parts| comp_parts.is_sole) { - panic!("Cannot spawn entity with sole component '{sole_name}'"); + panic!("Cannot spawn entity with sole component {sole_id}"); } self.action_queue @@ -131,11 +131,11 @@ impl Actions<'_> return; } - if let Some(ComponentParts { name: sole_name, .. }) = components_parts + if let Some(ComponentParts { id: sole_id, .. }) = components_parts .iter() .find(|comp_parts| comp_parts.is_sole) { - panic!("Cannot spawn entity with sole component '{sole_name}'"); + panic!("Cannot add sole component {sole_id} to a entity"); } self.action_queue diff --git a/engine-ecs/src/component.rs b/engine-ecs/src/component.rs index a548518..3320102 100644 --- a/engine-ecs/src/component.rs +++ b/engine-ecs/src/component.rs @@ -263,11 +263,9 @@ pub trait IntoParts pub struct Parts { pub id: Uid, - pub name: Cow<'static, str>, - pub type_name: &'static str, pub is_sole: bool, - pub type_reflection: Option<&'static TypeReflection>, pub data: Box<dyn Any>, + pub component_info: Info, pub pair_relation_metadata: Option<PairMemberMetadata>, pub pair_target_metadata: Option<PairMemberMetadata>, } @@ -276,18 +274,12 @@ impl Parts { /// Creates components parts from the component information stored in /// `component_info`. - /// - /// # Panics - /// This function will panic if the type ID of the value in `data` does not equal - /// to the type ID stored in `component_info`. #[must_use] pub fn from_component_info(component_info: &Info, id: Uid, data: Box<dyn Any>) -> Self { Self::builder() - .type_reflection(component_info.type_reflection) - .name(component_info.name.clone()) - .type_name(component_info.type_name) + .component_info(component_info.clone()) .is_sole(component_info.is_sole) .build_with_any_data(id, data) } @@ -310,10 +302,8 @@ impl IntoParts for Parts #[derive(Debug)] pub struct PartsBuilder { - name: Cow<'static, str>, - type_name: &'static str, is_sole: bool, - type_reflection: Option<&'static TypeReflection>, + component_info: Info, pair_relation_metadata: Option<PairMemberMetadata>, pair_target_metadata: Option<PairMemberMetadata>, } @@ -321,16 +311,9 @@ pub struct PartsBuilder impl PartsBuilder { #[must_use] - pub fn name(mut self, name: impl Into<Cow<'static, str>>) -> Self - { - self.name = name.into(); - self - } - - #[must_use] - pub fn type_name(mut self, type_name: &'static str) -> Self + pub fn component_info(mut self, component_info: Info) -> Self { - self.type_name = type_name.into(); + self.component_info = component_info; self } @@ -342,16 +325,6 @@ impl PartsBuilder } #[must_use] - pub fn type_reflection( - mut self, - type_reflection: Option<&'static TypeReflection>, - ) -> Self - { - self.type_reflection = type_reflection; - self - } - - #[must_use] pub fn pair_relation_metadata(mut self, metadata: Option<PairMemberMetadata>) -> Self { @@ -371,11 +344,9 @@ impl PartsBuilder { Parts { id, - name: self.name, - type_name: self.type_name, is_sole: self.is_sole, - type_reflection: self.type_reflection, data: Box::new(data), + component_info: self.component_info, pair_relation_metadata: self.pair_relation_metadata, pair_target_metadata: self.pair_target_metadata, } @@ -386,11 +357,9 @@ impl PartsBuilder { Parts { id, - name: self.name, - type_name: self.type_name, is_sole: self.is_sole, - type_reflection: self.type_reflection, data, + component_info: self.component_info, pair_relation_metadata: self.pair_relation_metadata, pair_target_metadata: self.pair_target_metadata, } @@ -402,10 +371,8 @@ impl Default for PartsBuilder fn default() -> Self { Self { - name: "<unspecified>".into(), - type_name: "<unspecified>", is_sole: false, - type_reflection: None, + component_info: Info::default(), pair_relation_metadata: None, pair_target_metadata: None, } @@ -422,6 +389,53 @@ pub struct Info pub is_sole: bool, } +impl Default for Info +{ + fn default() -> Self + { + Self { + type_reflection: None, + name: "<unspecified>".into(), + type_name: "<unspecified>", + is_sole: false, + } + } +} + +impl Info +{ + #[must_use] + pub fn with_name(mut self, name: impl Into<Cow<'static, str>>) -> Self + { + self.name = name.into(); + self + } + + #[must_use] + pub fn with_type_name(mut self, type_name: &'static str) -> Self + { + self.type_name = type_name.into(); + self + } + + #[must_use] + pub fn with_is_sole(mut self, is_sole: bool) -> Self + { + self.is_sole = is_sole; + self + } + + #[must_use] + pub fn with_type_reflection( + mut self, + type_reflection: Option<&'static TypeReflection>, + ) -> Self + { + self.type_reflection = type_reflection; + self + } +} + mod sealed { pub trait Sealed {} diff --git a/engine-ecs/src/lib.rs b/engine-ecs/src/lib.rs index 711b3c8..ee2dca6 100644 --- a/engine-ecs/src/lib.rs +++ b/engine-ecs/src/lib.rs @@ -212,7 +212,7 @@ impl World assert!( !component_parts.is_sole, "Cannot add sole component {} to an entity", - component_parts.name + component_parts.id ); Self::add_entity_components( @@ -624,13 +624,13 @@ impl World self.data.component_storage.create_entity(sole_parts.id) { tracing::error!( - name = %sole_parts.name, + id = %sole_parts.id, "Can not create sole that already exists" ); continue; } - let name = sole_parts.name.clone(); + let name = sole_parts.component_info.name.clone(); Self::add_entity_components( sole_parts.id, @@ -664,12 +664,11 @@ impl World for component_parts in component_iter { assert!( flags.allow_soles || !component_parts.is_sole, - "Cannot add sole component '{}' to an entity", - component_parts.name + "Cannot add sole component {} to an entity", + component_parts.id ); let comp_id = component_parts.id; - let comp_name = component_parts.name; if let Err(err) = component_storage .add_entity_component(entity_uid, (comp_id, component_parts.data)) @@ -683,7 +682,6 @@ impl World entity = %entity_uid, entity_name = ent_name.as_ref().map(|ent_name| ent_name.name.as_ref()), component = %comp_id, - component_name = %comp_name, "Failed to add component to entity: {err}" ); @@ -725,12 +723,7 @@ impl World Self::create_component_info_entity_if_missing( component_storage, comp_id, - ComponentInfo { - type_reflection: component_parts.type_reflection, - name: comp_name, - type_name: component_parts.type_name, - is_sole: component_parts.is_sole, - }, + component_parts.component_info, ); event_submitter.submit_event( diff --git a/engine-ecs/src/pair.rs b/engine-ecs/src/pair.rs index a6066e1..d0b9179 100644 --- a/engine-ecs/src/pair.rs +++ b/engine-ecs/src/pair.rs @@ -7,6 +7,7 @@ use crate::component::{ Handle as ComponentHandle, HandleError as ComponentHandleError, HandleMut as ComponentHandleMut, + Info as ComponentInfo, IntoParts as IntoComponentParts, Parts as ComponentParts, Removable as RemovableComponent, @@ -215,7 +216,7 @@ impl IntoComponentParts for Pair<Uid, Uid> let id = self.id(); ComponentParts::builder() - .name("Pair") + .component_info(ComponentInfo::default().with_name("Pair")) .pair_relation_metadata(self.relation_metadata) .pair_target_metadata(self.target_metadata) .build(id, ()) @@ -234,7 +235,7 @@ where }); ComponentParts::builder() - .name("Pair") + .component_info(ComponentInfo::default().with_name("Pair")) .pair_relation_metadata(self.relation_metadata) .pair_target_metadata(self.target_metadata) .build(id, self.target) @@ -253,7 +254,7 @@ where }); ComponentParts::builder() - .name("Pair") + .component_info(ComponentInfo::default().with_name("Pair")) .pair_relation_metadata(self.relation_metadata) .pair_target_metadata(self.target_metadata) .build(id, self.relation) |
