Browse Source

Add resources as well

Getty Ritter 4 years ago
parent
commit
7741a5bf38
1 changed files with 94 additions and 15 deletions
  1. 94 15
      src/lib.rs

+ 94 - 15
src/lib.rs

@@ -1,11 +1,29 @@
+pub struct SpecsUnit;
+impl<'a> specs::SystemData<'a> for SpecsUnit {
+    fn setup(_world: &mut specs::World) {
+    }
+
+    fn fetch(_world: &'a specs::World) -> Self {
+        SpecsUnit
+    }
+
+    fn reads() -> Vec<specs::prelude::ResourceId> {
+        Vec::new()
+    }
+
+    fn writes() -> Vec<specs::prelude::ResourceId> {
+        Vec::new()
+    }
+}
+
 #[macro_export]
 macro_rules! system {
-    ($name:ident $pat:tt { $($rest:tt)* } ) => {
+    ($name:ident ( $($pat:tt)* ) { $($rest:tt)* } ) => {
         pub struct $name;
         impl<'a> specs::System<'a> for $name {
-            type SystemData = args_to_systemdata!($pat);
-            fn run(&mut self, args_to_pat!($pat): Self::SystemData) {
-                for args_to_pat!($pat) in args_to_join!($pat).join() {
+            type SystemData = args_to_systemdata!(($($pat)*,));
+            fn run(&mut self, args_to_fn_pat!(($($pat)*)): Self::SystemData) {
+                for args_to_join_pat!(($($pat)*,)) in args_to_join!(($($pat)*,)).join() {
                     $($rest)*
                 }
             }
@@ -15,8 +33,8 @@ macro_rules! system {
         pub struct $name;
         impl<'a> specs::System<'a> for $name {
             type SystemData = args_to_systemdata!($pat);
-            fn run(&mut self, args_to_pat!($pat): Self::SystemData) {
-                for args_to_pat!($pat) in args_to_join!($pat).join() {
+            fn run(&mut self, args_to_fn_pat!($pat): Self::SystemData) {
+                for args_to_join_pat!($pat) in args_to_join!($pat).join() {
                     $($rest)*
                 }
                 $($finally)*
@@ -27,6 +45,7 @@ macro_rules! system {
 
 #[macro_export]
 macro_rules! args_to_systemdata {
+    ( ( ) ) => { crate::SpecsUnit };
     ( ( $name:ident : $ty:ty $(,)? ) ) => {
         ( specs::ReadStorage<'a, $ty> ,)
     };
@@ -39,10 +58,17 @@ macro_rules! args_to_systemdata {
     ( ( mut $name:ident : $ty:ty , $( $tok:tt )* ) ) => {
         ( specs::WriteStorage<'a, $ty>, args_to_systemdata!( ( $( $tok )* ) ) )
     };
+    ( ( resource $name:ident : $ty:ty , $( $tok:tt )* ) ) => {
+        ( specs::ReadExpect<'a, $ty>, args_to_systemdata!( ( $( $tok )* ) ) )
+    };
+    ( ( resource mut $name:ident : $ty:ty , $( $tok:tt )* ) ) => {
+        ( specs::WriteExpect<'a, $ty>, args_to_systemdata!( ( $( $tok )* ) ) )
+    };
 }
 
 #[macro_export]
-macro_rules! args_to_pat {
+macro_rules! args_to_fn_pat {
+    ( ( ) ) => { _ };
     ( ( $name:ident : $ty:ty $(,)? ) ) => {
         ( $name ,)
     };
@@ -50,15 +76,45 @@ macro_rules! args_to_pat {
         ( mut $name ,)
     };
     ( ( $name:ident : $ty:ty , $($tok:tt)* ) ) => {
-        ( $name, args_to_pat!( ( $($tok)* ) ) )
+        ( $name, args_to_fn_pat!( ( $($tok)* ) ) )
     };
     ( ( mut $name:ident : $ty:ty , $($tok:tt)* ) ) => {
-        ( mut $name, args_to_pat!( ( $($tok)* ) ) )
+        ( mut $name, args_to_fn_pat!( ( $($tok)* ) ) )
+    };
+    ( ( resource $name:ident : $ty:ty , $($tok:tt)* ) ) => {
+        ( $name, args_to_fn_pat!( ( $($tok)* ) ) )
+    };
+    ( ( resource mut $name:ident : $ty:ty , $($tok:tt)* ) ) => {
+        ( mut $name, args_to_fn_pat!( ( $($tok)* ) ) )
+    };
+}
+
+#[macro_export]
+macro_rules! args_to_join_pat {
+    ( ( ) ) => { _ };
+    ( ( $name:ident : $ty:ty $(,)? ) ) => {
+        ( $name ,)
+    };
+    ( ( mut $name:ident : $ty:ty $(,)? ) ) => {
+        ( mut $name ,)
+    };
+    ( ( $name:ident : $ty:ty , $($tok:tt)* ) ) => {
+        ( $name, args_to_join_pat!( ( $($tok)* ) ) )
+    };
+    ( ( mut $name:ident : $ty:ty , $($tok:tt)* ) ) => {
+        ( mut $name, args_to_join_pat!( ( $($tok)* ) ) )
+    };
+    ( ( resource $name:ident : $ty:ty , $($tok:tt)* ) ) => {
+        args_to_join_pat!( ( $($tok)* ) )
+    };
+    ( ( resource mut $name:ident : $ty:ty , $($tok:tt)* ) ) => {
+        args_to_join_pat!( ( $($tok)* ) )
     };
 }
 
 #[macro_export]
 macro_rules! args_to_join {
+    ( ( ) ) => { crate::SpecsUnit };
     ( ( $name:ident : $ty:ty $(,)? ) ) => {
         ( & $name ,)
     };
@@ -71,8 +127,15 @@ macro_rules! args_to_join {
     ( ( mut $name:ident : $ty:ty , $($tok:tt)* ) ) => {
         ( &mut $name, args_to_join!( ( $($tok)* ) ) )
     };
+    ( ( resource $name:ident : $ty:ty , $($tok:tt)* ) ) => {
+        args_to_join!( ( $($tok)* ) )
+    };
+    ( ( resource mut $name:ident : $ty:ty , $($tok:tt)* ) ) => {
+        args_to_join!( ( $($tok)* ) )
+    };
 }
 
+
 #[cfg(test)]
 mod tests {
     use specs::join::Join;
@@ -89,15 +152,31 @@ mod tests {
         type Storage = specs::VecStorage<Mov>;
     }
 
-    system! { Foo (_x: Mov, mut y: Pos) {
-        y.x += 1;
+    system! {
+        Foo (_x: Mov, mut y: Pos) {
+            y.x += 1;
+        }
     }
+
+    system! {
+        Bar (_x: Mov, mut y: Pos) {
+            y.x += 1;
+        } finally {
+            println!("Done!");
+        }
     }
 
-    system! { Bar (_x: Mov, mut y: Pos) {
-        y.x += 1;
-    } finally {
-        println!("Done!");
+    system! {
+        Baz (resource n: usize, _x: Mov, mut y: Pos) {
+            y.x += *n;
+        }
     }
+
+    system! {
+        Quux (resource mut n: usize, _x: Mov, mut y: Pos) {
+            y.x += *n;
+            *n += 1
+        }
     }
+
 }