diff options
Diffstat (limited to 'engine-ecs/src/lib.rs')
| -rw-r--r-- | engine-ecs/src/lib.rs | 99 |
1 files changed, 70 insertions, 29 deletions
diff --git a/engine-ecs/src/lib.rs b/engine-ecs/src/lib.rs index b987f65..450940a 100644 --- a/engine-ecs/src/lib.rs +++ b/engine-ecs/src/lib.rs @@ -1,6 +1,6 @@ #![deny(clippy::all, clippy::pedantic)] -use std::any::{Any, TypeId}; +use std::any::Any; use std::borrow::Cow; use std::fmt::Debug; use std::hint::cold_path; @@ -54,7 +54,6 @@ use crate::query::{ TermsBuilderInterface, MAX_TERM_CNT as QUERY_MAX_TERM_CNT, }; -use crate::reflection::Type as TypeReflection; use crate::sole::{Single, Sole}; use crate::system::observer::Observer; use crate::system::{ @@ -176,7 +175,20 @@ impl World where Comps: ComponentSequence, { - self.create_ent(entity_uid, components.into_parts_array()); + let components_parts = components.into_parts_array(); + + if let Some(comp_parts) = components_parts + .as_ref() + .iter() + .find(|comp_parts| comp_parts.is_sole) + { + panic!( + "Cannot spawn entity with sole component {}", + comp_parts.name + ); + } + + self.create_ent(entity_uid, components_parts); } /// Creates a entity with the given components. The entity will have the specified @@ -193,10 +205,22 @@ impl World ) where Comps: ComponentSequence, { + let components_parts = components.into_parts_array(); + + if let Some(comp_parts) = components_parts + .as_ref() + .iter() + .find(|comp_parts| comp_parts.is_sole) + { + panic!( + "Cannot spawn entity with sole component {}", + comp_parts.name + ); + } + self.create_ent( entity_uid, - components - .into_parts_array() + components_parts .into_iter() .chain([EntityName { name: name.into() }.into_parts()]), ); @@ -204,6 +228,12 @@ impl World pub fn add_component(&mut self, entity_id: Uid, component_parts: ComponentParts) { + assert!( + !component_parts.is_sole, + "Cannot add sole component {} to an entity", + component_parts.name + ); + Self::add_entity_components( entity_id, [component_parts], @@ -225,6 +255,15 @@ impl World where SoleT: Sole, { + if self + .data + .component_storage + .get_entity_archetype(SoleT::id()) + .is_some() + { + return Err(SoleAlreadyExistsError); + } + let name = sole.name(); self.create_ent( @@ -685,22 +724,28 @@ impl World &component_parts.pair_relation_metadata { Self::create_component_info_entity_if_missing( - comp_id.relation(), - pair_relation_metadata.name, - pair_relation_metadata.ty, - pair_relation_metadata.ty_id, component_storage, + comp_id.relation(), + ComponentInfo { + type_reflection: pair_relation_metadata.ty, + ty_id: pair_relation_metadata.ty_id, + name: pair_relation_metadata.name, + is_sole: pair_relation_metadata.is_sole, + }, ); } if let Some(pair_target_metadata) = &component_parts.pair_target_metadata { Self::create_component_info_entity_if_missing( - comp_id.target(), - pair_target_metadata.name, - pair_target_metadata.ty, - pair_target_metadata.ty_id, component_storage, + comp_id.target(), + ComponentInfo { + type_reflection: pair_target_metadata.ty, + ty_id: pair_target_metadata.ty_id, + name: pair_target_metadata.name, + is_sole: pair_target_metadata.is_sole, + }, ); } @@ -708,11 +753,14 @@ impl World } Self::create_component_info_entity_if_missing( - comp_id, - comp_name, - component_parts.type_reflection, - comp_type_id, component_storage, + comp_id, + ComponentInfo { + type_reflection: component_parts.type_reflection, + ty_id: comp_type_id, + name: comp_name, + is_sole: component_parts.is_sole, + }, ); event_submitter.submit_event( @@ -726,11 +774,9 @@ impl World } fn create_component_info_entity_if_missing( - comp_id: Uid, - comp_name: &'static str, - type_reflection: Option<&'static TypeReflection>, - type_id: TypeId, component_storage: &mut ComponentStorage, + comp_id: Uid, + comp_info: ComponentInfo, ) { if let Some(comp_ent_archetype) = component_storage.get_entity_archetype(comp_id) @@ -746,12 +792,7 @@ impl World } } - let comp_info_parts = ComponentInfo { - type_reflection, - name: comp_name, - ty_id: type_id, - } - .into_parts(); + let comp_info_parts = comp_info.into_parts(); match component_storage.add_entity_component( comp_id, @@ -962,5 +1003,5 @@ impl ActionQueue } #[derive(Debug, thiserror::Error)] -#[error("Sole {0} already exists")] -pub struct SoleAlreadyExistsError(pub &'static str); +#[error("Sole already exists")] +pub struct SoleAlreadyExistsError; |
