diff options
Diffstat (limited to 'src/component.rs')
| -rw-r--r-- | src/component.rs | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/src/component.rs b/src/component.rs new file mode 100644 index 0000000..1b36c3a --- /dev/null +++ b/src/component.rs @@ -0,0 +1,52 @@ +use super::Entity; +use super::sparse_set::SparseSet; + +pub trait Component {} + +#[macro_export] +macro_rules! component { + ($name:ident) => { + #[derive(Clone, Copy, Debug)] + pub struct $name; + impl Component for $name {} + }; + + ($name:ident, $ty:ty) => { + #[derive(Clone, Copy, Debug)] + pub struct $name(pub $ty); + + impl std::ops::Deref for $name { + type Target = $ty; + fn deref(&self) -> &Self::Target { + &self.0 + } + } + + impl std::ops::DerefMut for $name { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } + } + + impl Component for $name {} + }; + + ($name:ident, { $($field:ident : $ty:ty),+ $(,)? }) => { + #[derive(Clone, Debug)] + pub struct $name { + $(pub $field: $ty),+ + } + + impl Component for $name {} + }; +} + +pub trait ComponentStorage<T: Component> { + fn storage(&self) -> &SparseSet<T>; + fn storage_mut(&mut self) -> &mut SparseSet<T>; + fn storage_ptr(&mut self) -> *mut SparseSet<T>; + + fn insert(&mut self, entity: Entity, component: T) { + self.storage_mut().insert(entity, component); + } +} |
