Compare commits

...

4 commits

Author SHA1 Message Date
e5d820eded Abandon map_indexing_system
All checks were successful
continuous-integration/drone/push Build is passing
2022-01-31 19:10:24 -05:00
1c54755039 Skip out-of-bounds whip frames 2022-01-31 18:49:59 -05:00
770793ea68 Ensure hit beep on last frame of whip 2022-01-31 18:15:04 -05:00
9219b6c475 Use FunDSP crate for beepboops 2022-01-31 18:10:39 -05:00
11 changed files with 270 additions and 164 deletions

139
Cargo.lock generated
View file

@ -778,12 +778,37 @@ version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7"
[[package]]
name = "fundsp"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e44dcde4aedad5675c131b5114d431b4d7c739dc32dd599bf0460f36413d247e"
dependencies = [
"generic-array",
"lazy_static",
"num-complex",
"numeric-array",
"rsor",
"rustfft",
"tinyvec",
]
[[package]]
name = "futures"
version = "0.1.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3a471a38ef8ed83cd6e40aa59c1ffe17db6855c18e3604d9c4ed8c08ebc28678"
[[package]]
name = "generic-array"
version = "0.14.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd48d33ec7f05fbfa152300fdad764757cbded343c1aa1cff2fbaf4134851803"
dependencies = [
"typenum",
"version_check",
]
[[package]]
name = "getrandom"
version = "0.2.3"
@ -1024,11 +1049,11 @@ version = "0.1.0"
dependencies = [
"bracket-lib",
"cpal",
"fundsp",
"oddio",
"rand",
"specs",
"specs-derive",
"spin_sleep",
"typenum",
"winres",
]
@ -1413,6 +1438,15 @@ dependencies = [
"winapi 0.3.9",
]
[[package]]
name = "num-complex"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26873667bbbb7c5182d4a37c1add32cdf09f841af72da53318fdb81543c15085"
dependencies = [
"num-traits",
]
[[package]]
name = "num-derive"
version = "0.3.3"
@ -1520,6 +1554,16 @@ dependencies = [
"syn",
]
[[package]]
name = "numeric-array"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2c59a9b227913a685ed84aa0de5ded58274e8bcfa55760faae07bc82aee64ccf"
dependencies = [
"generic-array",
"num-traits",
]
[[package]]
name = "objc"
version = "0.2.7"
@ -1650,10 +1694,13 @@ dependencies = [
]
[[package]]
name = "ppv-lite86"
version = "0.2.15"
name = "primal-check"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed0cfbc8191465bed66e1718596ee0b0b35d5ee1f41c5df2189d0fe8bde535ba"
checksum = "01419cee72c1a1ca944554e23d83e483e1bccf378753344e881de28b5487511d"
dependencies = [
"num-integer",
]
[[package]]
name = "proc-macro-crate"
@ -1698,19 +1745,6 @@ version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2e7573632e6454cf6b99d7aac4ccca54be06da05aca2ef7423d22d27d4d4bcd8"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
"rand_hc",
]
[[package]]
name = "rand_chacha"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core",
]
@ -1719,18 +1753,6 @@ name = "rand_core"
version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7"
dependencies = [
"getrandom",
]
[[package]]
name = "rand_hc"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d51e9f596de227fda2ea6c84607f5558e196eeaf43c986b724ba4fb8fdf497e7"
dependencies = [
"rand_core",
]
[[package]]
name = "rand_xorshift"
@ -1786,12 +1808,32 @@ version = "0.6.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b"
[[package]]
name = "rsor"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4b57e3964dc31a38416366d2e8f7675755402a10832d5cf4e4112d66ac77cdda"
[[package]]
name = "rustc-hash"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
[[package]]
name = "rustfft"
version = "6.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1d089e5c57521629a59f5f39bca7434849ff89bd6873b521afe389c1c602543"
dependencies = [
"num-complex",
"num-integer",
"num-traits",
"primal-check",
"strength_reduce",
"transpose",
]
[[package]]
name = "rusttype"
version = "0.9.2"
@ -1961,6 +2003,12 @@ version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ef5430c8e36b713e13b48a9f709cc21e046723fe44ce34587b73a830203b533e"
[[package]]
name = "strength_reduce"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a3ff2f71c82567c565ba4b3009a9350a96a7269eaa4001ebedae926230bc2254"
[[package]]
name = "strsim"
version = "0.9.3"
@ -1998,6 +2046,21 @@ dependencies = [
"syn",
]
[[package]]
name = "tinyvec"
version = "1.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2c1c1d5a42b6245520c249549ec267180beaffcc0615401ac8e31853d4b6d8d2"
dependencies = [
"tinyvec_macros",
]
[[package]]
name = "tinyvec_macros"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c"
[[package]]
name = "tokio-executor"
version = "0.1.10"
@ -2029,6 +2092,16 @@ dependencies = [
"serde",
]
[[package]]
name = "transpose"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95f9c900aa98b6ea43aee227fd680550cdec726526aab8ac801549eadb25e39f"
dependencies = [
"num-integer",
"strength_reduce",
]
[[package]]
name = "ttf-parser"
version = "0.6.2"
@ -2050,6 +2123,12 @@ dependencies = [
"nom 5.1.2",
]
[[package]]
name = "typenum"
version = "1.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcf81ac59edc17cc8697ff311e8f5ef2d99fcbd9817b34cec66f90b6c3dfd987"
[[package]]
name = "ultraviolet"
version = "0.8.1"

