From 4632dd8e9e95a377993ef677a157202fda8072f9 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Tue, 2 Jun 2026 23:23:57 +0200 Subject: Implement ECS --- src/component.rs | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/component.rs (limited to 'src/component.rs') 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 { + fn storage(&self) -> &SparseSet; + fn storage_mut(&mut self) -> &mut SparseSet; + fn storage_ptr(&mut self) -> *mut SparseSet; + + fn insert(&mut self, entity: Entity, component: T) { + self.storage_mut().insert(entity, component); + } +} -- cgit v1.2.3