From 29f435d478e1195ae95fca4360178f044ed0df00 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Sat, 3 Oct 2020 18:13:08 +0900 Subject: [PATCH] Impl player movement, fix player drawing --- src/game.rs | 8 ++++ src/player.rs | 20 ++++++-- src/scenes/mainscene.rs | 101 +++++++++++++++++++++++++++++++++++----- 3 files changed, 112 insertions(+), 17 deletions(-) diff --git a/src/game.rs b/src/game.rs index 4d64509..4a6195f 100644 --- a/src/game.rs +++ b/src/game.rs @@ -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); + } } diff --git a/src/player.rs b/src/player.rs index 5cc4ea7..f10bf22 100644 --- a/src/player.rs +++ b/src/player.rs @@ -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"); } } } diff --git a/src/scenes/mainscene.rs b/src/scenes/mainscene.rs index b8b7c7e..532f49f 100644 --- a/src/scenes/mainscene.rs +++ b/src/scenes/mainscene.rs @@ -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>, @@ -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; + } + } } } } -- 2.49.0