diff options
author | HampusM <hampus@hampusmat.com> | 2024-02-20 21:55:33 +0100 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2024-02-20 21:55:33 +0100 |
commit | 3e18fb061538265610041d45c45642ee2686efd9 (patch) | |
tree | ca5deab4266261c3b8e5a3b4637fac916cd8ff77 /ecs/src | |
parent | 88d6ae3e4854c5fb4b37f75a29aba4f13dcfb382 (diff) |
feat(ecs): rename WorldExtra to ComponentStorage & make public
Diffstat (limited to 'ecs/src')
-rw-r--r-- | ecs/src/lib.rs | 39 |
1 files changed, 21 insertions, 18 deletions
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<Event> { systems: Vec<Box<dyn AnySystem>>, events: HashMap<Event, Vec<usize>>, - extra: WorldExtra, -} - -#[derive(Debug)] -struct WorldExtra -{ - entities: Vec<Entity>, + component_storage: ComponentStorage, } impl<Event> World<Event> @@ -38,7 +32,7 @@ impl<Event> World<Event> { Self { systems: Vec::new(), - extra: WorldExtra { entities: Vec::new() }, + component_storage: ComponentStorage { entities: Vec::new() }, events: HashMap::new(), } } @@ -47,7 +41,7 @@ impl<Event> World<Event> where Comps: ComponentSequence, { - self.extra + self.component_storage .entities .push(Entity { components: components.into_vec() }); } @@ -81,7 +75,7 @@ impl<Event> World<Event> 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<Event> World<Event> where Comps: ComponentSequence, { - Query::new(&mut self.extra) + Query::new(&mut self.component_storage) } } @@ -105,16 +99,16 @@ pub type System<Comps> = fn(Query<Comps>); trait AnySystem { - fn call(&self, world: &mut WorldExtra); + fn call(&self, component_storage: &mut ComponentStorage); } impl<Comps> AnySystem for System<Comps> 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<Comps>, } 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<Comps> { 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<Entity>, +} |