Improve the set_active_camera system (#4251)

# Objective

- Make `set_active_camera` system correctly respond to camera deletion, while preserving its correct behavior on first ever frame and any consequent frame, and with multiple cameras of the same type available in the world.
- Fixes #4227

## Solution

- Add a check that the entity referred to by `ActiveCamera` still exists in the world.
This commit is contained in:
Kirillov Kirill 2022-04-07 23:30:47 +00:00
parent d478723e19
commit 01bdf67c33

View File

@ -269,14 +269,22 @@ impl<T: Component> ActiveCamera<T> {
pub fn set_active_camera<T: Component>(
mut active_camera: ResMut<ActiveCamera<T>>,
cameras: Query<Entity, With<T>>,
cameras: Query<Entity, (With<Camera>, With<T>)>,
) {
if active_camera.get().is_some() {
// Check if there is already an active camera set and
// that it has not been deleted on the previous frame
if let Some(camera) = active_camera.get() {
if cameras.contains(camera) {
return;
}
}
// If the previous active camera ceased to exist
// fallback to another camera of the same type T
if let Some(camera) = cameras.iter().next() {
active_camera.camera = Some(camera);
} else {
active_camera.camera = None;
}
}