summaryrefslogtreecommitdiff
path: root/ecs/src
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2024-04-06 15:35:51 +0200
committerHampusM <hampus@hampusmat.com>2024-04-06 16:09:25 +0200
commit6038a5b701ee9ea3bd3b5da50d7d06930cd9a270 (patch)
tree4b00618fd6f8290006dcedb92f15bde20cba93ce /ecs/src
parent97f973d685baf389dcde08044dd3dfb7ba556050 (diff)
refactor(ecs): make stopping into a action
Diffstat (limited to 'ecs/src')
-rw-r--r--ecs/src/actions.rs15
-rw-r--r--ecs/src/flags.rs66
-rw-r--r--ecs/src/lib.rs29
3 files changed, 21 insertions, 89 deletions
diff --git a/ecs/src/actions.rs b/ecs/src/actions.rs
index ea4837a..a5aaf0c 100644
--- a/ecs/src/actions.rs
+++ b/ecs/src/actions.rs
@@ -26,6 +26,20 @@ impl<'world> Actions<'world>
.expect("Failed to aquire read-write action queue lock")
.push(Action::Spawn(components.into_vec()));
}
+
+ /// Adds stopping the loop in [`Engine::event_loop`] at the next oppertune time to the
+ /// action queue.
+ ///
+ /// # Panics
+ /// Will panic if a mutable internal lock cannot be acquired.
+ pub fn stop(&mut self)
+ {
+ self.world_data
+ .action_queue
+ .write_nonblock()
+ .expect("Failed to aquire read-write action queue lock")
+ .push(Action::Stop);
+ }
}
unsafe impl<'world> SystemParam<'world> for Actions<'world>
@@ -66,6 +80,7 @@ unsafe impl<'world> SystemParam<'world> for Actions<'world>
pub(crate) enum Action
{
Spawn(Vec<Box<dyn Component>>),
+ Stop,
}
struct Comparable;
diff --git a/ecs/src/flags.rs b/ecs/src/flags.rs
deleted file mode 100644
index ad10a0f..0000000
--- a/ecs/src/flags.rs
+++ /dev/null
@@ -1,66 +0,0 @@
-use std::any::Any;
-
-use crate::lock::WriteGuard;
-use crate::system::{
- NoInitParamFlag as NoInitSystemParamFlag,
- Param as SystemParam,
- System,
-};
-use crate::tuple::FilterExclude as TupleFilterExclude;
-use crate::{WorldData, WorldFlags};
-
-#[derive(Debug)]
-pub struct Flags<'world>
-{
- world_flags: WriteGuard<'world, WorldFlags>,
-}
-
-impl<'world> Flags<'world>
-{
- /// Calling this function makes the loop in [`Engine::event_loop`] stop at the next
- /// opportune time.
- pub fn stop(&mut self)
- {
- self.world_flags.stop = true;
- }
-}
-
-unsafe impl<'world> SystemParam<'world> for Flags<'world>
-{
- type Flags = NoInitSystemParamFlag;
- type Input = TupleFilterExclude;
-
- fn initialize<SystemImpl>(
- _system: &mut impl System<'world, SystemImpl>,
- _input: Self::Input,
- )
- {
- }
-
- fn new<SystemImpl>(
- _system: &'world impl System<'world, SystemImpl>,
- world_data: &'world WorldData,
- ) -> Self
- {
- Self {
- world_flags: world_data
- .flags
- .write_nonblock()
- .expect("Failed to acquire read-write world flags lock"),
- }
- }
-
- fn is_compatible<Other: SystemParam<'world>>() -> bool
- {
- let other_comparable = Other::get_comparable();
-
- other_comparable.downcast_ref::<Comparable>().is_none()
- }
-
- fn get_comparable() -> Box<dyn Any>
- {
- Box::new(Comparable)
- }
-}
-
-struct Comparable;
diff --git a/ecs/src/lib.rs b/ecs/src/lib.rs
index 5273b67..1c4e44f 100644
--- a/ecs/src/lib.rs
+++ b/ecs/src/lib.rs
@@ -7,6 +7,7 @@ use std::marker::PhantomData;
use std::mem::ManuallyDrop;
use std::ops::RangeBounds;
use std::slice::Iter as SliceIter;
+use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Weak};
use std::vec::Drain;
@@ -26,7 +27,6 @@ use crate::type_name::TypeName;
pub mod actions;
pub mod component;
pub mod event;
-pub mod flags;
pub mod lock;
pub mod system;
pub mod tuple;
@@ -53,6 +53,7 @@ pub struct World
{
systems: Vec<TypeErasedSystem>,
data: WorldData,
+ stop: AtomicBool,
}
impl World
@@ -160,6 +161,9 @@ impl World
.collect(),
});
}
+ Action::Stop => {
+ self.stop.store(true, Ordering::Relaxed);
+ }
}
}
}
@@ -179,13 +183,7 @@ impl World
self.perform_queued_actions();
- let flags = self
- .data
- .flags
- .read_nonblock()
- .expect("Failed to aquire lock to flags");
-
- if flags.stop {
+ if self.stop.load(Ordering::Relaxed) {
break;
}
}
@@ -209,26 +207,11 @@ impl World
}
#[derive(Debug, Default)]
-struct WorldFlags
-{
- stop: bool,
-}
-
-impl TypeName for WorldFlags
-{
- fn type_name(&self) -> &'static str
- {
- type_name::<Self>()
- }
-}
-
-#[derive(Debug, Default)]
pub struct WorldData
{
events: HashMap<EventId, Vec<usize>>,
component_storage: Arc<Lock<ComponentStorage>>,
action_queue: Lock<ActionQueue>,
- flags: Lock<WorldFlags>,
}
#[derive(Debug, Default)]