Browse Source

Clean that up a little bit

Getty Ritter 4 years ago
parent
commit
99b24c2e2d
1 changed files with 9 additions and 2 deletions
  1. 9 2
      ch2/src/main.rs

+ 9 - 2
ch2/src/main.rs

@@ -24,6 +24,13 @@ pub struct MoveLeft;
 #[derive(Component)]
 pub struct Player;
 
+impl Player {
+    fn get_entity(world: &mut specs::World) -> Entity {
+        let storage = (&world.read_component::<Player>(), &world.entities());
+        storage.join().next().expect("No entities tagged as Player").1
+    }
+}
+
 #[derive(Component)]
 pub struct Motion {
     down: i8,
@@ -31,8 +38,8 @@ pub struct Motion {
 }
 
 impl Motion {
-    fn move_player(&mut specs::World, down: i8, right: i8) {
-        let player = (&world.read_component::<Player>(), &world.entities()).join().next().unwrap().1;
+    fn move_player(world: &mut specs::World, down: i8, right: i8) {
+        let player = Player::get_entity(world);
         world.write_component::<Motion>().insert(player, Motion { down, right }).unwrap();
     }
 }