Fix window close in example cause panic (#17533)
# Objective Fixes #17532 ## Solution - check window valide
This commit is contained in:
parent
51bb4f08a9
commit
dfac3b9bfd
@ -12,9 +12,13 @@ fn main() {
|
|||||||
|
|
||||||
fn draw_cursor(
|
fn draw_cursor(
|
||||||
camera_query: Single<(&Camera, &GlobalTransform)>,
|
camera_query: Single<(&Camera, &GlobalTransform)>,
|
||||||
window: Single<&Window>,
|
window: Query<&Window>,
|
||||||
mut gizmos: Gizmos,
|
mut gizmos: Gizmos,
|
||||||
) {
|
) {
|
||||||
|
let Ok(window) = window.get_single() else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
let (camera, camera_transform) = *camera_query;
|
let (camera, camera_transform) = *camera_query;
|
||||||
|
|
||||||
let Some(cursor_position) = window.cursor_position() else {
|
let Some(cursor_position) = window.cursor_position() else {
|
||||||
|
@ -13,9 +13,13 @@ fn main() {
|
|||||||
fn draw_cursor(
|
fn draw_cursor(
|
||||||
camera_query: Single<(&Camera, &GlobalTransform)>,
|
camera_query: Single<(&Camera, &GlobalTransform)>,
|
||||||
ground: Single<&GlobalTransform, With<Ground>>,
|
ground: Single<&GlobalTransform, With<Ground>>,
|
||||||
windows: Single<&Window>,
|
windows: Query<&Window>,
|
||||||
mut gizmos: Gizmos,
|
mut gizmos: Gizmos,
|
||||||
) {
|
) {
|
||||||
|
let Ok(windows) = windows.get_single() else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
let (camera, camera_transform) = *camera_query;
|
let (camera, camera_transform) = *camera_query;
|
||||||
|
|
||||||
let Some(cursor_position) = windows.cursor_position() else {
|
let Some(cursor_position) = windows.cursor_position() else {
|
||||||
|
@ -172,9 +172,13 @@ fn draw_shapes(mut gizmos: Gizmos, mines: Query<&Mine>) {
|
|||||||
fn handle_click(
|
fn handle_click(
|
||||||
mouse_button_input: Res<ButtonInput<MouseButton>>,
|
mouse_button_input: Res<ButtonInput<MouseButton>>,
|
||||||
camera: Single<(&Camera, &GlobalTransform)>,
|
camera: Single<(&Camera, &GlobalTransform)>,
|
||||||
windows: Single<&Window>,
|
windows: Query<&Window>,
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
) {
|
) {
|
||||||
|
let Ok(windows) = windows.get_single() else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
let (camera, camera_transform) = *camera;
|
let (camera, camera_transform) = *camera;
|
||||||
if let Some(pos) = windows
|
if let Some(pos) = windows
|
||||||
.cursor_position()
|
.cursor_position()
|
||||||
|
@ -42,7 +42,10 @@ fn move_system(mut sprites: Query<(&mut Transform, &Velocity)>) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Bounce sprites outside the window
|
// Bounce sprites outside the window
|
||||||
fn bounce_system(window: Single<&Window>, mut sprites: Query<(&Transform, &mut Velocity)>) {
|
fn bounce_system(window: Query<&Window>, mut sprites: Query<(&Transform, &mut Velocity)>) {
|
||||||
|
let Ok(window) = window.get_single() else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
let width = window.width();
|
let width = window.width();
|
||||||
let height = window.height();
|
let height = window.height();
|
||||||
let left = width / -2.0;
|
let left = width / -2.0;
|
||||||
|
@ -252,10 +252,14 @@ fn gravity(time: Res<Time>, mut velocity_query: Query<&mut Velocity>) {
|
|||||||
/// velocity. On collision with the ground it applies an upwards
|
/// velocity. On collision with the ground it applies an upwards
|
||||||
/// force.
|
/// force.
|
||||||
fn collisions(
|
fn collisions(
|
||||||
window: Single<&Window>,
|
window: Query<&Window>,
|
||||||
mut query: Query<(&mut Velocity, &mut Transform), With<Contributor>>,
|
mut query: Query<(&mut Velocity, &mut Transform), With<Contributor>>,
|
||||||
mut rng: ResMut<SharedRng>,
|
mut rng: ResMut<SharedRng>,
|
||||||
) {
|
) {
|
||||||
|
let Ok(window) = window.get_single() else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
let window_size = window.size();
|
let window_size = window.size();
|
||||||
|
|
||||||
let collision_area = Aabb2d::new(Vec2::ZERO, (window_size - SPRITE_SIZE) / 2.);
|
let collision_area = Aabb2d::new(Vec2::ZERO, (window_size - SPRITE_SIZE) / 2.);
|
||||||
|
@ -46,12 +46,16 @@ fn main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn touch_camera(
|
fn touch_camera(
|
||||||
window: Single<&Window>,
|
window: Query<&Window>,
|
||||||
mut touches: EventReader<TouchInput>,
|
mut touches: EventReader<TouchInput>,
|
||||||
mut camera_transform: Single<&mut Transform, With<Camera3d>>,
|
mut camera_transform: Single<&mut Transform, With<Camera3d>>,
|
||||||
mut last_position: Local<Option<Vec2>>,
|
mut last_position: Local<Option<Vec2>>,
|
||||||
mut rotations: EventReader<RotationGesture>,
|
mut rotations: EventReader<RotationGesture>,
|
||||||
) {
|
) {
|
||||||
|
let Ok(window) = window.get_single() else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
for touch in touches.read() {
|
for touch in touches.read() {
|
||||||
if touch.phase == TouchPhase::Started {
|
if touch.phase == TouchPhase::Started {
|
||||||
*last_position = None;
|
*last_position = None;
|
||||||
|
@ -327,12 +327,16 @@ fn mouse_handler(
|
|||||||
args: Res<Args>,
|
args: Res<Args>,
|
||||||
time: Res<Time>,
|
time: Res<Time>,
|
||||||
mouse_button_input: Res<ButtonInput<MouseButton>>,
|
mouse_button_input: Res<ButtonInput<MouseButton>>,
|
||||||
window: Single<&Window>,
|
window: Query<&Window>,
|
||||||
bird_resources: ResMut<BirdResources>,
|
bird_resources: ResMut<BirdResources>,
|
||||||
mut counter: ResMut<BevyCounter>,
|
mut counter: ResMut<BevyCounter>,
|
||||||
mut rng: Local<Option<ChaCha8Rng>>,
|
mut rng: Local<Option<ChaCha8Rng>>,
|
||||||
mut wave: Local<usize>,
|
mut wave: Local<usize>,
|
||||||
) {
|
) {
|
||||||
|
let Ok(window) = window.get_single() else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
if rng.is_none() {
|
if rng.is_none() {
|
||||||
// We're seeding the PRNG here to make this example deterministic for testing purposes.
|
// We're seeding the PRNG here to make this example deterministic for testing purposes.
|
||||||
// This isn't strictly required in practical use unless you need your app to be deterministic.
|
// This isn't strictly required in practical use unless you need your app to be deterministic.
|
||||||
@ -529,7 +533,11 @@ fn handle_collision(half_extents: Vec2, translation: &Vec3, velocity: &mut Vec3)
|
|||||||
velocity.y = 0.0;
|
velocity.y = 0.0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn collision_system(window: Single<&Window>, mut bird_query: Query<(&mut Bird, &Transform)>) {
|
fn collision_system(window: Query<&Window>, mut bird_query: Query<(&mut Bird, &Transform)>) {
|
||||||
|
let Ok(window) = window.get_single() else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
let half_extents = 0.5 * window.size();
|
let half_extents = 0.5 * window.size();
|
||||||
|
|
||||||
for (mut bird, transform) in &mut bird_query {
|
for (mut bird, transform) in &mut bird_query {
|
||||||
|
Loading…
Reference in New Issue
Block a user