|
| 1 | +use bevy_ecs::prelude::*; |
| 2 | +use glam::Vec3; |
| 3 | + |
| 4 | +use crate::{ |
| 5 | + TransformComponent, |
| 6 | + audio::audio_command_queue::{AudioCommand, AudioCommandQueue}, |
| 7 | + components::simple_on_hit_audio_component::SimpleOnHitAudioComponent, |
| 8 | + physics::{ |
| 9 | + collision_system::ordered_pair, physics_event::PhysicsEventType, |
| 10 | + physics_resource::CollisionFrameData, |
| 11 | + }, |
| 12 | +}; |
| 13 | + |
| 14 | +pub struct SimplePhysAudioSystem {} |
| 15 | + |
| 16 | +impl SimplePhysAudioSystem { |
| 17 | + pub fn on_hit_audio_system( |
| 18 | + query: Query<(&SimpleOnHitAudioComponent, &TransformComponent)>, |
| 19 | + collision_frame_data: Res<CollisionFrameData>, |
| 20 | + mut audio_command_queue: ResMut<AudioCommandQueue>, |
| 21 | + ) { |
| 22 | + for manifold_entry in collision_frame_data.manifolds.iter() { |
| 23 | + let pair = ordered_pair(manifold_entry.entity_a, manifold_entry.entity_b); |
| 24 | + let event_type = if collision_frame_data.previous_manifolds.get(pair).is_some() { |
| 25 | + PhysicsEventType::Stay |
| 26 | + } else { |
| 27 | + PhysicsEventType::Hit |
| 28 | + }; |
| 29 | + |
| 30 | + match event_type { |
| 31 | + PhysicsEventType::Hit => { |
| 32 | + if query.get(manifold_entry.entity_a).is_ok() { |
| 33 | + let volume = query.get(manifold_entry.entity_a).unwrap().0.volume; |
| 34 | + let force_modifier = query |
| 35 | + .get(manifold_entry.entity_a) |
| 36 | + .unwrap() |
| 37 | + .0 |
| 38 | + .force_volume_scale; |
| 39 | + let contact_force = manifold_entry |
| 40 | + .manifold |
| 41 | + .contacts |
| 42 | + .iter() |
| 43 | + .map(|c| c.penetration) |
| 44 | + .sum::<f32>(); |
| 45 | + let final_volume = |
| 46 | + volume * (contact_force * force_modifier).clamp(0.0, 1.0); |
| 47 | + dbg!(final_volume); |
| 48 | + let sound_position = manifold_entry |
| 49 | + .manifold |
| 50 | + .contacts |
| 51 | + .iter() |
| 52 | + .map(|c| c.contact_point) |
| 53 | + .sum::<Vec3>() |
| 54 | + / manifold_entry.manifold.contacts.len() as f32; |
| 55 | + audio_command_queue |
| 56 | + .queue |
| 57 | + .push(AudioCommand::PlaySoundAtLocation { |
| 58 | + track: 0, |
| 59 | + sound: query.get(manifold_entry.entity_a).unwrap().0.sound_handle, |
| 60 | + volume: final_volume, |
| 61 | + looping: false, |
| 62 | + location: sound_position, |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + if query.get(manifold_entry.entity_b).is_ok() { |
| 67 | + let volume = query.get(manifold_entry.entity_b).unwrap().0.volume; |
| 68 | + let force_modifier = query |
| 69 | + .get(manifold_entry.entity_b) |
| 70 | + .unwrap() |
| 71 | + .0 |
| 72 | + .force_volume_scale; |
| 73 | + let contact_force = manifold_entry |
| 74 | + .manifold |
| 75 | + .contacts |
| 76 | + .iter() |
| 77 | + .map(|c| c.penetration) |
| 78 | + .sum::<f32>(); |
| 79 | + let final_volume = |
| 80 | + volume * (contact_force * force_modifier).clamp(0.0, 1.0); |
| 81 | + dbg!(final_volume); |
| 82 | + let sound_position = manifold_entry |
| 83 | + .manifold |
| 84 | + .contacts |
| 85 | + .iter() |
| 86 | + .map(|c| c.contact_point) |
| 87 | + .sum::<Vec3>() |
| 88 | + / manifold_entry.manifold.contacts.len() as f32; |
| 89 | + audio_command_queue |
| 90 | + .queue |
| 91 | + .push(AudioCommand::PlaySoundAtLocation { |
| 92 | + track: 0, |
| 93 | + sound: query.get(manifold_entry.entity_b).unwrap().0.sound_handle, |
| 94 | + volume: final_volume, |
| 95 | + looping: false, |
| 96 | + location: sound_position, |
| 97 | + }); |
| 98 | + } |
| 99 | + } |
| 100 | + PhysicsEventType::Stay => continue, |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments