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 crate::{
commands::RunIfExistsExt,
core::{Coordinates, CoreConfig, Player, PointLike},
exploration::ExplorationFocused,
visibility::{Viewshed, VisibleEntities},
@ -91,12 +92,15 @@ fn add_footstep_sounds(
) {
for (entity, footstep) in footsteps.iter() {
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,
state: SoundState::Stopped,
gain: footstep.gain,
gain,
..Default::default()
});
});
}
}
@ -138,18 +142,25 @@ fn add_sound_icon_sounds(
) {
for (entity, icon) in icons.iter() {
let buffer = assets.get_handle(icon.sound);
let gain = icon.gain;
let pitch = icon.pitch;
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,
gain: icon.gain,
pitch: icon.pitch,
gain,
pitch,
looping,
state: SoundState::Stopped,
reference_distance: icon.reference_distance,
max_distance: icon.max_distance,
rolloff_factor: icon.rolloff_factor,
reference_distance,
max_distance,
rolloff_factor,
..Default::default()
});
});
}
}