Browse Source

Switch to using macro

Getty Ritter 4 years ago
parent
commit
23b465fe6e
4 changed files with 35 additions and 29 deletions
  1. 11 0
      Cargo.lock
  2. 1 0
      ch1/Cargo.toml
  3. 2 1
      ch2/Cargo.toml
  4. 21 28
      ch2/src/main.rs

+ 11 - 0
Cargo.lock

@@ -260,6 +260,7 @@ version = "0.1.0"
 dependencies = [
  "carpet 0.1.0",
  "ggez 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
+ "specs-system-macro 0.1.0 (git+https://git.infinitenegativeutility.com/getty/specs-system-macro.git)",
 ]
 
 [[package]]
@@ -270,6 +271,7 @@ dependencies = [
  "ggez 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
  "specs 0.15.1 (registry+https://github.com/rust-lang/crates.io-index)",
  "specs-derive 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "specs-system-macro 0.1.0 (git+https://git.infinitenegativeutility.com/getty/specs-system-macro.git)",
 ]
 
 [[package]]
@@ -1879,6 +1881,14 @@ dependencies = [
  "syn 0.15.44 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
+[[package]]
+name = "specs-system-macro"
+version = "0.1.0"
+source = "git+https://git.infinitenegativeutility.com/getty/specs-system-macro.git#d2e1a7a72064d8ccc096216c6e932390091d68aa"
+dependencies = [
+ "specs 0.15.1 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
 [[package]]
 name = "stb_truetype"
 version = "0.3.0"
@@ -2473,6 +2483,7 @@ dependencies = [
 "checksum smithay-client-toolkit 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "2ccb8c57049b2a34d2cc2b203fa785020ba0129d31920ef0d317430adaf748fa"
 "checksum specs 0.15.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4943fde8c5d3d14c3d19d2a4c7abbd7b626c270a19e6cd35252294a48feb698c"
 "checksum specs-derive 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a63549fa0d4a6f76e99e6634c328f25d0c9fa8ad6f8498aef74f6c35c0b269e5"
+"checksum specs-system-macro 0.1.0 (git+https://git.infinitenegativeutility.com/getty/specs-system-macro.git)" = "<none>"
 "checksum stb_truetype 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "824210d6fb52cbc3ad2545270ead6860c7311aa5450642b078da4515937b6f7a"
 "checksum stdweb 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ef5430c8e36b713e13b48a9f709cc21e046723fe44ce34587b73a830203b533e"
 "checksum stdweb 0.4.18 (registry+https://github.com/rust-lang/crates.io-index)" = "a68c0ce28cf7400ed022e18da3c4591e14e1df02c70e93573cc59921b3923aeb"

+ 1 - 0
ch1/Cargo.toml

@@ -9,3 +9,4 @@ edition = "2018"
 [dependencies]
 ggez = "*"
 carpet = { path = "../carpet" }
+specs-system-macro = { git = "https://git.infinitenegativeutility.com/getty/specs-system-macro.git" }

+ 2 - 1
ch2/Cargo.toml

@@ -10,4 +10,5 @@ edition = "2018"
 ggez = "*"
 carpet = { path = "../carpet" }
 specs = "*"
-specs-derive = "*"
+specs-derive = "*"
+specs-system-macro = { git = "https://git.infinitenegativeutility.com/getty/specs-system-macro.git" }

+ 21 - 28
ch2/src/main.rs

@@ -1,30 +1,27 @@
 #[macro_use] extern crate specs_derive;
+#[macro_use] extern crate specs_system_macro;
 
 use ggez::{Context, GameError, event::EventHandler};
 use specs::prelude::*;
 
 #[derive(Component)]
-struct Pos {
+pub struct Pos {
     x: usize,
     y: usize,
 }
 
 #[derive(Component)]
-struct Renderable {
+pub struct Renderable {
     glyph: u8,
     color: carpet::Color,
 }
 
-struct Draw;
-
-impl<'a> System<'a> for Draw {
-    type SystemData = (
-        ReadStorage<'a, Pos>,
-        ReadStorage<'a, Renderable>,
-        WriteExpect<'a, carpet::Board>,
-    );
-
-    fn run(&mut self, (pos, renderable, mut board): Self::SystemData) {
+system_impl! {
+    Draw(
+        resource mut board: carpet::Board,
+        renderable: Renderable,
+        pos: Pos,
+    ) {
         board.clear();
         for (p, r) in (&pos, &renderable).join() {
             board.set_with_color([p.x, p.y], r.glyph, r.color);
@@ -33,21 +30,17 @@ impl<'a> System<'a> for Draw {
 }
 
 #[derive(Component)]
-struct MoveLeft;
-
-impl<'a> System<'a> for MoveLeft {
-    type SystemData = (
-        ReadStorage<'a, MoveLeft>,
-        WriteStorage<'a, Pos>,
-    );
-
-    fn run(&mut self, (left, mut pos): Self::SystemData) {
-        for (_, p) in (&left, &mut pos).join() {
-            if p.x == 0 {
-                p.x = 79;
-            } else {
-                p.x -= 1;
-            }
+pub struct MoveLeft;
+
+system! {
+    Leftward (
+        _left: MoveLeft,
+        mut pos: Pos,
+    ) {
+        if pos.x == 0 {
+            pos.x = 79;
+        } else {
+            pos.x -= 1;
         }
     }
 }
@@ -65,7 +58,7 @@ impl EventHandler for State {
 
     fn update(&mut self, _ctx: &mut Context) -> Result<(), GameError> {
         Draw.run_now(&mut self.world);
-        MoveLeft.run_now(&mut self.world);
+        Leftward.run_now(&mut self.world);
         Ok(())
     }
 }