From 3e18fb061538265610041d45c45642ee2686efd9 Mon Sep 17 00:00:00 2001 From: HampusM Date: Tue, 20 Feb 2024 21:55:33 +0100 Subject: feat(ecs): rename WorldExtra to ComponentStorage & make public --- ecs/src/lib.rs | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) (limited to 'ecs/src/lib.rs') diff --git a/ecs/src/lib.rs b/ecs/src/lib.rs index 5b8942b..f824ebc 100644 --- a/ecs/src/lib.rs +++ b/ecs/src/lib.rs @@ -22,13 +22,7 @@ pub struct World { systems: Vec>, events: HashMap>, - extra: WorldExtra, -} - -#[derive(Debug)] -struct WorldExtra -{ - entities: Vec, + component_storage: ComponentStorage, } impl World @@ -38,7 +32,7 @@ impl World { Self { systems: Vec::new(), - extra: WorldExtra { entities: Vec::new() }, + component_storage: ComponentStorage { entities: Vec::new() }, events: HashMap::new(), } } @@ -47,7 +41,7 @@ impl World where Comps: ComponentSequence, { - self.extra + self.component_storage .entities .push(Entity { components: components.into_vec() }); } @@ -81,7 +75,7 @@ impl World for system_index in system_indices { let system = self.systems.get_mut(system_index).unwrap(); - system.call(&mut self.extra); + system.call(&mut self.component_storage); } } @@ -89,7 +83,7 @@ impl World where Comps: ComponentSequence, { - Query::new(&mut self.extra) + Query::new(&mut self.component_storage) } } @@ -105,16 +99,16 @@ pub type System = fn(Query); trait AnySystem { - fn call(&self, world: &mut WorldExtra); + fn call(&self, component_storage: &mut ComponentStorage); } impl AnySystem for System where Comps: ComponentSequence, { - fn call(&self, world: &mut WorldExtra) + fn call(&self, component_storage: &mut ComponentStorage) { - self(Query::new(world)); + self(Query::new(component_storage)); } } @@ -129,15 +123,18 @@ impl Debug for dyn AnySystem #[derive(Debug)] pub struct Query<'world, Comps> { - world: &'world mut WorldExtra, + component_storage: &'world mut ComponentStorage, comps_pd: PhantomData, } impl<'world, Comps> Query<'world, Comps> { - fn new(world: &'world mut WorldExtra) -> Self + fn new(component_storage: &'world mut ComponentStorage) -> Self { - Self { world, comps_pd: PhantomData } + Self { + component_storage, + comps_pd: PhantomData, + } } } @@ -148,7 +145,7 @@ where pub fn iter_mut(&mut self) -> QueryComponentIter { QueryComponentIter { - entity_iter: self.world.entities.iter_mut(), + entity_iter: self.component_storage.entities.iter_mut(), component_type_ids: Comps::type_ids(), comps_pd: PhantomData, } @@ -192,3 +189,9 @@ where Some(Comps::from_components(&mut entity.components)) } } + +#[derive(Debug)] +pub struct ComponentStorage +{ + entities: Vec, +} -- cgit v1.2.3-18-g5258