From 4c9d15356b195c0d51bfdea7d24dc4a5cd000263 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sat, 16 Mar 2024 13:52:31 +0100 Subject: feat(ecs): make perform_queued_actions take non mut self reference --- ecs/src/lib.rs | 50 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 17 deletions(-) (limited to 'ecs/src/lib.rs') diff --git a/ecs/src/lib.rs b/ecs/src/lib.rs index d781b3e..18e7381 100644 --- a/ecs/src/lib.rs +++ b/ecs/src/lib.rs @@ -1,7 +1,7 @@ #![deny(clippy::all, clippy::pedantic)] use std::any::{Any, TypeId}; -use std::cell::RefCell; +use std::cell::{Ref, RefCell}; use std::collections::{HashMap, HashSet}; use std::fmt::Debug; use std::marker::PhantomData; @@ -51,13 +51,17 @@ impl World where Comps: ComponentSequence, { - self.data.component_storage.entities.push(Entity { - components: components - .into_vec() - .into_iter() - .map(RefCell::new) - .collect(), - }); + self.data + .component_storage + .borrow_mut() + .entities + .push(Entity { + components: components + .into_vec() + .into_iter() + .map(RefCell::new) + .collect(), + }); } pub fn register_system<'this, SystemImpl>( @@ -104,14 +108,21 @@ impl World } /// Peforms the actions that have been queued up using [`Actions`]. - pub fn perform_queued_actions(&mut self) + pub fn perform_queued_actions(&self) { for action in self.data.action_queue.borrow_mut().drain(..) { match action { Action::Spawn(components) => { - self.data.component_storage.entities.push(Entity { - components: components.into_iter().map(RefCell::new).collect(), - }); + self.data + .component_storage + .borrow_mut() + .entities + .push(Entity { + components: components + .into_iter() + .map(RefCell::new) + .collect(), + }); } } } @@ -122,7 +133,7 @@ impl World pub struct WorldData { events: HashMap>, - component_storage: ComponentStorage, + component_storage: RefCell, action_queue: RefCell>, } @@ -131,7 +142,7 @@ pub struct Query<'world, Comps> where Comps: ComponentSequence, { - world_data: &'world WorldData, + component_storage: Ref<'world, ComponentStorage>, comps_pd: PhantomData, } @@ -139,10 +150,12 @@ impl<'world, Comps> Query<'world, Comps> where Comps: ComponentSequence, { - pub fn iter(&self) -> QueryComponentIter<'world, Comps> + pub fn iter<'this>(&'this self) -> QueryComponentIter<'world, Comps> + where + 'this: 'world, { QueryComponentIter { - entity_iter: self.world_data.component_storage.entities.iter(), + entity_iter: self.component_storage.entities.iter(), component_type_ids: Comps::type_ids(), comps_pd: PhantomData, } @@ -150,7 +163,10 @@ where fn new(world_data: &'world WorldData) -> Self { - Self { world_data, comps_pd: PhantomData } + Self { + component_storage: world_data.component_storage.borrow(), + comps_pd: PhantomData, + } } } -- cgit v1.2.3-18-g5258