View file

@ -11,8 +11,8 @@ specs = { version = "0.16.1", default-features = false }
specs-derive = "0.4.1"
cpal = "0.13"
oddio = "0.5"
rand = "0.8"
spin_sleep = "1.0"
fundsp = "0.1.0"
typenum = "1.15.0"
[target.'cfg(windows)'.build-dependencies]
winres = "0.1"

View file

@ -2,7 +2,7 @@ use bracket_lib::prelude::*;
use specs::prelude::*;
use specs_derive::Component;
#[derive(Component)]
#[derive(Component, Clone, Copy)]
pub struct Position {
pub x: i32,
pub y: i32,
@ -13,3 +13,21 @@ impl PartialEq<Point> for Position {
self.x == other.x && self.y == other.y
}
}
impl From<Point> for Position {
fn from(point: Point) -> Self {
Self {
x: point.x,
y: point.y,
}
}
}
impl From<Position> for Point {
fn from(position: Position) -> Self {
Self {
x: position.x,
y: position.y,
}
}
}

View file

@ -182,6 +182,10 @@ impl Map {
self.tile_content[index] = Some(entity);
}
pub fn clear_tile_content(&mut self, index: usize) {
self.tile_content[index] = None;
}
pub fn get_tiles(&self) -> &Vec<TileType> {
&self.tiles
}

View file

