Compare commits

...

7 commits

Author SHA1 Message Date
CoCo_Sol 4df80f7e32 fix fmt nightly + fix name error 2023-06-02 09:04:27 +02:00
CoCo_Sol e1fdcd4405 Merge branch 'main' into 'recadrage-camera'
# Conflicts:
#   src/render.rs
2023-06-02 07:00:59 +00:00
CoCo_Sol 19ab110893 fontion get_path n'impl plus Tile. 2023-06-02 08:45:16 +02:00
CoCo_Sol f8cd281d26 mofification d'un use 2023-06-02 08:38:35 +02:00
CoCo_Sol c46281effa fix saut de ligne 2023-05-31 11:10:36 +02:00
CoCo_Sol 0bb19ace02 fix fmt + correction fonction 2023-05-28 13:16:51 +02:00
CoCo_Sol f25e729308 fonction de recadre et de zoom 2023-05-27 19:38:03 +02:00
2 changed files with 41 additions and 3 deletions

View file

@ -24,7 +24,8 @@ pub struct Position {
} }
impl Position { impl Position {
/// Peret de recuper la position, sous form d'un transform, d'un objet sur la carte du jeu. /// Peret de recuper la position, sous form d'un transform, d'un objet sur
/// la carte du jeu.
pub fn to_world(&self) -> Transform { pub fn to_world(&self) -> Transform {
let offset_x = self.y % 2; let offset_x = self.y % 2;
let new_x = (offset_x as f32).mul_add(0.5, self.x as f32); let new_x = (offset_x as f32).mul_add(0.5, self.x as f32);

View file

@ -1,7 +1,10 @@
//! Permet de Rendre le jeu sur l'ecran. //! Permet de Rendre le jeu sur l'ecran.
use crate::{map::Tile, Position};
use bevy::prelude::*; use bevy::prelude::*;
use bevy::render::camera::ScalingMode;
use crate::map::Tile;
use crate::Position;
/// Plugin permettant de rendre sur l'ecran le jeu. /// Plugin permettant de rendre sur l'ecran le jeu.
pub struct RenderPlugin; pub struct RenderPlugin;
@ -9,6 +12,7 @@ pub struct RenderPlugin;
impl Plugin for RenderPlugin { impl Plugin for RenderPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_startup_system(setup_camera) app.add_startup_system(setup_camera)
.add_system(update_camera)
.add_system(render_tiles); .add_system(render_tiles);
} }
} }
@ -16,7 +20,7 @@ impl Plugin for RenderPlugin {
/// Initialisation de la camera. /// Initialisation de la camera.
fn setup_camera(mut commands: Commands) { fn setup_camera(mut commands: Commands) {
commands.spawn(Camera2dBundle { commands.spawn(Camera2dBundle {
transform: Transform::from_xyz(5., 0.5, 100.), transform: Transform::from_xyz(0., 0., 100.),
..default() ..default()
}); });
} }
@ -53,3 +57,36 @@ const fn get_tile_path(tile: &Tile) -> &str {
Tile::Tower => "tiles/tower.png", Tile::Tower => "tiles/tower.png",
} }
} }
/// Permet de mettre a jour la camera de regler l'endroit de la camera et son
/// zoom.
fn update_camera(
all_positions: Query<&Position, With<Tile>>,
mut camera: Query<(&mut Transform, &mut OrthographicProjection), With<Camera>>,
) {
let mut min_x = 0;
let mut min_y = 0;
let mut max_x = 0;
let mut max_y = 0;
for position in all_positions.iter() {
if position.x < min_x {
min_x = position.x;
} else if position.x > max_x {
max_x = position.x;
}
if position.y < min_y {
min_y = position.y;
} else if position.y > max_y {
max_y = position.y;
}
}
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.;
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,
};
}