diff options
author | HampusM <hampus@hampusmat.com> | 2025-01-23 19:19:55 +0100 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2025-01-23 19:19:55 +0100 |
commit | c1fd49bfd8b2b23069fd522f6d7d2c3424801638 (patch) | |
tree | 7ad34fc5739301b21f41e15c1858d1575f83cc67 | |
parent | c10f3e758f7d98750cfffef336a124d6d65c636f (diff) |
fix(ecs): make optional relationships possible
-rw-r--r-- | ecs/src/component.rs | 8 | ||||
-rw-r--r-- | ecs/src/relationship.rs | 69 |
2 files changed, 73 insertions, 4 deletions
diff --git a/ecs/src/component.rs b/ecs/src/component.rs index 35e5430..b0423c5 100644 --- a/ecs/src/component.rs +++ b/ecs/src/component.rs @@ -9,7 +9,7 @@ use crate::event::component::{ Removed as ComponentRemovedEvent, }; use crate::lock::{Error as LockError, Lock, ReadGuard, WriteGuard}; -use crate::system::{ComponentRef, ComponentRefMut, Input as SystemInput}; +use crate::system::Input as SystemInput; use crate::type_name::TypeName; use crate::uid::Uid; use crate::util::Array; @@ -105,10 +105,12 @@ impl TypeName for Box<dyn Component> impl<ComponentT> Component for Option<ComponentT> where ComponentT: Component, + for<'a> Option<ComponentT::Ref<'a>>: FromOptional<'a> + FromLockedOptional<'a>, + for<'a> Option<ComponentT::RefMut<'a>>: FromOptionalMut<'a> + FromLockedOptional<'a>, { type Component = ComponentT; - type Ref<'component> = Option<ComponentRef<'component, ComponentT>>; - type RefMut<'component> = Option<ComponentRefMut<'component, ComponentT>>; + type Ref<'component> = Option<ComponentT::Ref<'component>>; + type RefMut<'component> = Option<ComponentT::RefMut<'component>>; fn id() -> Uid { diff --git a/ecs/src/relationship.rs b/ecs/src/relationship.rs index d136db4..143b589 100644 --- a/ecs/src/relationship.rs +++ b/ecs/src/relationship.rs @@ -10,7 +10,7 @@ use crate::component::{ FromOptional as FromOptionalComponent, FromOptionalMut as FromOptionalMutComponent, }; -use crate::lock::{Error as LockError, Lock, ReadGuard}; +use crate::lock::{Error as LockError, Lock, ReadGuard, WriteGuard}; use crate::system::{ComponentRef, ComponentRefMut}; use crate::uid::{Kind as UidKind, Uid}; use crate::World; @@ -104,6 +104,22 @@ where } } +impl<'rel_comp, Kind, ComponentT> FromOptionalMutComponent<'rel_comp> + for Option<RelationMut<'rel_comp, Kind, ComponentT>> +where + ComponentT: Component, +{ + fn from_optional_mut_component( + optional_component: Option<WriteGuard<'rel_comp, Box<dyn Component>>>, + world: &'rel_comp World, + ) -> Self + { + optional_component.map(|component| { + RelationMut::from_optional_mut_component(Some(component), world) + }) + } +} + impl<'rel_comp, Kind, ComponentT> FromLockedOptionalComponent<'rel_comp> for RelationMut<'rel_comp, Kind, ComponentT> where @@ -123,6 +139,24 @@ where } } +impl<'rel_comp, Kind, ComponentT> FromLockedOptionalComponent<'rel_comp> + for Option<RelationMut<'rel_comp, Kind, ComponentT>> +where + ComponentT: Component, +{ + fn from_locked_optional_component( + optional_component: Option<&'rel_comp Lock<Box<dyn Component>>>, + world: &'rel_comp World, + ) -> Result<Self, crate::lock::Error> + { + optional_component + .map(|component| { + RelationMut::from_locked_optional_component(Some(component), world) + }) + .transpose() + } +} + impl<'rel_comp, Kind, ComponentT> RelationMut<'rel_comp, Kind, ComponentT> where ComponentT: Component, @@ -327,6 +361,21 @@ where } } +impl<'rel_comp, Kind, ComponentT> FromOptionalComponent<'rel_comp> + for Option<Relation<'rel_comp, Kind, ComponentT>> +where + ComponentT: Component, +{ + fn from_optional_component( + optional_component: Option<ReadGuard<'rel_comp, Box<dyn Component>>>, + world: &'rel_comp World, + ) -> Self + { + optional_component + .map(|component| Relation::from_optional_component(Some(component), world)) + } +} + impl<'rel_comp, Kind, ComponentT> FromLockedOptionalComponent<'rel_comp> for Relation<'rel_comp, Kind, ComponentT> where @@ -346,6 +395,24 @@ where } } +impl<'rel_comp, Kind, ComponentT> FromLockedOptionalComponent<'rel_comp> + for Option<Relation<'rel_comp, Kind, ComponentT>> +where + ComponentT: Component, +{ + fn from_locked_optional_component( + optional_component: Option<&'rel_comp Lock<Box<dyn Component>>>, + world: &'rel_comp World, + ) -> Result<Self, crate::lock::Error> + { + optional_component + .map(|component| { + Relation::from_locked_optional_component(Some(component), world) + }) + .transpose() + } +} + impl<'rel_comp, Kind, ComponentT> Relation<'rel_comp, Kind, ComponentT> where ComponentT: Component, |