Browse Source

Move the new components to the components module

Getty Ritter 4 years ago
parent
commit
3f5e9a82df
2 changed files with 24 additions and 24 deletions
  1. 21 0
      chapter-07-damage/src/components.rs
  2. 3 24
      chapter-07-damage/src/player.rs

+ 21 - 0
chapter-07-damage/src/components.rs

@@ -55,3 +55,24 @@ pub struct WantsToMelee {
 pub struct SufferDamage {
     pub amount: i32,
 }
+
+#[derive(Component, Copy, Clone, Debug)]
+pub struct MoveEvent {
+    pub destination: usize,
+    pub tgt_x: i32,
+    pub tgt_y: i32,
+}
+
+
+/// This type represents all the "low-level" actions we have available
+/// to us, corresponding directly to the set of actions we have bound
+/// on the keys
+#[derive(Component)]
+pub enum InputEvent {
+    // attempt to move the player some amount in the given direction
+    // (delta_x and delta_y should be -1, 0, or 1)
+    PlayerMovement {
+        delta_x: i32,
+        delta_y: i32,
+    },
+}

+ 3 - 24
chapter-07-damage/src/player.rs

@@ -1,7 +1,7 @@
 extern crate rltk;
 use rltk::{Point, Rltk, VirtualKeyCode};
 extern crate specs;
-use super::{CombatStats, Map, Position, RunState, State, Viewshed, WantsToMelee};
+use super::{CombatStats, Map, Position, MoveEvent, RunState, State, Viewshed, WantsToMelee, InputEvent};
 use specs::prelude::*;
 
 
@@ -15,13 +15,6 @@ fn clamp<T: PartialOrd>(low: T, high: T, val: T) -> T {
     }
 }
 
-#[derive(Component, Copy, Clone)]
-pub struct MoveEvent {
-    destination: usize,
-    tgt_x: i32,
-    tgt_y: i32,
-}
-
 pub struct HandleMoveEvent;
 
 impl<'a> System<'a> for HandleMoveEvent {
@@ -34,8 +27,8 @@ impl<'a> System<'a> for HandleMoveEvent {
     );
 
     fn run(&mut self, (mut move_events, mut pos, mut viewshed, mut point, map): Self::SystemData) {
-        for (event, pos, viewshed) in (&move_events, &mut pos, &mut viewshed).join() {
-            let MoveEvent { destination, tgt_x, tgt_y } = *event;
+        for (&event, pos, viewshed) in (&move_events, &mut pos, &mut viewshed).join() {
+            let MoveEvent { destination, tgt_x, tgt_y } = event;
             if !map.blocked[destination] {
                 viewshed.dirty = true;
                 pos.x = tgt_x;
@@ -85,20 +78,6 @@ impl<'a> System<'a> for HandleInputEvent {
     }
 }
 
-
-/// This type represents all the "low-level" actions we have available
-/// to us, corresponding directly to the set of actions we have bound
-/// on the keys
-#[derive(Component)]
-pub enum InputEvent {
-    // attempt to move the player some amount in the given direction
-    // (delta_x and delta_y should be -1, 0, or 1)
-    PlayerMovement {
-        delta_x: i32,
-        delta_y: i32,
-    },
-}
-
 pub fn player_input(gs: &mut State, ctx: &mut Rltk) -> RunState {
     let player = *gs.ecs.fetch::<Entity>();
     let mut input = gs.ecs.write_storage::<InputEvent>();