Browse Source

Move some values into specs-based resources

Getty Ritter 5 years ago
parent
commit
edfcb36e8c
5 changed files with 49 additions and 37 deletions
  1. 2 3
      src/game.rs
  2. 5 8
      src/main.rs
  3. 26 0
      src/res.rs
  4. 6 15
      src/sys/drawing.rs
  5. 10 11
      src/sys/input.rs

+ 2 - 3
src/game.rs

@@ -1,9 +1,8 @@
-use sdl2::keyboard as sdl;
+// use sdl2::keyboard as sdl;
 
 /// The shared values that the game state needs, mostly contained in
 /// the specs ECS world
 pub struct MyGame {
     pub world: specs::World,
-    pub sprites: ggez::graphics::spritebatch::SpriteBatch,
-    pub keys: std::collections::HashSet<sdl::Keycode>,
+    // pub keys: std::collections::HashSet<sdl::Keycode>,
 }

+ 5 - 8
src/main.rs

@@ -39,7 +39,7 @@ impl EventHandler for MyGame {
         if keycode == sdl::Keycode::Escape {
             ctx.quit().expect("Should never fail");
         }
-        self.keys.insert(keycode);
+        self.world.write_resource::<res::KeySet>().insert(keycode);
     }
 
     fn key_up_event(
@@ -49,7 +49,7 @@ impl EventHandler for MyGame {
         _keymod: sdl::Mod,
         _repeat: bool,
     ) {
-        self.keys.remove(&keycode);
+        self.world.write_resource::<res::KeySet>().remove(&keycode);
     }
 }
 
@@ -76,13 +76,10 @@ fn main() -> Result<(), ggez::error::GameError> {
     let image = ggez::graphics::Image::new(&mut ctx, "/spritesheet.png")?;
     let mut sprites = ggez::graphics::spritebatch::SpriteBatch::new(image);
     sprites.set_filter(ggez::graphics::FilterMode::Nearest);
-    let keys = std::collections::HashSet::new();
+    world.add_resource(sprites);
+    world.add_resource(res::KeySet::new());
 
-    let mut my_game = MyGame {
-        world,
-        sprites,
-        keys,
-    };
+    let mut my_game = MyGame { world };
 
     event::run(&mut ctx, &mut my_game)
 }

+ 26 - 0
src/res.rs

@@ -4,6 +4,32 @@ use crate::components::*;
 use specs::world::Builder;
 use std::path::Path;
 
+use sdl2::keyboard as sdl;
+
+#[derive(Debug, Default)]
+pub struct KeySet {
+    keys: std::collections::HashSet<sdl::Keycode>,
+}
+
+impl KeySet {
+    pub fn new() -> KeySet {
+        KeySet {
+            keys: std::collections::HashSet::new(),
+        }
+    }
+
+    pub fn contains(&self, kc: &sdl::Keycode) -> bool {
+        self.keys.contains(kc)
+    }
+
+    pub fn insert(&mut self, kc: sdl::Keycode) {
+        self.keys.insert(kc);
+    }
+
+    pub fn remove(&mut self, kc: &sdl::Keycode) {
+        self.keys.remove(kc);
+    }
+}
 
 #[derive(Debug, Copy, Clone)]
 enum DrawLayer {

+ 6 - 15
src/sys/drawing.rs

@@ -13,7 +13,7 @@ use specs::RunNow;
 /// in.
 pub struct Draw<'t, Phase> {
     pub ctx: &'t mut Context,
-    pub sprites: &'t mut SpriteBatch,
+    //pub sprites: &'t mut SpriteBatch,
     pub _phase: Phase,
 }
 
@@ -22,9 +22,10 @@ impl<'a, 't, Phase : specs::Component> specs::System<'a> for Draw<'t, Phase> {
         specs::ReadStorage<'a, Position>,
         specs::ReadStorage<'a, Sprite>,
         specs::ReadStorage<'a, Phase>,
+        specs::WriteExpect<'a, SpriteBatch>,
     );
 
-    fn run(&mut self, (positions, sprites, phase): Self::SystemData) {
+    fn run(&mut self, (positions, sprites, phase, mut batch): Self::SystemData) {
         use specs::Join;
 
         for (pos, spr, _) in (&positions, &sprites, &phase).join() {
@@ -34,13 +35,13 @@ impl<'a, 't, Phase : specs::Component> specs::System<'a> for Draw<'t, Phase> {
                 scale: ggez::nalgebra::Point2::new(consts::SCALE, consts::SCALE),
                 ..Default::default()
             };
-            self.sprites.add(param);
+            batch.add(param);
             graphics::draw(
                 self.ctx,
-                self.sprites,
+                &*batch,
                 ggez::nalgebra::Point2::new(0.0, 0.0),
                 0.0).unwrap();
-            self.sprites.clear();
+            batch.clear();
         }
     }
 }
@@ -52,27 +53,17 @@ pub fn systems(game: &mut MyGame, ctx: &mut Context) -> ggez::GameResult<()> {
 
     Draw {
         ctx,
-        sprites: &mut game.sprites,
         _phase: components::Background,
     }.run_now(&game.world.res);
     Draw {
         ctx,
-        sprites: &mut game.sprites,
         _phase: components::Foreground,
     }.run_now(&game.world.res);
     Draw {
         ctx,
-        sprites: &mut game.sprites,
         _phase: components::Decoration,
     }.run_now(&game.world.res);
 
-    graphics::draw(
-        ctx,
-        &game.sprites,
-        ggez::nalgebra::Point2::new(0.0, 0.0),
-        0.0
-    )?;
-    game.sprites.clear();
     graphics::present(ctx);
     Ok(())
 }

+ 10 - 11
src/sys/input.rs

@@ -1,43 +1,42 @@
 use crate::components::{Controlled, Velocity};
 use crate::game::MyGame;
+use crate::res::KeySet;
 
 use sdl2::keyboard as sdl;
 use specs::RunNow;
 
-pub struct Move<'t> {
-    keys: &'t std::collections::HashSet<sdl::Keycode>,
-}
+pub struct Move;
 
-impl<'a, 't> specs::System<'a> for Move<'t> {
+impl<'a> specs::System<'a> for Move {
     type SystemData = (
         specs::ReadStorage<'a, Controlled>,
         specs::WriteStorage<'a, Velocity>,
+        specs::Read<'a, KeySet>,
     );
 
-    fn run(&mut self, (movable, mut velocity): Self::SystemData) {
+    fn run(&mut self, (movable, mut velocity, keys): Self::SystemData) {
         use specs::Join;
 
         for (_, vel) in (&movable, &mut velocity).join() {
             vel.dx = 0.0;
             vel.dy = 0.0;
 
-            if self.keys.contains(&sdl::Keycode::W) {
+            if keys.contains(&sdl::Keycode::W) {
                 vel.dy -= 2.0;
             }
-            if self.keys.contains(&sdl::Keycode::A) {
+            if keys.contains(&sdl::Keycode::A) {
                 vel.dx -= 2.0;
             }
-            if self.keys.contains(&sdl::Keycode::S) {
+            if keys.contains(&sdl::Keycode::S) {
                 vel.dy += 2.0;
             }
-            if self.keys.contains(&sdl::Keycode::D) {
+            if keys.contains(&sdl::Keycode::D) {
                 vel.dx += 2.0;
             }
         }
     }
 }
 
-
 pub fn systems(game: &mut MyGame) {
-    Move { keys: &game.keys }.run_now(&game.world.res);
+    Move.run_now(&game.world.res);
 }