summaryrefslogtreecommitdiff
path: root/game/src/lib.rs
diff options
context:
space:
mode:
authorFreya Murphy <freya@freyacat.org>2025-11-24 14:30:30 -0500
committerFreya Murphy <freya@freyacat.org>2025-11-24 14:30:30 -0500
commit4601aa8a74f3f99e8cbac0ccade955bb242db910 (patch)
tree10cb11ceeb4467133bed03d157b2074d744d5486 /game/src/lib.rs
parentaudio: disable music debug (diff)
downloadDungeonCrawl-4601aa8a74f3f99e8cbac0ccade955bb242db910.tar.gz
DungeonCrawl-4601aa8a74f3f99e8cbac0ccade955bb242db910.tar.bz2
DungeonCrawl-4601aa8a74f3f99e8cbac0ccade955bb242db910.zip
audio: move data out of crate
Diffstat (limited to 'game/src/lib.rs')
-rw-r--r--game/src/lib.rs64
1 files changed, 52 insertions, 12 deletions
diff --git a/game/src/lib.rs b/game/src/lib.rs
index 436d488..6e09a37 100644
--- a/game/src/lib.rs
+++ b/game/src/lib.rs
@@ -1,28 +1,53 @@
+use std::rc::Rc;
+
use dungeon::{
Dungeon, UpdateResult, entity::Item, map::Tile, player::PLAYER_INVENTORY_SIZE_USIZE,
player_input::PlayerInput, pos::Direction,
};
use graphics::{Key, Window};
+use crate::music::Music;
+
+mod music;
+
+macro_rules! play_sound {
+ ($self:expr, $sound:expr) => {
+ $self.window.audio().schedule(
+ Rc::clone(&$sound.track),
+ $sound.priority,
+ $sound.looping,
+ );
+ };
+}
+
+/// The `Error` type used within this crate
+pub type Error = Box<dyn std::error::Error>;
+
+/// The `Result` type used witin this crate
+pub type Result<T> = std::result::Result<T, crate::Error>;
+
pub struct Game {
window: Window,
dungeon: Dungeon,
+ music: Music,
// 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 {
+ pub fn new(window: Window, seed: Option<u64>) -> Result<Self> {
+ let music = Music::load()?;
let dungeon = match seed {
Some(s) => Dungeon::new(s),
None => Dungeon::random(),
};
-
- Self {
+ let game = Self {
window,
dungeon,
+ music,
current_dir: None,
- }
+ };
+ Ok(game)
}
fn player_dir(&mut self) -> Option<Direction> {
@@ -79,9 +104,7 @@ impl Game {
}
pub fn run(&mut self) {
- //// debug music
- //let track = self.window.audio().data.test.clone();
- //self.window.audio().schedule(track, 1, true);
+ play_sound!(self, &self.music.background);
// Main game loop
while self.window.is_open() {
@@ -100,19 +123,36 @@ impl Game {
.dungeon
.update(inputs, self.window.delta_time().as_secs_f32());
match result {
- UpdateResult::Default(_action) => {
- // TODO: make noises for player actions
+ UpdateResult::Default(action) => {
+ // items
+ if action.bomb {
+ play_sound!(self, &self.music.use_bomb);
+ } else if action.potion {
+ play_sound!(self, &self.music.use_potion);
+ } else if action.drop_item {
+ play_sound!(self, &self.music.drop_item);
+ } else if action.pickup_item {
+ play_sound!(self, &self.music.pickup_item);
+ }
+
+ // actions
+ if action.attack {
+ play_sound!(self, &self.music.attack);
+ } else if action.walk {
+ // TODO: do we want walking sounds? (were a ghost)
+ }
}
UpdateResult::NextFloor => {
- // TODO: stairs audio
+ play_sound!(self, &self.music.discover);
}
UpdateResult::MessageUpdated(changed) => {
if changed {
- //self.window.audio().speak.play();
+ play_sound!(self, &self.music.speak);
}
}
UpdateResult::GameOver => {
- // TODO: player game over music, very sad
+ self.window.audio().clear();
+ play_sound!(self, &self.music.game_over);
}
UpdateResult::Nothing => {}
}