From 5ba82fd26c27b4bcc96dd4cc9eb602709eed9d4b Mon Sep 17 00:00:00 2001 From: Alex Page Date: Tue, 25 Jan 2022 23:51:49 -0500 Subject: [PATCH] Reorganize constants and sounds --- src/constants.rs | 9 +++ src/main.rs | 141 ++++++------------------------------------- src/map.rs | 12 ++-- src/sidebar.rs | 4 +- src/sound_effects.rs | 112 ++++++++++++++++++++++++++++++++++ 5 files changed, 143 insertions(+), 135 deletions(-) create mode 100644 src/constants.rs create mode 100644 src/sound_effects.rs diff --git a/src/constants.rs b/src/constants.rs new file mode 100644 index 0000000..fa78f61 --- /dev/null +++ b/src/constants.rs @@ -0,0 +1,9 @@ +pub const SIDEBAR_POS_X: i32 = 66; +pub const SIDEBAR_POS_Y: i32 = 0; + +pub const MAP_WIDTH: usize = 64; +pub const MAP_HEIGHT: usize = 23; +pub const MAP_SIZE: usize = MAP_WIDTH * MAP_HEIGHT; + +pub const MAP_X: usize = 1; +pub const MAP_Y: usize = 1; diff --git a/src/main.rs b/src/main.rs index 8bb2d00..836927a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,33 +1,25 @@ #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] use bracket_lib::prelude::*; -use map::{Map, TileType, MAP_HEIGHT, MAP_WIDTH, MAP_X, MAP_Y}; -use sound::{Sound, SoundEffect, SoundEffectSamples, SoundSystem}; +use constants::{MAP_HEIGHT, MAP_WIDTH, MAP_X, MAP_Y}; +use map::{Map, TileType}; +use sound::SoundSystem; +use sound_effects::SoundEffects; use specs::prelude::*; use specs_derive::Component; -use std::{ - array, iter, - time::{Duration, Instant}, -}; +use std::time::{Duration, Instant}; use vga_color as vga; -use crate::sound::SoundType; - +pub mod constants; mod map; mod sidebar; mod sound; +mod sound_effects; pub mod vga_color; #[derive(Default)] struct LevelNumber(u32); -struct SoundEffects { - step_sound: SoundEffectSamples, - pickup_sound: SoundEffectSamples, - bad_key_sound: SoundEffectSamples, - blocked_sound: SoundEffectSamples, -} - #[derive(Component)] struct Position { x: i32, @@ -70,31 +62,14 @@ fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World, sound_system: &m let destination_x = pos.x + delta_x; let destination_y = pos.y + delta_y; - let sound_effects = ecs.fetch::(); + let mut sound_effects = ecs.fetch_mut::(); if destination_x < 0 || (destination_x as usize) >= MAP_WIDTH || destination_y < 0 || (destination_y as usize) >= MAP_HEIGHT { - let mut rng = RandomNumberGenerator::new(); - let static_sound = sound_system.render_sound_effect(&SoundEffect { - sounds: (1..=33) - .map(|_| { - if rng.roll_dice(1, 2) > 1 { - Sound { - sound_type: SoundType::Noise(3000, 7000), - duration: Duration::from_millis(rng.range(1, 15)), - } - } else { - Sound { - sound_type: SoundType::Silence, - duration: Duration::from_millis(rng.range(0, 30)), - } - } - }) - .collect(), - }); + let static_sound = sound_effects.get_new_static_effect(sound_system); sound_system.play_sound(static_sound); player.last_moved = now; continue; @@ -105,10 +80,10 @@ fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World, sound_system: &m pos.x = destination_x; pos.y = destination_y; - let step_sound = sound_effects.step_sound.clone(); + let step_sound = sound_effects.step.clone(); sound_system.play_sound(step_sound); } else { - let blocked_sound = sound_effects.blocked_sound.clone(); + let blocked_sound = sound_effects.blocked.clone(); sound_system.play_sound(blocked_sound); } player.last_moved = now; @@ -147,7 +122,7 @@ fn player_input(gs: &mut State, ctx: &mut BTerm) { } VirtualKeyCode::S => { let sound_effects = gs.ecs.fetch::(); - let pickup_sound = sound_effects.pickup_sound.clone(); + let pickup_sound = sound_effects.pickup.clone(); gs.sound_system.play_sound(pickup_sound); } VirtualKeyCode::Escape => { @@ -155,7 +130,7 @@ fn player_input(gs: &mut State, ctx: &mut BTerm) { } _ => { let sound_effects = gs.ecs.fetch::(); - let bad_key_sound = sound_effects.bad_key_sound.clone(); + let bad_key_sound = sound_effects.bad_key.clone(); gs.sound_system.play_sound(bad_key_sound); } }, @@ -218,86 +193,18 @@ fn main() -> BError { .with_title("Kroz") .with_tile_dimensions(8, 16) .build()?; - let ss = SoundSystem::new(); - let step_sound = ss.render_sound_effect(&SoundEffect { - sounds: vec![ - Sound { - sound_type: SoundType::Noise(350, 900), - duration: Duration::from_millis(6), - }, - Sound { - sound_type: SoundType::Silence, - duration: Duration::from_millis(120), - }, - Sound { - sound_type: SoundType::Noise(150, 200), - duration: Duration::from_millis(6), - }, - ], - }); + let mut ss = SoundSystem::new(); - let pickup_sound = ss.render_sound_effect(&SoundEffect { - sounds: vec![ - Sound { - sound_type: SoundType::Noise(350, 900), - duration: Duration::from_millis(6), - }, - Sound { - sound_type: SoundType::Silence, - duration: Duration::from_millis(120), - }, - Sound { - sound_type: SoundType::Noise(1000, 2000), - duration: Duration::from_millis(20), - }, - ], - }); - - let bad_key_sound = ss.render_sound_effect(&SoundEffect { - sounds: iter::once(Sound { - sound_type: SoundType::Tone(400), - duration: Duration::from_millis(20), - }) - .chain((0..4).flat_map(|_| { - array::IntoIter::new([ - Sound { - sound_type: SoundType::Tone(100), - duration: Duration::from_millis(15), - }, - Sound { - sound_type: SoundType::Silence, - duration: Duration::from_millis(15), - }, - ]) - })) - .collect(), - }); - - let blocked_sound = ss.render_sound_effect(&SoundEffect { - sounds: (30..=60) - .rev() - .step_by(6) - .map(|x| Sound { - sound_type: SoundType::Tone(x), - duration: Duration::from_millis(18), - }) - .collect(), - }); + let sound_effects = SoundEffects::new(&ss); + ss.play_sound(sound_effects.startup.clone()); let mut gs = State { ecs: World::new(), - // sound_sender: tx, sound_system: ss, - //step_sound, }; gs.ecs.insert(LevelNumber(0)); - gs.ecs.insert(SoundEffects { - step_sound, - pickup_sound, - bad_key_sound, - blocked_sound, - }); + gs.ecs.insert(sound_effects); gs.ecs.insert(Map::new()); gs.ecs.register::(); @@ -354,20 +261,6 @@ fn main() -> BError { // let _ = gs.sound_system.play_sound(descent_effect); - let start_sounds: Vec = (30..400) - .step_by(8) - .map(|x| Sound { - sound_type: SoundType::Tone(x), - duration: Duration::from_millis(24), - }) - .collect(); - - let swoop_effect = gs.sound_system.render_sound_effect(&SoundEffect { - sounds: start_sounds, - }); - - gs.sound_system.play_sound(swoop_effect); - // let effect = gs.sound_system.render_sound_effect(SoundEffect { // sounds: vec![Sound { // sound_type: SoundType::Tone(3500), diff --git a/src/map.rs b/src/map.rs index 1906e0f..f442e03 100644 --- a/src/map.rs +++ b/src/map.rs @@ -1,16 +1,12 @@ -use crate::vga_color as vga; +use crate::{ + constants::{MAP_HEIGHT, MAP_SIZE, MAP_WIDTH, MAP_X, MAP_Y}, + vga_color as vga, +}; use bracket_lib::{ prelude::{to_cp437, BTerm, Rect, RGB}, random::RandomNumberGenerator, }; -pub const MAP_WIDTH: usize = 64; -pub const MAP_HEIGHT: usize = 23; -const MAP_SIZE: usize = MAP_WIDTH * MAP_HEIGHT; - -pub const MAP_X: usize = 1; -pub const MAP_Y: usize = 1; - #[derive(PartialEq, Copy, Clone)] pub enum TileType { Wall, diff --git a/src/sidebar.rs b/src/sidebar.rs index 1401441..77269e4 100644 --- a/src/sidebar.rs +++ b/src/sidebar.rs @@ -1,11 +1,9 @@ +use crate::constants::{SIDEBAR_POS_X, SIDEBAR_POS_Y}; use crate::vga_color as vga; use crate::{LevelNumber, Player}; use bracket_lib::prelude::*; use specs::prelude::*; -const SIDEBAR_POS_X: i32 = 66; -const SIDEBAR_POS_Y: i32 = 0; - pub fn draw(ecs: &World, ctx: &mut BTerm) { // Blue background ctx.fill_region( diff --git a/src/sound_effects.rs b/src/sound_effects.rs new file mode 100644 index 0000000..d096880 --- /dev/null +++ b/src/sound_effects.rs @@ -0,0 +1,112 @@ +use std::{array, iter, time::Duration}; + +use bracket_lib::random::RandomNumberGenerator; + +use crate::sound::{Sound, SoundEffect, SoundEffectSamples, SoundSystem, SoundType}; + +pub struct SoundEffects { + pub startup: SoundEffectSamples, + pub step: SoundEffectSamples, + pub pickup: SoundEffectSamples, + pub bad_key: SoundEffectSamples, + pub blocked: SoundEffectSamples, + rng: RandomNumberGenerator, +} + +impl SoundEffects { + pub fn new(ss: &SoundSystem) -> Self { + Self { + startup: ss.render_sound_effect(&SoundEffect { + sounds: (30..400) + .step_by(8) + .map(|x| Sound { + sound_type: SoundType::Tone(x), + duration: Duration::from_millis(24), + }) + .collect(), + }), + step: ss.render_sound_effect(&SoundEffect { + sounds: vec![ + Sound { + sound_type: SoundType::Noise(350, 900), + duration: Duration::from_millis(6), + }, + Sound { + sound_type: SoundType::Silence, + duration: Duration::from_millis(120), + }, + Sound { + sound_type: SoundType::Noise(150, 200), + duration: Duration::from_millis(6), + }, + ], + }), + pickup: ss.render_sound_effect(&SoundEffect { + sounds: vec![ + Sound { + sound_type: SoundType::Noise(350, 900), + duration: Duration::from_millis(6), + }, + Sound { + sound_type: SoundType::Silence, + duration: Duration::from_millis(120), + }, + Sound { + sound_type: SoundType::Noise(1000, 2000), + duration: Duration::from_millis(20), + }, + ], + }), + bad_key: ss.render_sound_effect(&SoundEffect { + sounds: iter::once(Sound { + sound_type: SoundType::Tone(400), + duration: Duration::from_millis(20), + }) + .chain((0..4).flat_map(|_| { + array::IntoIter::new([ + Sound { + sound_type: SoundType::Tone(100), + duration: Duration::from_millis(15), + }, + Sound { + sound_type: SoundType::Silence, + duration: Duration::from_millis(15), + }, + ]) + })) + .collect(), + }), + blocked: ss.render_sound_effect(&SoundEffect { + sounds: (30..=60) + .rev() + .step_by(6) + .map(|x| Sound { + sound_type: SoundType::Tone(x), + duration: Duration::from_millis(18), + }) + .collect(), + }), + rng: RandomNumberGenerator::new(), + } + } + + pub fn get_new_static_effect(&mut self, ss: &SoundSystem) -> SoundEffectSamples { + ss.render_sound_effect(&SoundEffect { + sounds: (1..=33) + .map(|_| { + if self.rng.roll_dice(1, 2) > 1 { + Sound { + sound_type: SoundType::Noise(3000, 7000), + duration: Duration::from_millis(self.rng.range(1, 15)), + } + } else { + Sound { + sound_type: SoundType::Silence, + duration: Duration::from_millis(self.rng.range(0, 30)), + } + } + }) + .collect(), + }) + } +}