diff options
Diffstat (limited to 'engine-ecs/src/sole.rs')
| -rw-r--r-- | engine-ecs/src/sole.rs | 57 |
1 files changed, 31 insertions, 26 deletions
diff --git a/engine-ecs/src/sole.rs b/engine-ecs/src/sole.rs index 9e27fee..9409bcb 100644 --- a/engine-ecs/src/sole.rs +++ b/engine-ecs/src/sole.rs @@ -1,33 +1,43 @@ use std::any::{type_name, Any}; use std::fmt::Debug; -use std::marker::PhantomData; use std::ops::{Deref, DerefMut}; -use std::sync::Arc; -use crate::lock::{Lock, WriteGuard}; +use crate::uid::Uid; +use crate::component::HandleMut as ComponentHandleMut; +use crate::component::IntoParts as IntoComponentParts; use crate::system::{Metadata as SystemMetadata, Param as SystemParam}; use crate::World; /// A type which has a single instance and is shared globally. -pub trait Sole: Any +pub trait Sole: Any + IntoComponentParts { - fn drop_last(&self) -> bool; + fn id() -> Uid + where + Self: Sized; - fn as_any_mut(&mut self) -> &mut dyn Any; + fn type_reflection() -> Option<&'static crate::reflection::Type> + where + Self: Sized; - fn as_any(&self) -> &dyn Any; + /// Returns the name of this component. + fn name(&self) -> &'static str; } impl dyn Sole { pub fn downcast_mut<Real: 'static>(&mut self) -> Option<&mut Real> { - self.as_any_mut().downcast_mut() + (self as &mut dyn Any).downcast_mut() } pub fn downcast_ref<Real: 'static>(&self) -> Option<&Real> { - self.as_any().downcast_ref() + (self as &dyn Any).downcast_ref() + } + + pub fn is<Other: 'static>(&self) -> bool + { + (self as &dyn Any).is::<Other>() } } @@ -43,25 +53,17 @@ impl Debug for dyn Sole #[derive(Debug)] pub struct Single<'world, SoleT: Sole> { - sole: WriteGuard<'world, Box<dyn Sole>>, - _ph: PhantomData<SoleT>, + sole: ComponentHandleMut<'world, SoleT>, } + impl<'world, SoleT> Single<'world, SoleT> where SoleT: Sole, { - pub(crate) fn new(sole: &'world Arc<Lock<Box<dyn Sole>>>) -> Self + pub(crate) fn new(sole: ComponentHandleMut<'world, SoleT>) -> Self { - Self { - sole: sole.write_nonblock().unwrap_or_else(|_| { - panic!( - "Failed to aquire read-write lock to single component {}", - type_name::<SoleT>() - ) - }), - _ph: PhantomData, - } + Self { sole } } } @@ -73,9 +75,12 @@ where fn new(world: &'world World, _system_metadata: &SystemMetadata) -> Self { - let sole = world.data.sole_storage.get::<SoleT>().unwrap_or_else(|| { - panic!("Sole {} was not found in world", type_name::<SoleT>()) - }); + let sole = world + .get_entity(SoleT::id()) + .and_then(|ent| ent.get_with_id_mut::<SoleT>(SoleT::id())) + .unwrap_or_else(|| { + panic!("Sole component {} was not found in world", type_name::<SoleT>()) + }); Self::new(sole) } @@ -89,7 +94,7 @@ where fn deref(&self) -> &Self::Target { - self.sole.downcast_ref().unwrap() + &*self.sole } } @@ -99,6 +104,6 @@ where { fn deref_mut(&mut self) -> &mut Self::Target { - self.sole.downcast_mut().unwrap() + &mut *self.sole } } |
