summaryrefslogtreecommitdiff
path: root/ecs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2024-03-16 13:52:31 +0100
committerHampusM <hampus@hampusmat.com>2024-03-16 13:52:31 +0100
commit4c9d15356b195c0d51bfdea7d24dc4a5cd000263 (patch)
treebe02c32e064af064b4aa7ec27a1e4fec825f478e /ecs
parent01066718b0f13846587d26b1869f03e3713082c6 (diff)
feat(ecs): make perform_queued_actions take non mut self reference
Diffstat (limited to 'ecs')
-rw-r--r--ecs/src/lib.rs50
1 files changed, 33 insertions, 17 deletions
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<EventId, Vec<usize>>,
- component_storage: ComponentStorage,
+ component_storage: RefCell<ComponentStorage>,
action_queue: RefCell<Vec<Action>>,
}
@@ -131,7 +142,7 @@ pub struct Query<'world, Comps>
where
Comps: ComponentSequence,
{
- world_data: &'world WorldData,
+ component_storage: Ref<'world, ComponentStorage>,
comps_pd: PhantomData<Comps>,
}
@@ -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,
+ }
}
}