Compare commits
7 commits
main
...
recadrage-
Author | SHA1 | Date | |
---|---|---|---|
CoCo_Sol | 4df80f7e32 | ||
CoCo_Sol | e1fdcd4405 | ||
CoCo_Sol | 19ab110893 | ||
CoCo_Sol | f8cd281d26 | ||
CoCo_Sol | c46281effa | ||
CoCo_Sol | 0bb19ace02 | ||
CoCo_Sol | f25e729308 |
|
@ -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);
|
||||||
|
|
|
@ -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,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue