Fix crashes caused by non-existent entities.

This commit is contained in:
Nolan Darilek 2022-02-24 13:37:16 -06:00
parent 6d6ff3ae23
commit 3202956719

View File

@ -6,6 +6,7 @@ use bevy_openal::{Buffer, Context, Sound, SoundState};
use rand::random; use rand::random;
use crate::{ use crate::{
commands::RunIfExistsExt,
core::{Coordinates, CoreConfig, Player, PointLike}, core::{Coordinates, CoreConfig, Player, PointLike},
exploration::ExplorationFocused, exploration::ExplorationFocused,
visibility::{Viewshed, VisibleEntities}, visibility::{Viewshed, VisibleEntities},
@ -91,11 +92,14 @@ fn add_footstep_sounds(
) { ) {
for (entity, footstep) in footsteps.iter() { for (entity, footstep) in footsteps.iter() {
let buffer = assets.get_handle(footstep.sound); let buffer = assets.get_handle(footstep.sound);
commands.entity(entity).insert(Sound { let gain = footstep.gain;
buffer, commands.run_if_exists(entity, move |mut entity| {
state: SoundState::Stopped, entity.insert(Sound {
gain: footstep.gain, buffer,
..Default::default() state: SoundState::Stopped,
gain,
..Default::default()
});
}); });
} }
} }
@ -138,17 +142,24 @@ fn add_sound_icon_sounds(
) { ) {
for (entity, icon) in icons.iter() { for (entity, icon) in icons.iter() {
let buffer = assets.get_handle(icon.sound); let buffer = assets.get_handle(icon.sound);
let gain = icon.gain;
let pitch = icon.pitch;
let looping = icon.interval.is_none(); let looping = icon.interval.is_none();
commands.entity(entity).insert(Sound { let reference_distance = icon.reference_distance;
buffer, let max_distance = icon.max_distance;
gain: icon.gain, let rolloff_factor = icon.rolloff_factor;
pitch: icon.pitch, commands.run_if_exists(entity, move |mut entity| {
looping, entity.insert(Sound {
state: SoundState::Stopped, buffer,
reference_distance: icon.reference_distance, gain,
max_distance: icon.max_distance, pitch,
rolloff_factor: icon.rolloff_factor, looping,
..Default::default() state: SoundState::Stopped,
reference_distance,
max_distance,
rolloff_factor,
..Default::default()
});
}); });
} }
} }