summaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
authorDominik Kaiser2026-06-02 23:23:57 +0200
committerDominik Kaiser2026-06-02 23:23:57 +0200
commit4632dd8e9e95a377993ef677a157202fda8072f9 (patch)
tree60990f769556109c98e697c72dac1ad92f774d0f /src/lib.rs
downloadecs-main.tar.gz
ecs-main.zip
Implement ECSHEADmain
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs110
1 files changed, 110 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs
new file mode 100644
index 0000000..be4c2bf
--- /dev/null
+++ b/src/lib.rs
@@ -0,0 +1,110 @@
+pub mod bundle;
+pub mod component;
+pub mod query;
+pub mod sparse_set;
+
+pub mod prelude {
+ pub use super::component::Component;
+ pub use super::component::ComponentStorage;
+ pub use super::sparse_set::SparseSet;
+ pub use super::Entity;
+ pub use super::bundle::ComponentBundle;
+ pub use super::query::Query;
+ pub use super::query::QueryMut;
+}
+
+pub type Entity = usize;
+
+#[macro_export]
+macro_rules! ecs {
+ ($name:ident {
+ context: $ctx:ty,
+ components: {
+ $($comp:ident),* $(,)?
+ },
+ systems: {
+ $($sys:path),* $(,)?
+ }$(,)?
+ }) => {
+ #[allow(non_snake_case)]
+ struct $name {
+ next_entity: Entity,
+ $($comp: SparseSet<$comp>),*
+ }
+
+ impl $name {
+ pub fn new() -> Self {
+ Self {
+ next_entity: 0,
+ $($comp: SparseSet::new()),*
+ }
+ }
+
+ pub fn update(&mut self, ctx: &mut $ctx) {
+ $($sys(self, ctx));*
+ }
+
+ pub fn insert<T: ComponentBundle<Self>>(&mut self, entity: Entity, bundle: T) {
+ bundle.insert(self, entity);
+ }
+
+ pub fn spawn<T: ComponentBundle<Self>>(&mut self, bundle:T) -> Entity {
+ let e = self.next_entity;
+ self.next_entity += 1;
+ bundle.insert(self, e);
+ e
+ }
+
+ pub fn remove<T: Component>(&mut self, entity: Entity)
+ where
+ Self: ComponentStorage<T>,
+ {
+ self.storage_mut().remove(entity);
+ }
+
+ pub fn delete(&mut self, entity: Entity) {
+ $(self.$comp.remove(entity);)*
+ }
+
+ pub fn get<T: Component>(&self, entity: Entity) -> Option<&T>
+ where
+ Self: ComponentStorage<T>,
+ {
+ self.storage().get(entity)
+ }
+
+ pub fn get_mut<T: Component>(&mut self, entity: Entity) -> Option<&mut T>
+ where
+ Self: ComponentStorage<T>,
+ {
+ self.storage_mut().get_mut(entity)
+ }
+
+ }
+
+ impl<'a> $name {
+ pub fn query<Q: Query<'a, Self>>(&'a self) -> impl Iterator<Item = Q::Item> {
+ Q::run(self)
+ }
+
+ pub fn query_mut<Q: QueryMut<'a, Self>>(&'a mut self)
+ -> impl Iterator<Item = Q::Item> {
+ Q::run_mut(self)
+ }
+ }
+
+ $(impl ComponentStorage<$comp> for $name {
+ fn storage(&self) -> &SparseSet<$comp> {
+ &self.$comp
+ }
+
+ fn storage_mut(&mut self) -> &mut SparseSet<$comp> {
+ &mut self.$comp
+ }
+
+ fn storage_ptr(&mut self) -> *mut SparseSet<$comp> {
+ &mut self.$comp
+ }
+ })+
+ };
+}