diff options
author | HampusM <hampus@hampusmat.com> | 2024-04-09 21:14:54 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2024-04-09 21:14:54 +0200 |
commit | 50234ddc9ee7acd6d2b8d0a5626caf9e9293c0da (patch) | |
tree | 8a2ee16c5ace5d018bcbb85fb5d7477b2cb1492e | |
parent | c47067d4c1eb2e6f2ea0ac7d3daba01d4f46e5e1 (diff) |
refactor(ecs): move Local to own module
-rw-r--r-- | ecs/examples/with_local.rs | 2 | ||||
-rw-r--r-- | ecs/src/component.rs | 84 | ||||
-rw-r--r-- | ecs/src/component/local.rs | 79 |
3 files changed, 83 insertions, 82 deletions
diff --git a/ecs/examples/with_local.rs b/ecs/examples/with_local.rs index 0342930..eb5de28 100644 --- a/ecs/examples/with_local.rs +++ b/ecs/examples/with_local.rs @@ -1,4 +1,4 @@ -use ecs::component::Local; +use ecs::component::local::Local; use ecs::event::{Event, Id as EventId}; use ecs::system::{Into, System}; use ecs::{Component, Query, World}; diff --git a/ecs/src/component.rs b/ecs/src/component.rs index c1f8168..7a61f39 100644 --- a/ecs/src/component.rs +++ b/ecs/src/component.rs @@ -1,18 +1,13 @@ use std::any::{Any, TypeId}; use std::fmt::Debug; -use std::ops::{Deref, DerefMut}; use seq_macro::seq; use crate::lock::{Lock, WriteGuard}; -use crate::system::{ - ComponentRefMut, - Input as SystemInput, - Param as SystemParam, - System, -}; +use crate::system::{ComponentRefMut, Input as SystemInput}; use crate::type_name::TypeName; -use crate::WorldData; + +pub mod local; pub trait Component: SystemInput + Any + TypeName { @@ -131,76 +126,3 @@ macro_rules! inner { seq!(C in 0..=64 { inner!(C); }); - -/// Holds a component which is local to a single system. -#[derive(Debug)] -pub struct Local<'world, LocalComponent: Component> -{ - local_component: ComponentRefMut<'world, LocalComponent>, -} - -unsafe impl<'world, LocalComponent> SystemParam<'world> for Local<'world, LocalComponent> -where - LocalComponent: Component, -{ - type Flags = (); - type Input = LocalComponent; - - fn initialize<SystemImpl>( - system: &mut impl System<'world, SystemImpl>, - input: Self::Input, - ) - { - system.set_local_component(input); - } - - fn new<SystemImpl>( - system: &'world impl System<'world, SystemImpl>, - _world_data: &'world WorldData, - ) -> Self - { - let local_component = system - .get_local_component_mut::<LocalComponent>() - .expect("Local component is uninitialized"); - - Self { local_component } - } - - fn is_compatible<Other: SystemParam<'world>>() -> bool - { - let other_comparable = Other::get_comparable(); - - let Some(other_type_id) = other_comparable.downcast_ref::<TypeId>() else { - return true; - }; - - TypeId::of::<LocalComponent>() != *other_type_id - } - - fn get_comparable() -> Box<dyn Any> - { - Box::new(TypeId::of::<LocalComponent>()) - } -} - -impl<'world, LocalComponent> Deref for Local<'world, LocalComponent> -where - LocalComponent: Component, -{ - type Target = LocalComponent; - - fn deref(&self) -> &Self::Target - { - &self.local_component - } -} - -impl<'world, LocalComponent> DerefMut for Local<'world, LocalComponent> -where - LocalComponent: Component, -{ - fn deref_mut(&mut self) -> &mut Self::Target - { - &mut self.local_component - } -} diff --git a/ecs/src/component/local.rs b/ecs/src/component/local.rs new file mode 100644 index 0000000..e1a0c1f --- /dev/null +++ b/ecs/src/component/local.rs @@ -0,0 +1,79 @@ +use std::any::{Any, TypeId}; +use std::ops::{Deref, DerefMut}; + +use crate::component::Component; +use crate::system::{ComponentRefMut, Param as SystemParam, System}; +use crate::WorldData; + +/// Holds a component which is local to a single system. +#[derive(Debug)] +pub struct Local<'world, LocalComponent: Component> +{ + local_component: ComponentRefMut<'world, LocalComponent>, +} + +unsafe impl<'world, LocalComponent> SystemParam<'world> for Local<'world, LocalComponent> +where + LocalComponent: Component, +{ + type Flags = (); + type Input = LocalComponent; + + fn initialize<SystemImpl>( + system: &mut impl System<'world, SystemImpl>, + input: Self::Input, + ) + { + system.set_local_component(input); + } + + fn new<SystemImpl>( + system: &'world impl System<'world, SystemImpl>, + _world_data: &'world WorldData, + ) -> Self + { + let local_component = system + .get_local_component_mut::<LocalComponent>() + .expect("Local component is uninitialized"); + + Self { local_component } + } + + fn is_compatible<Other: SystemParam<'world>>() -> bool + { + let other_comparable = Other::get_comparable(); + + let Some(other_type_id) = other_comparable.downcast_ref::<TypeId>() else { + return true; + }; + + TypeId::of::<LocalComponent>() != *other_type_id + } + + fn get_comparable() -> Box<dyn Any> + { + Box::new(TypeId::of::<LocalComponent>()) + } +} + +impl<'world, LocalComponent> Deref for Local<'world, LocalComponent> +where + LocalComponent: Component, +{ + type Target = LocalComponent; + + fn deref(&self) -> &Self::Target + { + &self.local_component + } +} + +impl<'world, LocalComponent> DerefMut for Local<'world, LocalComponent> +where + LocalComponent: Component, +{ + fn deref_mut(&mut self) -> &mut Self::Target + { + &mut self.local_component + } +} |