From cb1ff29c8c7fa77bbb4e532eaa7e45df717a58ef Mon Sep 17 00:00:00 2001 From: HampusM Date: Thu, 6 Jun 2024 12:50:05 +0200 Subject: refactor(ecs): move ComponentStorage to it's own module --- ecs/src/lib.rs | 117 ++++++++++----------------------------------------------- 1 file changed, 19 insertions(+), 98 deletions(-) (limited to 'ecs/src/lib.rs') diff --git a/ecs/src/lib.rs b/ecs/src/lib.rs index f76d1ae..1983f66 100644 --- a/ecs/src/lib.rs +++ b/ecs/src/lib.rs @@ -1,7 +1,7 @@ #![deny(clippy::all, clippy::pedantic)] use std::any::{type_name, TypeId}; -use std::collections::{HashMap, HashSet}; +use std::collections::HashMap; use std::fmt::Debug; use std::mem::ManuallyDrop; use std::ops::RangeBounds; @@ -10,11 +10,8 @@ use std::sync::Arc; use std::vec::Drain; use crate::actions::Action; -use crate::component::{ - Component, - IsOptional as ComponentIsOptional, - Sequence as ComponentSequence, -}; +use crate::component::storage::ComponentStorage; +use crate::component::{Component, Sequence as ComponentSequence}; use crate::event::{Event, Id as EventId, Ids, Sequence as EventSequence}; use crate::extension::{Collector as ExtensionCollector, Extension}; use crate::lock::Lock; @@ -37,21 +34,6 @@ pub use ecs_macros::{Component, Sole}; pub use crate::query::Query; -#[derive(Debug, Default)] -struct Entity -{ - components: Vec, -} - -#[derive(Debug)] -#[non_exhaustive] -pub struct EntityComponent -{ - pub id: TypeId, - pub name: &'static str, - pub component: Lock>, -} - #[derive(Debug, Default)] pub struct World { @@ -80,18 +62,7 @@ impl World .component_storage .write_nonblock() .expect("Failed to acquire read-write component storage lock") - .entities - .push(Entity { - components: components - .into_vec() - .into_iter() - .map(|component| EntityComponent { - id: (*component).type_id(), - name: component.type_name(), - component: Lock::new(component), - }) - .collect(), - }); + .push_entity(components.into_vec()) } /// Adds a globally shared singleton value. @@ -169,23 +140,12 @@ impl World .drain(..) { match action { - Action::Spawn(components) => { - self.data - .component_storage - .write_nonblock() - .expect("Failed to acquire read-write component storage lock") - .entities - .push(Entity { - components: components - .into_iter() - .map(|component| EntityComponent { - id: (*component).type_id(), - name: component.type_name(), - component: Lock::new(component), - }) - .collect(), - }); - } + Action::Spawn(components) => self + .data + .component_storage + .write_nonblock() + .expect("Failed to acquire read-write component storage lock") + .push_entity(components), Action::Stop => { self.stop.store(true, Ordering::Relaxed); } @@ -240,6 +200,15 @@ pub struct WorldData action_queue: Arc>, } +#[derive(Debug)] +#[non_exhaustive] +pub struct EntityComponent +{ + pub id: TypeId, + pub name: &'static str, + pub component: Lock>, +} + #[derive(Debug, Default)] struct ActionQueue { @@ -267,54 +236,6 @@ impl TypeName for ActionQueue } } -#[derive(Debug, Default)] -pub struct ComponentStorage -{ - entities: Vec, -} - -impl ComponentStorage -{ - fn find_entity_with_components( - &self, - start_index: usize, - component_type_ids: &[(TypeId, ComponentIsOptional)], - ) -> Option<(usize, &Entity)> - { - // TODO: This is a really dumb and slow way to do this. Refactor the world - // to store components in archetypes - self.entities.iter().enumerate().skip(start_index).find( - move |(_index, entity)| { - let entity_components = entity - .components - .iter() - .map(|component| component.id) - .collect::>(); - - if component_type_ids - .iter() - .filter(|(_, is_optional)| *is_optional == ComponentIsOptional::No) - .all(|(component_type_id, _)| { - entity_components.contains(component_type_id) - }) - { - return true; - } - - false - }, - ) - } -} - -impl TypeName for ComponentStorage -{ - fn type_name(&self) -> &'static str - { - type_name::() - } -} - #[derive(Debug, thiserror::Error)] #[error("Sole {0} already exists")] pub struct SoleAlreadyExistsError(pub &'static str); -- cgit v1.2.3-18-g5258