]> git.seodisparate.com - LudumDare47_StuckInALoop/commitdiff
Impl player movement, fix player drawing
authorStephen Seo <seo.disparate@gmail.com>
Sat, 3 Oct 2020 09:13:08 +0000 (18:13 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Sat, 3 Oct 2020 09:13:08 +0000 (18:13 +0900)
src/game.rs
src/player.rs
src/scenes/mainscene.rs

index 4d6450965f28c7dd52e4373403536f30099c42bc..4a6195f4c680405e27163e616846a09f3f8d5b8d 100644 (file)
@@ -82,6 +82,10 @@ impl EventHandler for Game {
             .mouse_button_down_event(ctx, button, x, y);
     }
 
+    fn mouse_button_up_event(&mut self, ctx: &mut Context, button: MouseButton, x: f32, y: f32) {
+        self.current_scene.mouse_button_up_event(ctx, button, x, y);
+    }
+
     fn key_down_event(
         &mut self,
         ctx: &mut Context,
@@ -92,4 +96,8 @@ impl EventHandler for Game {
         self.current_scene
             .key_down_event(ctx, keycode, keymods, repeat);
     }
+
+    fn key_up_event(&mut self, ctx: &mut Context, keycode: KeyCode, keymods: KeyMods) {
+        self.current_scene.key_up_event(ctx, keycode, keymods);
+    }
 }
index 5cc4ea767e6d1ab6f464d5e3cf7ec335098b3c91..f10bf227dfebaa8a9027cc05a28c20f19cfb75b9 100644 (file)
@@ -5,6 +5,7 @@ use ggez::{Context, GameResult};
 
 const WALK_TIME: f32 = 0.34f32;
 
+#[derive(PartialEq)]
 pub enum PlayerState {
     Standing,
     Walking(bool, f32),
@@ -35,9 +36,11 @@ impl Player {
 
     pub fn set_walking(&mut self, is_walking: bool) {
         if is_walking {
-            self.state = PlayerState::Standing;
+            if self.state == PlayerState::Standing {
+                self.state = PlayerState::Walking(true, 0f32);
+            }
         } else {
-            self.state = PlayerState::Walking(true, 0f32);
+            self.state = PlayerState::Standing;
         }
     }
 
@@ -49,12 +52,13 @@ impl Player {
         let dt = delta(ctx);
         match &mut self.state {
             PlayerState::Standing => (),
-            PlayerState::Walking(left, timer) => {
+            PlayerState::Walking(ref mut left, ref mut timer) => {
                 *timer += dt.as_secs_f32();
                 if *timer >= WALK_TIME {
                     *timer -= WALK_TIME;
                     *left = !*left;
                 }
+                //println!("{}", timer);
             }
         }
         Ok(())
@@ -87,8 +91,9 @@ impl Player {
                     )?;
                 }
             }
-            PlayerState::Walking(left, _) => {
-                if *left {
+            PlayerState::Walking(step, _) => {
+                if *step {
+                    //print!("step 0, ");
                     if self.xflip {
                         graphics::draw(
                             ctx,
@@ -106,6 +111,7 @@ impl Player {
                                 .scale([-1f32, 1f32])
                                 .offset([1f32, 0f32]),
                         )?;
+                    //println!("left");
                     } else {
                         graphics::draw(
                             ctx,
@@ -121,8 +127,10 @@ impl Player {
                                 .rotation(self.rot)
                                 .color(self.color),
                         )?;
+                        //println!("right");
                     }
                 } else {
+                    //print!("step 1, ");
                     if self.xflip {
                         graphics::draw(
                             ctx,
@@ -140,6 +148,7 @@ impl Player {
                                 .scale([-1f32, 1f32])
                                 .offset([1f32, 0f32]),
                         )?;
+                    //println!("left");
                     } else {
                         graphics::draw(
                             ctx,
@@ -155,6 +164,7 @@ impl Player {
                                 .rotation(self.rot)
                                 .color(self.color),
                         )?;
+                        //println!("right");
                     }
                 }
             }
index b8b7c7ec60f9ce918017a3c3f239faf0a29972d4..532f49f824b14eb694533329d4646e88106aac0c 100644 (file)
@@ -19,6 +19,7 @@ const TEXT_RATE: f32 = 0.3f32;
 const TEXT_FAST_RATE: f32 = 0.1f32;
 const IN_POD_TEXT_WAIT_TIME: f32 = 1f32;
 const GET_OUT_OF_POD_TIME: f32 = 3f32;
+const PLAYER_MOVEMENT_SPEED: f32 = 200f32;
 
 enum State {
     InPod_InDarkness,
@@ -31,6 +32,12 @@ enum Room {
     StasisPod,
 }
 
+enum WalkingState {
+    Standing,
+    Left,
+    Right,
+}
+
 pub struct MainScene {
     font: Font,
     player: Rc<RefCell<Player>>,
@@ -49,6 +56,7 @@ pub struct MainScene {
     draw_flicker_pod: bool,
     index: usize,
     room: Room,
+    walking_state: WalkingState,
 }
 
 impl MainScene {
@@ -76,6 +84,7 @@ impl MainScene {
             draw_flicker_pod: false,
             index: 0usize,
             room: Room::StasisPod,
+            walking_state: WalkingState::Standing,
         }
     }
 
@@ -141,7 +150,34 @@ impl EventHandler for MainScene {
                 }
             }
             State::Investigate => {
-                // TODO
+                let mut player = self.player.borrow_mut();
+                match self.walking_state {
+                    WalkingState::Standing => {
+                        player.set_walking(false);
+                    }
+                    WalkingState::Left => {
+                        player.x -= dt * PLAYER_MOVEMENT_SPEED;
+                        if player.x <= 0f32 {
+                            player.x = 0f32;
+                            self.walking_state = WalkingState::Standing;
+                            player.set_walking(false);
+                        } else {
+                            player.set_walking(true);
+                            player.set_xflip(true);
+                        }
+                    }
+                    WalkingState::Right => {
+                        player.x += dt * PLAYER_MOVEMENT_SPEED;
+                        if player.x + 64f32 >= 800f32 {
+                            player.x = 800f32 - 64f32;
+                            self.walking_state = WalkingState::Standing;
+                            player.set_walking(false);
+                        } else {
+                            player.set_walking(true);
+                            player.set_xflip(false);
+                        }
+                    }
+                }
             }
         }
         self.player.borrow_mut().update(ctx)?;
@@ -220,13 +256,7 @@ impl EventHandler for MainScene {
         Ok(())
     }
 
-    fn mouse_button_down_event(
-        &mut self,
-        _ctx: &mut Context,
-        _button: MouseButton,
-        x: f32,
-        y: f32,
-    ) {
+    fn mouse_button_down_event(&mut self, ctx: &mut Context, button: MouseButton, x: f32, y: f32) {
         match self.state {
             State::InPod_InDarkness => (),
             State::InPod_WakeupText => {
@@ -238,14 +268,36 @@ impl EventHandler for MainScene {
                 }
             }
             State::GetOutOfPod => (),
-            State::Investigate => {}
+            State::Investigate => {
+                if button == MouseButton::Left {
+                    let player = self.player.borrow();
+                    if player.x > x {
+                        self.walking_state = WalkingState::Left;
+                    } else if player.x + 64f32 < x {
+                        self.walking_state = WalkingState::Right;
+                    }
+                }
+            }
+        }
+    }
+
+    fn mouse_button_up_event(&mut self, ctx: &mut Context, button: MouseButton, x: f32, y: f32) {
+        match self.state {
+            State::InPod_InDarkness => (),
+            State::InPod_WakeupText => (),
+            State::GetOutOfPod => (),
+            State::Investigate => {
+                if button == MouseButton::Left {
+                    self.walking_state = WalkingState::Standing;
+                }
+            }
         }
     }
 
     fn key_down_event(
         &mut self,
-        _ctx: &mut Context,
-        _keycode: KeyCode,
+        ctx: &mut Context,
+        keycode: KeyCode,
         _keymods: KeyMods,
         _repeat: bool,
     ) {
@@ -261,7 +313,32 @@ impl EventHandler for MainScene {
                 }
             }
             State::GetOutOfPod => (),
-            State::Investigate => {}
+            State::Investigate => {
+                if keycode == KeyCode::A || keycode == KeyCode::Left {
+                    if self.player.borrow().x > 0f32 {
+                        self.walking_state = WalkingState::Left;
+                    }
+                } else if keycode == KeyCode::D || keycode == KeyCode::Right {
+                    if self.player.borrow().x + 64f32 < 800f32 {
+                        self.walking_state = WalkingState::Right;
+                    }
+                }
+            }
+        }
+    }
+
+    fn key_up_event(&mut self, _ctx: &mut Context, keycode: KeyCode, _keymods: KeyMods) {
+        match self.state {
+            State::InPod_InDarkness => (),
+            State::InPod_WakeupText => (),
+            State::GetOutOfPod => (),
+            State::Investigate => {
+                if keycode == KeyCode::A || keycode == KeyCode::Left {
+                    self.walking_state = WalkingState::Standing;
+                } else if keycode == KeyCode::D || keycode == KeyCode::Right {
+                    self.walking_state = WalkingState::Standing;
+                }
+            }
         }
     }
 }