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,12 +92,15 @@ 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;
commands.run_if_exists(entity, move |mut entity| {
entity.insert(Sound {
buffer, buffer,
state: SoundState::Stopped, state: SoundState::Stopped,
gain: footstep.gain, gain,
..Default::default() ..Default::default()
}); });
});
} }
} }
@ -138,18 +142,25 @@ 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;
let max_distance = icon.max_distance;
let rolloff_factor = icon.rolloff_factor;
commands.run_if_exists(entity, move |mut entity| {
entity.insert(Sound {
buffer, buffer,
gain: icon.gain, gain,
pitch: icon.pitch, pitch,
looping, looping,
state: SoundState::Stopped, state: SoundState::Stopped,
reference_distance: icon.reference_distance, reference_distance,
max_distance: icon.max_distance, max_distance,
rolloff_factor: icon.rolloff_factor, rolloff_factor,
..Default::default() ..Default::default()
}); });
});
} }
} }