game.rs (990B)
1 use std::time::Duration; 2 3 use common::game::{ActionKind, GameState, Player}; 4 use macroquad::prelude::*; 5 6 pub(crate) fn handle_movement_input( 7 local_player: &mut Player, 8 dt: &Duration, 9 ) -> Option<ActionKind> { 10 let velocity_x = if is_key_down(KeyCode::A) || is_key_down(KeyCode::Left) { 11 -GameState::MAX_PLAYER_SPEED_PX_S 12 } else if is_key_down(KeyCode::D) || is_key_down(KeyCode::Right) { 13 GameState::MAX_PLAYER_SPEED_PX_S 14 } else { 15 0. 16 }; 17 let velocity_y = if is_key_down(KeyCode::W) || is_key_down(KeyCode::Up) { 18 -GameState::MAX_PLAYER_SPEED_PX_S 19 } else if is_key_down(KeyCode::S) || is_key_down(KeyCode::Down) { 20 GameState::MAX_PLAYER_SPEED_PX_S 21 } else { 22 0. 23 }; 24 if velocity_x != 0. || velocity_y != 0. { 25 Some(ActionKind::Move { 26 new_position: local_player.position() 27 + Vec2::new(velocity_x, velocity_y) * dt.as_secs_f32(), 28 }) 29 } else { 30 None 31 } 32 }