Browse Source

Special-case Entity

Getty Ritter 4 years ago
parent
commit
70162d7755
2 changed files with 22 additions and 1 deletions
  1. 2 0
      README.md
  2. 20 1
      src/lib.rs

+ 2 - 0
README.md

@@ -77,6 +77,8 @@ system! {
 }
 ```
 
+This macro also special-cases the `Entity` type, so that you can loop over all entities.
+
 # resources
 
 To get access to resources, you can use the `resource` or `resource mut` keywords. These will be represented as `ReadExpect` and `WriteExpect` resources in the relevant `SystemData`, and will not be looped over in the loop of the macro. Due to implementation restrictions, a `resource` _cannot_ be the last thing in the argument list, so it's best to keep `resource`s first.

+ 20 - 1
src/lib.rs

@@ -43,6 +43,9 @@ macro_rules! system_impl {
 
 #[macro_export]
 macro_rules! args_to_systemdata {
+    ( ( $name:ident : Entity $(,)? ) ) => {
+        ( specs::Entities<'a>, )
+    };
     ( ( $name:ident : $ty:ty $(,)? ) ) => {
         ( specs::ReadStorage<'a, $ty> ,)
     };
@@ -55,6 +58,9 @@ macro_rules! args_to_systemdata {
     ( ( resource mut $name:ident : $ty:ty $(,)? ) ) => {
         compile_error!("Resources cannot come at the end of the argument block.")
     };
+    ( ( $name:ident : Entity , $($tok:tt)* ) ) => {
+        ( specs::Entities<'a>, args_to_systemdata!( ( $( $tok )* ) ) )
+    };
     ( ( $name:ident : $ty:ty , $($tok:tt)* ) ) => {
         ( specs::ReadStorage<'a, $ty>, args_to_systemdata!( ( $( $tok )* ) ) )
     };
@@ -153,7 +159,6 @@ macro_rules! args_to_join {
     };
 }
 
-
 #[cfg(test)]
 mod tests {
 
@@ -232,4 +237,18 @@ mod tests {
         }
     }
 
+    system! {
+        Pippo (_e: Entity, _x: Mov, mut y: Pos) {
+            y.x += 1;
+        }
+    }
+
+    system_impl! {
+        PippoAlt (_e: Entity, x: Mov, mut y: Pos) {
+            for (_, mut y) in (&x, &mut y).join() {
+                y.x += 1;
+            }
+        }
+    }
+
 }