fix fmt + correction fonction

This commit is contained in:
CoCo_Sol 2023-05-28 13:16:51 +02:00
parent f25e729308
commit 0bb19ace02

View file

@ -1,7 +1,11 @@
//! Permet de Rendre le jeu sur l'ecran.
use crate::{map::Tile, Position};
use bevy::{prelude::*, window::WindowResized};
use crate::{
map::Tile,
Position,
};
use bevy::{prelude::*, render::camera::ScalingMode};
/// Plugin permettant de rendre sur l'ecran le jeu.
pub struct RenderPlugin;
@ -60,7 +64,6 @@ impl Tile {
/// Permet de mettre a jour la camera de regler l'endroit de la camera et son zoom.
fn update_camera(
resize_event: Res<Events<WindowResized>>,
all_positions: Query<&Position, With<Tile>>,
mut camera: Query<(&mut Transform, &mut OrthographicProjection), With<Camera>>,
) {
@ -84,32 +87,9 @@ fn update_camera(
let (mut camera_transform, mut projection) = camera.single_mut();
camera_transform.translation.x = (max_x + min_x) as f32 / 2.0;
camera_transform.translation.y = ((max_y + min_y) as f32 / 2.0) - 2.;
for e in resize_event.get_reader().iter(&resize_event) {
let zoom_factor = calculate_zoom(
min_x as f32 - 1.,
max_x as f32 + 1.,
min_y as f32 - 1.,
max_y as f32 + 1.,
e.width,
e.height,
);
projection.scale = 1.15 / zoom_factor;
}
}
/// Calcule le zoom de la camera.
fn calculate_zoom(
min_x: f32,
max_x: f32,
min_y: f32,
max_y: f32,
viewport_width: f32,
viewport_height: f32,
) -> f32 {
let x_range = max_x - min_x;
let y_range = max_y - min_y;
let x_zoom = viewport_width / x_range;
let y_zoom = viewport_height / y_range;
x_zoom.min(y_zoom)
projection.scaling_mode = ScalingMode::AutoMin {
min_width: max_x as f32 - min_x as f32,
min_height: max_y as f32 - min_y as f32,
};
}