diff options
| author | Freya Murphy <freya@freyacat.org> | 2025-11-17 10:02:56 -0500 |
|---|---|---|
| committer | Freya Murphy <freya@freyacat.org> | 2025-11-17 10:28:44 -0500 |
| commit | baae7dbc38ad4e131c107d9f0f638530ac250e2e (patch) | |
| tree | cb6c6dbab81424999617eef09885e798ee2c21c9 /game | |
| parent | Feedback and grade for Checkpoint (diff) | |
| download | DungeonCrawl-baae7dbc38ad4e131c107d9f0f638530ac250e2e.tar.gz DungeonCrawl-baae7dbc38ad4e131c107d9f0f638530ac250e2e.tar.bz2 DungeonCrawl-baae7dbc38ad4e131c107d9f0f638530ac250e2e.zip | |
wasm support!
Diffstat (limited to 'game')
| -rw-r--r-- | game/Cargo.toml | 5 | ||||
| -rw-r--r-- | game/src/lib.rs | 103 | ||||
| -rw-r--r-- | game/src/main.rs | 159 | ||||
| -rw-r--r-- | game/www/index.html | 44 |
4 files changed, 190 insertions, 121 deletions
diff --git a/game/Cargo.toml b/game/Cargo.toml index 5cdf4af..af4a533 100644 --- a/game/Cargo.toml +++ b/game/Cargo.toml @@ -8,7 +8,6 @@ publish.workspace = true rust-version.workspace = true [dependencies] -argh.workspace = true dungeon.workspace = true graphics.workspace = true @@ -21,3 +20,7 @@ x11 = ["graphics/x11"] wayland = ["graphics/wayland"] sdl = ["graphics/sdl"] static = ["graphics/static"] + +# desktop dependencies +[target.'cfg(not(target_arch = "wasm32"))'.dependencies] +argh.workspace = true diff --git a/game/src/lib.rs b/game/src/lib.rs new file mode 100644 index 0000000..77942b4 --- /dev/null +++ b/game/src/lib.rs @@ -0,0 +1,103 @@ +use dungeon::{Dungeon, UpdateResult, player_input::PlayerInput, pos::Direction}; +use graphics::{Key, Window}; + +pub struct Game { + window: Window, + dungeon: Dungeon, + // to ensure the most recently-pressed direction key is used: + current_dir: Option<(Direction, Key)>, +} + +impl Game { + pub fn new(window: Window, seed: Option<u64>) -> Self { + let dungeon = match seed { + Some(s) => Dungeon::new(s), + None => Dungeon::random(), + }; + + Self { + window, + dungeon, + current_dir: None, + } + } + + fn player_dir(&mut self) -> Option<Direction> { + const MOVE_KEYS: [(Direction, [Key; 2]); 4] = [ + (Direction::North, [Key::Up, Key::W]), + (Direction::West, [Key::Left, Key::A]), + (Direction::South, [Key::Down, Key::S]), + (Direction::East, [Key::Right, 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 + } + + pub fn run(&mut self) { + // Main game loop + while self.window.is_open() { + // Handle debug keys + if self.window.is_key_pressed(Key::F3) { + self.window.toggle_debug(); + } + if self.window.is_key_pressed(Key::F4) { + self.dungeon + .msg + .set_message("Lorem ipsum dolor sit amet consectetur adipiscing elit"); + } + + let inputs = PlayerInput { + direction: self.player_dir(), + interact: self.window.is_key_pressed(Key::Return), + }; + + // Update game state + let result = self + .dungeon + .update(inputs, self.window.delta_time().as_secs_f32()); + match result { + UpdateResult::EntityMovement => {} + UpdateResult::NextFloor => { + // TODO: stairs audio + } + UpdateResult::MessageUpdated(changed) => { + if changed { + self.window.audio().speak.play(); + } + } + } + + // Update on screen message + if self.dungeon.msg.update(inputs) { + self.window.audio().speak.play(); + } + + // Draw a single frame + self.window.draw_frame(&self.dungeon); + } + } +} diff --git a/game/src/main.rs b/game/src/main.rs index 5446b5f..a44f317 100644 --- a/game/src/main.rs +++ b/game/src/main.rs @@ -1,130 +1,49 @@ -use argh::FromArgs; -use dungeon::{Dungeon, UpdateResult, player_input::PlayerInput, pos::Direction}; -use graphics::{Key, Window, WindowBuilder}; +#[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))] +mod arch { + use argh::FromArgs; + use game::Game; + use graphics::WindowBuilder; -struct Game { - window: Window, - dungeon: Dungeon, - // to ensure the most recently-pressed direction key is used: - current_dir: Option<(Direction, Key)>, -} - -impl Game { - fn new(window: Window, seed: Option<u64>) -> Self { - let dungeon = match seed { - Some(s) => Dungeon::new(s), - None => Dungeon::random(), - }; - - Self { - window, - dungeon, - current_dir: None, - } + /// Play a dungeon crawl game + #[derive(FromArgs)] + struct Args { + /// enable vsync + #[argh(switch)] + vsync: bool, + /// enable verbose logging + #[argh(switch, short = 'v')] + verbose: bool, + /// set the map seed + #[argh(option)] + seed: Option<u64>, } - fn player_dir(&mut self) -> Option<Direction> { - const MOVE_KEYS: [(Direction, [Key; 2]); 4] = [ - (Direction::North, [Key::Up, Key::W]), - (Direction::West, [Key::Left, Key::A]), - (Direction::South, [Key::Down, Key::S]), - (Direction::East, [Key::Right, 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 + pub fn main() -> graphics::Result<()> { + // Parse arguments + let args: Args = argh::from_env(); + // Load the window + let window = WindowBuilder::new() + .vsync(args.vsync) + .verbose(args.verbose) + .build()?; + Game::new(window, args.seed).run(); + Ok(()) } +} - fn run(&mut self) { - // Main game loop - while self.window.is_open() { - // Handle debug keys - if self.window.is_key_pressed(Key::F3) { - self.window.toggle_debug(); - } - if self.window.is_key_pressed(Key::F4) { - self.dungeon - .msg - .set_message("Lorem ipsum dolor sit amet consectetur adipiscing elit"); - } - - let inputs = PlayerInput { - direction: self.player_dir(), - interact: self.window.is_key_pressed(Key::Return), - }; - - // Update game state - let result = self - .dungeon - .update(inputs, self.window.delta_time().as_secs_f32()); - match result { - UpdateResult::EntityMovement => {} - UpdateResult::NextFloor => { - // TODO: stairs audio - } - UpdateResult::MessageUpdated(changed) => { - if changed { - self.window.audio().speak.play(); - } - } - } - - // Update on screen message - if self.dungeon.msg.update(inputs) { - self.window.audio().speak.play(); - } +#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))] +mod arch { + use game::Game; + use graphics::WindowBuilder; - // Draw a single frame - self.window.draw_frame(&self.dungeon); - } + pub fn main() -> graphics::Result<()> { + // Load the window + let window = WindowBuilder::new().build()?; + Game::new(window, None).run(); + Ok(()) } } -/// Play a dungeon crawl game -#[derive(FromArgs)] -struct Args { - /// enable vsync - #[argh(switch)] - vsync: bool, - /// enable verbose logging - #[argh(switch, short = 'v')] - verbose: bool, - /// set the map seed - #[argh(option)] - seed: Option<u64>, -} - -fn main() -> graphics::Result<()> { - // Parse arguments - let args: Args = argh::from_env(); - // Load the window - let window = WindowBuilder::new() - .vsync(args.vsync) - .verbose(args.verbose) - .build()?; - Game::new(window, args.seed).run(); - Ok(()) +pub fn main() -> graphics::Result<()> { + arch::main() } diff --git a/game/www/index.html b/game/www/index.html new file mode 100644 index 0000000..2181c25 --- /dev/null +++ b/game/www/index.html @@ -0,0 +1,44 @@ +<!doctype html> +<html lang="en-us"> + <head> + <meta charset="utf-8"> + <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> + <meta name="viewport" content="width=device-width"> + <meta property="og:title" content="Dungeon Crawl"> + <meta property="og:description" content="Dungeon Crawl game!"> + <title>Dungeon Crawl</title> + <style> + * { + margin: 0; + padding: none; + border: none; + } + + canvas { + width: 100%; + height: 100%; + display: block; + background: black; + } + </style> + </head> + <body> + <canvas id="canvas" oncontextmenu="event.preventDefault()" tabindex=-1></canvas> + <script type='text/javascript'> + var Module = { + canvas: (function () { + var canvas = document.querySelector('#canvas'); + canvas.addEventListener("webglcontextlost", function (e) { + alert('WebGL context lost. You will need to reload the page.'); + e.preventDefault(); + }, false); + return canvas; + })(), + print: console.log, + printErr: console.error, + setStatus: console.debug, + }; + </script> + <script src="game.js"></script> + </body> +</html> |