Audio using HexoDSP
This commit is contained in:
parent
d9232fd5f0
commit
a2bf887700
10 changed files with 571 additions and 223 deletions
47
src/game.rs
47
src/game.rs
|
|
@ -1,6 +1,8 @@
|
|||
#![allow(clippy::precedence)]
|
||||
#![allow(clippy::too_many_arguments)]
|
||||
|
||||
pub use crate::audio::AudioMsg;
|
||||
|
||||
use crate::AppState;
|
||||
|
||||
use bevy::{
|
||||
|
|
@ -8,7 +10,6 @@ use bevy::{
|
|||
prelude::{shape::Quad, *},
|
||||
sprite::Mesh2dHandle,
|
||||
};
|
||||
use bevy_fundsp::prelude::*;
|
||||
use bevy_hanabi::*;
|
||||
use bevy_rapier2d::prelude::*;
|
||||
use std::collections::BTreeSet;
|
||||
|
|
@ -104,6 +105,7 @@ pub fn spawn_character(
|
|||
materials: &mut ResMut<Assets<ColorMaterial>>,
|
||||
selected_character_id: &mut Mut<SelectedCharacterId>,
|
||||
character_id_list: &mut Mut<CharacterIdList>,
|
||||
audio: &Res<crossbeam_channel::Sender<AudioMsg>>,
|
||||
transform: Transform,
|
||||
color: Color,
|
||||
) {
|
||||
|
|
@ -187,6 +189,9 @@ pub fn spawn_character(
|
|||
// If no character is selected, then select this one
|
||||
if selected_character_id.0.is_none() {
|
||||
selected_character_id.0 = Some(character_id);
|
||||
audio
|
||||
.send(AudioMsg::Color([color.r(), color.g(), color.b()]))
|
||||
.ok();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -199,6 +204,7 @@ fn collision_event_system(
|
|||
character_query: Query<(&CharacterId, &CharacterColor, &Transform)>,
|
||||
mut level_query: Query<(&mut SelectedCharacterId, &mut CharacterIdList)>,
|
||||
mut app_state: ResMut<State<AppState>>,
|
||||
audio: Res<crossbeam_channel::Sender<AudioMsg>>,
|
||||
) {
|
||||
for collision_event in collision_events.iter() {
|
||||
if let CollisionEvent::Started(e1, e2, flags) = collision_event {
|
||||
|
|
@ -233,6 +239,7 @@ fn collision_event_system(
|
|||
&mut materials,
|
||||
&mut selected_character_id,
|
||||
&mut character_id_list,
|
||||
&audio,
|
||||
*c1_transform,
|
||||
new_color.into(),
|
||||
);
|
||||
|
|
@ -244,27 +251,18 @@ fn collision_event_system(
|
|||
|
||||
fn keyboard_input_system(
|
||||
keyboard_input: Res<Input<KeyCode>>,
|
||||
mut characters: Query<(
|
||||
&CharacterId,
|
||||
&mut Velocity,
|
||||
&mut ExternalImpulse,
|
||||
&mut ExternalForce,
|
||||
&Children,
|
||||
)>,
|
||||
mut characters: Query<(&CharacterId, &mut Velocity, &CharacterColor, &Children)>,
|
||||
mut level_query: Query<(&mut SelectedCharacterId, &CharacterIdList)>,
|
||||
mut effect: Query<&mut ParticleEffect>,
|
||||
dsp_assets: Res<DspAssets>,
|
||||
audio: Res<Audio>,
|
||||
mut app_state: ResMut<State<AppState>>,
|
||||
audio: Res<crossbeam_channel::Sender<AudioMsg>>,
|
||||
) {
|
||||
if let Ok((mut selected_character_id, character_id_list)) = level_query.get_single_mut() {
|
||||
if keyboard_input.just_pressed(KeyCode::Tab) {
|
||||
audio.play(dsp_assets.graph(&sine_wave));
|
||||
|
||||
let selected = if let Some(selected_character_id) = &mut selected_character_id.0 {
|
||||
if let Some((_character_id, _velocity, _impulse, _force, children)) = characters
|
||||
if let Some((_character_id, _velocity, _color, children)) = characters
|
||||
.iter_mut()
|
||||
.find(|(character_id, _velocity, _impulse, _force, _children)| {
|
||||
.find(|(character_id, _velocity, _color, _children)| {
|
||||
*character_id == selected_character_id
|
||||
}) {
|
||||
effect
|
||||
|
|
@ -286,17 +284,19 @@ fn keyboard_input_system(
|
|||
CharacterId(0)
|
||||
};
|
||||
|
||||
if let Some((_character_id, _velocity, _impulse, _force, children)) = characters
|
||||
if let Some((_character_id, _velocity, color, children)) = characters
|
||||
.iter_mut()
|
||||
.find(|(character_id, _velocity, _impulse, _force, _children)| {
|
||||
**character_id == selected
|
||||
}) {
|
||||
.find(|(character_id, _velocity, _color, _children)| **character_id == selected)
|
||||
{
|
||||
effect
|
||||
.get_mut(children[0])
|
||||
.unwrap()
|
||||
.maybe_spawner()
|
||||
.unwrap()
|
||||
.set_active(true);
|
||||
audio
|
||||
.send(AudioMsg::Color([color.0.r(), color.0.g(), color.0.b()]))
|
||||
.ok();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -306,14 +306,15 @@ fn keyboard_input_system(
|
|||
keyboard_input.pressed(KeyCode::Left) || keyboard_input.pressed(KeyCode::A);
|
||||
|
||||
if let Some(selected_character_id) = &selected_character_id.0 {
|
||||
if let Some((_character_id, mut velocity, _impulse, _force, _children)) = characters
|
||||
if let Some((_character_id, mut velocity, _color, _children)) = characters
|
||||
.iter_mut()
|
||||
.find(|(character_id, _velocity, _impulse, _force, _children)| {
|
||||
.find(|(character_id, _velocity, _color, _children)| {
|
||||
*character_id == selected_character_id
|
||||
}) {
|
||||
velocity.linvel.x = 200. * (right_pressed as i8 - left_pressed as i8) as f32;
|
||||
|
||||
if keyboard_input.just_pressed(KeyCode::Space) {
|
||||
audio.send(AudioMsg::Jump).ok();
|
||||
velocity.linvel.y = 500.;
|
||||
}
|
||||
}
|
||||
|
|
@ -342,9 +343,3 @@ fn win_setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
|||
})
|
||||
.insert(Level);
|
||||
}
|
||||
|
||||
// Sounds
|
||||
|
||||
pub fn sine_wave() -> impl AudioUnit32 {
|
||||
sine_hz(440.0) >> split::<U2>() * 0.2
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue