From dba19db5f649f73a5abb1bc3589580721a9a4e32 Mon Sep 17 00:00:00 2001 From: HampusM Date: Wed, 6 Mar 2024 00:23:07 +0100 Subject: refactor(ecs): pass around all world data and not component storage --- ecs/src/lib.rs | 53 +++++++++++++++++++++++------------------------------ 1 file changed, 23 insertions(+), 30 deletions(-) (limited to 'ecs/src/lib.rs') diff --git a/ecs/src/lib.rs b/ecs/src/lib.rs index e22913f..0c06472 100644 --- a/ecs/src/lib.rs +++ b/ecs/src/lib.rs @@ -23,18 +23,17 @@ pub mod tuple; pub use ecs_macros::Component; -#[derive(Debug)] +#[derive(Debug, Default)] struct Entity { components: Vec>, } -#[derive(Debug)] +#[derive(Debug, Default)] pub struct World { systems: Vec, - events: HashMap>, - component_storage: ComponentStorage, + data: WorldData, } impl World @@ -42,18 +41,15 @@ impl World #[must_use] pub fn new() -> Self { - Self { - systems: Vec::new(), - component_storage: ComponentStorage { entities: Vec::new() }, - events: HashMap::new(), - } + Self::default() } pub fn create_entity(&mut self, components: Comps) where Comps: ComponentSequence, { - self.component_storage + self.data + .component_storage .entities .push(Entity { components: components.into_vec() }); } @@ -67,7 +63,8 @@ impl World { self.systems.push(system.into_type_erased()); - self.events + self.data + .events .entry(event.id()) .or_default() .push(self.systems.len() - 1); @@ -80,14 +77,14 @@ impl World /// Will panic if a system has dissapeared. pub fn emit(&mut self, event: &impl Event) { - let Some(system_indices) = self.events.get(&event.id()).cloned() else { + let Some(system_indices) = self.data.events.get(&event.id()).cloned() else { return; }; for system_index in system_indices { let system = self.systems.get_mut(system_index).unwrap(); - system.run(&mut self.component_storage); + system.run(&mut self.data); } } @@ -95,16 +92,15 @@ impl World where Comps: ComponentSequence, { - Query::new(&mut self.component_storage) + Query::new(&mut self.data) } } -impl Default for World +#[derive(Debug, Default)] +pub struct WorldData { - fn default() -> Self - { - Self::new() - } + events: HashMap>, + component_storage: ComponentStorage, } #[derive(Debug)] @@ -112,7 +108,7 @@ pub struct Query<'world, Comps> where Comps: ComponentSequence, { - component_storage: &'world mut ComponentStorage, + world_data: &'world mut WorldData, comps_pd: PhantomData, } @@ -120,12 +116,9 @@ impl<'world, Comps> Query<'world, Comps> where Comps: ComponentSequence, { - fn new(component_storage: &'world mut ComponentStorage) -> Self + fn new(world_data: &'world mut WorldData) -> Self { - Self { - component_storage, - comps_pd: PhantomData, - } + Self { world_data, comps_pd: PhantomData } } } @@ -136,7 +129,7 @@ where pub fn iter(&self) -> QueryComponentIter { QueryComponentIter { - entity_iter: self.component_storage.entities.iter(), + entity_iter: self.world_data.component_storage.entities.iter(), component_type_ids: Comps::type_ids(), comps_pd: PhantomData, } @@ -145,7 +138,7 @@ where pub fn iter_mut(&mut self) -> QueryComponentMutIter { QueryComponentMutIter { - entity_iter: self.component_storage.entities.iter_mut(), + entity_iter: self.world_data.component_storage.entities.iter_mut(), component_type_ids: Comps::type_ids(), comps_pd: PhantomData, } @@ -191,10 +184,10 @@ where fn new( _system: &'world mut impl System, - component_storage: &'world mut ComponentStorage, + world_data: &'world mut WorldData, ) -> Self { - Self::new(component_storage) + Self::new(world_data) } fn is_compatible>() -> bool @@ -338,7 +331,7 @@ where }) } -#[derive(Debug)] +#[derive(Debug, Default)] pub struct ComponentStorage { entities: Vec, -- cgit v1.2.3-18-g5258