Reorganize constants and sounds

This commit is contained in:
Alex Page 2022-01-25 23:51:49 -05:00
parent 5284b33808
commit 5ba82fd26c
5 changed files with 143 additions and 135 deletions

9
src/constants.rs Normal file
View file

@ -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;

View file

@ -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::<SoundEffects>();
let mut sound_effects = ecs.fetch_mut::<SoundEffects>();
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::<SoundEffects>();
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::<SoundEffects>();
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::<Position>();
@ -354,20 +261,6 @@ fn main() -> BError {
// let _ = gs.sound_system.play_sound(descent_effect);
let start_sounds: Vec<Sound> = (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),

View file

@ -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,

View file

@ -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(

112
src/sound_effects.rs Normal file
View file

@ -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(),
})
}
}