diff options
| author | Yusuf Elsharawy <yusufse-2004@hotmail.com> | 2025-11-10 20:05:46 +0000 |
|---|---|---|
| committer | Yusuf Elsharawy <yusufse-2004@hotmail.com> | 2025-11-10 20:10:38 +0000 |
| commit | b1c7a57e72ab7359191249d76ec84d6cae524e2a (patch) | |
| tree | c533911f87b1641d893de5ab68ed05c2558b1cba /game/src/main.rs | |
| parent | graphics: decouple sdl & wayland (diff) | |
| download | DungeonCrawl-b1c7a57e72ab7359191249d76ec84d6cae524e2a.tar.gz DungeonCrawl-b1c7a57e72ab7359191249d76ec84d6cae524e2a.tar.bz2 DungeonCrawl-b1c7a57e72ab7359191249d76ec84d6cae524e2a.zip | |
Refactored some code, implemented player movement
Diffstat (limited to 'game/src/main.rs')
| -rw-r--r-- | game/src/main.rs | 103 |
1 files changed, 78 insertions, 25 deletions
diff --git a/game/src/main.rs b/game/src/main.rs index d96273e..aabe38a 100644 --- a/game/src/main.rs +++ b/game/src/main.rs @@ -2,6 +2,82 @@ use clap::Parser; use dungeon::*; use graphics::*; +struct Game { + window: Window, + dungeon: Dungeon, + // to ensure the most recently-pressed direction key is used: + current_dir: Option<(Direction, KeyCode)>, +} + +impl Game { + fn new(window: Window) -> Self { + // Initial game state + let dungeon = Dungeon::new(); + Ok(Self { + window, + dungeon, + current_dir: None, + }) + } + + fn player_dir(&mut self) -> Option<Direction> { + const MOVE_KEYS: [(Direction, [KeyCode; 2]); 4] = [ + (Direction::North, [KeyCode::KEY_UP, KeyCode::KEY_W]), + (Direction::West, [KeyCode::KEY_LEFT, KeyCode::KEY_A]), + (Direction::South, [KeyCode::KEY_DOWN, KeyCode::KEY_S]), + (Direction::East, [KeyCode::KEY_RIGHT, KeyCode::KEY_D]), + ]; + + // if a key was just pressed, use it for the new direction to move in + for (dir, keys) in MOVE_KEYS { + for key in keys { + if self.window.is_key_pressed(key) { + self.current_dir = Some((dir, key)); + return Some(dir); + } + } + } + // otherwise, use existing direction, so long as the key is still down + match self.current_dir { + Some((dir, key)) if self.window.is_key_down(key) => return Some(dir), + _ => self.current_dir = None, + } + // otherwise, use any key that is already down + for (dir, keys) in MOVE_KEYS { + for key in keys { + if self.window.is_key_down(key) { + self.current_dir = Some((dir, key)); + return Some(dir); + } + } + } + // otherwise, no direction key is pressed, so return None + None + } + + fn run(&mut self) { + // Main game loop + while self.window.is_open() { + // Handle keyboard input + if self.window.is_key_pressed(KeyCode::KEY_F3) { + self.window.toggle_debug(); + } + + let inputs = PlayerInput { + direction: self.player_dir(), + }; + + // Update game state + self.dungeon.update(inputs, self.window.delta_time()); + + // Draw a single frame + self.window.draw_frame(&self.dungeon); + } + Ok(()) + } +} + + #[derive(Parser)] struct Args { /// Enable vsync @@ -16,33 +92,10 @@ fn main() -> Result<()> { // Parse arguments let args = Args::parse(); // Load the window - let mut window = WindowBuilder::new() + let window = WindowBuilder::new() .vsync(args.vsync) .verbose(args.verbose) .build()?; - // Initial game state - let mut dungeon = Dungeon::new(); - - // Main game loop - while window.is_open() { - // TODO update game state - - // Handle keyboard input - if window.is_key_pressed(KeyCode::KEY_F3) { - window.toggle_debug(); - } - - for enemy in dungeon.enemies.iter_mut() { - enemy.handle_movement( - dungeon.player.entity.pos, - &mut dungeon.floor, - window.delta_time(), - ); - } - - // Draw a single frame - window.draw_frame(&dungeon); - } - + Game::new(window).run(); Ok(()) } |