@ -5,12 +5,15 @@ use bracket_lib::random::RandomNumberGenerator;
use crate::resources::sound_output::{SoundOutput, SoundSamples};
type Frequency = u32;
type StartFrequency = u32;
type EndFrequency = u32;
type MinFrequency = u32;
type MaxFrequency = u32;
pub enum SoundType {
Silence,
Tone(Frequency),
Sweep(StartFrequency, EndFrequency),
Noise(MinFrequency, MaxFrequency),
}
@ -31,6 +34,7 @@ pub struct SoundEffects {
pub blocked: SoundSamples,
pub whipping: SoundSamples,
pub whipping_hit: SoundSamples,
pub whipping_hit_end: SoundSamples,
rng: RandomNumberGenerator,
}
@ -38,13 +42,10 @@ impl SoundEffects {
pub fn new(ss: &SoundOutput) -> 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(),
sounds: vec![Sound {
sound_type: SoundType::Sweep(1, 350),
duration: Duration::from_secs(1),
}],
}),
step: ss.render_sound_effect(&SoundEffect {
sounds: vec![
@ -80,8 +81,8 @@ impl SoundEffects {
}),
bad_key: ss.render_sound_effect(&SoundEffect {
sounds: iter::once(Sound {
sound_type: SoundType::Tone(400),
duration: Duration::from_millis(20),
sound_type: SoundType::Tone(540),
duration: Duration::from_millis(40),
})
.chain((0..4).flat_map(|_| {
array::IntoIter::new([
@ -125,6 +126,12 @@ impl SoundEffects {
},
],
}),
whipping_hit_end: ss.render_sound_effect(&SoundEffect {
sounds: vec![Sound {
sound_type: SoundType::Tone(400),
duration: Duration::from_millis(20),
}],
}),
rng: RandomNumberGenerator::new(),
}
}

View file

@ -1,11 +1,11 @@
use std::{f32::consts::PI, sync::Arc};
use std::sync::Arc;
use cpal::{
traits::{DeviceTrait, HostTrait, StreamTrait},
SampleRate, Stream,
};
use fundsp::prelude::*;
use oddio::{Frames, FramesSignal, Gain, Handle, Mixer, MonoToStereo, Stop};
use rand::Rng;
use super::sound_effects::{SoundEffect, SoundType};
@ -70,43 +70,30 @@ impl SoundOutput {
.map(|_| 0f32)
.collect::<Vec<f32>>(),
SoundType::Tone(freq) => {
if freq == 0 {
return (0..(self.sample_rate.0 as f32 * sound.duration.as_secs_f32())
as usize)
.map(|_| 0f32)
.collect::<Vec<f32>>();
}
let num_harmonics = self.sample_rate.0 / (freq as u32 * 2);
let coefficients = (0..=num_harmonics)
.map(|i| {
if i == 0 {
return 0.0;
}
(i as f32 * 0.5 * PI).sin() * 2.0 / (i as f32 * PI)
})
.collect::<Vec<f32>>();
let scaler = freq as f32 * PI * 2.0 / self.sample_rate.0 as f32;
let mut c = square_hz(freq as f32);
c.reset(Some(self.sample_rate.0 as f64));
(0..(self.sample_rate.0 as f32 * sound.duration.as_secs_f32()) as usize)
.map(|i| {
let temp = scaler * i as f32;
coefficients
.iter()
.enumerate()
.map(|(j, coef)| coef * (j as f32 * temp).cos())
.sum::<f32>()
})
.map(|_| c.get_mono())
.collect::<Vec<f32>>()
}
SoundType::Sweep(start_freq, end_freq) => {
let mut c = lfo(|t| {
lerp(
start_freq as f32,
end_freq as f32,
t * sound.duration.as_secs_f32(),
)
}) >> square();
c.reset(Some(self.sample_rate.0 as f64));
(0..(self.sample_rate.0 as f32 * sound.duration.as_secs_f32()) as usize)
.map(|_| c.get_mono())
.collect::<Vec<f32>>()
}
SoundType::Noise(min, max) => {
let mut c =
(((white() + dc(1.0)) * dc(max as f32 / 2.0)) + dc(min as f32)) >> square();
(0..(self.sample_rate.0 as f32 * sound.duration.as_secs_f32()) as usize)
.map(|i| {
let t = i as f32 / self.sample_rate.0 as f32;
(t * (rand::thread_rng().gen_range(min as f32..max as f32))
* 2.0
* std::f32::consts::PI)
.sin()
.signum()
})
.map(|_| c.get_mono())
.collect::<Vec<f32>>()
}
})
@ -120,7 +107,7 @@ impl SoundOutput {
.control::<oddio::Mixer<_>, _>()
.play(oddio::MonoToStereo::new(oddio::Gain::new(
oddio::FramesSignal::from(samples),
0.20,
0.50,
)))
}
}

View file

@ -117,7 +117,7 @@ impl State {
let mut positions = self.ecs.write_storage::<Position>();
let mut players = self.ecs.write_storage::<Player>();
let monsters = self.ecs.write_storage::<Monster>();
let map = self.ecs.read_resource::<Map>();
let mut map = self.ecs.write_resource::<Map>();
let mut stats = self.ecs.write_resource::<Stats>();
let mut sound_system = self.ecs.write_resource::<SoundOutput>();
let wants_to_whips = self.ecs.read_storage::<WantsToWhip>();
@ -148,6 +148,9 @@ impl State {
}
}
let index = map.point2d_to_index(Point::from(*pos));
map.clear_tile_content(index);
pos.x = destination.x;
pos.y = destination.y;
@ -155,6 +158,9 @@ impl State {
player_pos.x = pos.x;
player_pos.y = pos.y;
let index = map.point2d_to_index(destination);
map.set_tile_content(index, player_entity);
self.ecs.write_resource::<Clock>().force_tick();
sound_system.play_sound(sound_effects.step.clone());
@ -172,10 +178,13 @@ impl State {
pub fn draw_whip(&self, ctx: &mut BTerm) {
let positions = self.ecs.read_storage::<Position>();
let wants_to_whips = self.ecs.read_storage::<WantsToWhip>();
let map = self.ecs.read_resource::<Map>();
let mut rng = RandomNumberGenerator::new();
for (position, wants_to_whip) in (&positions, &wants_to_whips).join() {
let color = RGB::named(vga::get_by_index(rng.range(1, 16)));
let frame_data = match wants_to_whip.frame {
let mut rendered_frame = wants_to_whip.frame;
let frame_data = loop {
let frame_data = match rendered_frame {
0 => Some((-1, -1, '\\')),
1 => Some((-1, 0, '─')),
2 => Some((-1, 1, '/')),
@ -186,6 +195,21 @@ impl State {
7 => Some((0, -1, '│')),
_ => None,
};
if let Some(data) = frame_data {
let dest = Point {
x: position.x + data.0,
y: position.y + data.1,
};
if map.in_bounds(dest) {
break frame_data;
}
rendered_frame += 1;
if rendered_frame > 7 {
break None;
}
}
};
if let Some(data) = frame_data {
ctx.set(
(position.x + MAP_X as i32) + data.0,
@ -199,9 +223,6 @@ impl State {
}
fn run_systems(&mut self) {
let mut map_indexing_system = MapIndexingSystem {};
map_indexing_system.run_now(&self.ecs);
let mut whip_system = WhipSystem {};
whip_system.run_now(&self.ecs);

View file

@ -1,27 +0,0 @@
use bracket_lib::prelude::*;
use specs::prelude::*;
use crate::{components::Position, resources::Map};
pub struct MapIndexingSystem {}
impl<'a> System<'a> for MapIndexingSystem {
type SystemData = (
WriteExpect<'a, Map>,
ReadStorage<'a, Position>,
Entities<'a>,
);
fn run(&mut self, data: Self::SystemData) {
let (mut map, position, entities) = data;
map.clear_all_tile_content();
for (entity, position) in (&entities, &position).join() {
let index = map.point2d_to_index(Point {
x: position.x,
y: position.y,
});
map.set_tile_content(index, entity);
}
}
}

View file

@ -1,7 +1,5 @@
pub mod map_indexing_system;
pub mod monster_ai_system;
pub mod whip_system;
pub use map_indexing_system::MapIndexingSystem;
pub use monster_ai_system::MonsterAiSystem;
pub use whip_system::WhipSystem;

View file

@ -17,7 +17,7 @@ impl<'a> System<'a> for MonsterAiSystem {
Entities<'a>,
ReadExpect<'a, Clock>,
ReadExpect<'a, Point>,
ReadExpect<'a, Map>,
WriteExpect<'a, Map>,
WriteExpect<'a, Stats>,
WriteStorage<'a, Monster>,
WriteStorage<'a, Position>,
@ -31,7 +31,7 @@ impl<'a> System<'a> for MonsterAiSystem {
entities,
clock,
player_pos,
map,
mut map,
mut stats,
mut monsters,
mut positions,
@ -88,8 +88,12 @@ impl<'a> System<'a> for MonsterAiSystem {
let _ = entities.delete(*entity);
}
_ => {
let index = map.point2d_to_index(Point::from(**position));
map.clear_tile_content(index);
position.x = destination.x;
position.y = destination.y;
let index = map.point2d_to_index(destination);
map.set_tile_content(index, *entity);
}
}
}

View file

@ -43,6 +43,7 @@ impl<'a> System<'a> for WhipSystem {
stop_clock.0 = true;
let now = Instant::now();
if now - wants_to_whip.last_frame > Duration::from_secs_f32(0.1) {
let destination = loop {
let destination = match wants_to_whip.frame {
0 => Some(Point {
x: position.x - 1,
@ -81,12 +82,26 @@ impl<'a> System<'a> for WhipSystem {
if let Some(dest) = destination {
if map.in_bounds(dest) {
break destination;
}
wants_to_whip.frame += 1;
if wants_to_whip.frame > 7 {
break None;
}
}
};
if let Some(dest) = destination {
if let Some(e) = map.get_tile_content(map.point2d_to_index(dest)) {
if let Some(_monster) = monsters.get(e) {
let _ = entities.delete(e);
if let Some(sound) = &mut wants_to_whip.sound {
sound.control::<oddio::Stop<_>, _>().stop();
}
if wants_to_whip.frame == 7 {
wants_to_whip.sound = None;
sound_output.play_sound(sound_effects.whipping_hit_end.clone());
} else {
wants_to_whip.sound = Some(
sound_output.play_sound(sound_effects.whipping_hit.clone()),
);
@ -96,7 +111,7 @@ impl<'a> System<'a> for WhipSystem {
}
if wants_to_whip.frame < 7 {
(*wants_to_whip).frame += 1;
wants_to_whip.frame += 1;
wants_to_whip.last_frame = now;
} else {
entities_to_remove.push(entity);