Merge branch 'render-tile' into 'main'
Resolve "Affichage de la carte" Closes #19 See merge request CoCo_Sol/border-wars!7
This commit is contained in:
commit
e4f7eaa25e
11
src/lib.rs
11
src/lib.rs
|
@ -8,7 +8,7 @@
|
||||||
#![deny(clippy::all)]
|
#![deny(clippy::all)]
|
||||||
#![deny(warnings)]
|
#![deny(warnings)]
|
||||||
|
|
||||||
use bevy::prelude::Component;
|
use bevy::prelude::*;
|
||||||
|
|
||||||
pub mod map;
|
pub mod map;
|
||||||
pub mod render;
|
pub mod render;
|
||||||
|
@ -23,6 +23,15 @@ pub struct Position {
|
||||||
pub y: u8,
|
pub y: u8,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Position {
|
||||||
|
/// Peret de recuper la position, sous form d'un transform, d'un objet sur la carte du jeu.
|
||||||
|
pub fn to_world(&self) -> Transform {
|
||||||
|
let offset_x = self.y % 2;
|
||||||
|
let new_x = (offset_x as f32).mul_add(0.5, self.x as f32);
|
||||||
|
Transform::from_xyz(new_x, self.y as f32 * 0.42, self.y as f32 * -1.)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
slotmap::new_key_type! {
|
slotmap::new_key_type! {
|
||||||
/// Peut être utiliser pour identifier n'importe quel objet appartenant a
|
/// Peut être utiliser pour identifier n'importe quel objet appartenant a
|
||||||
/// un joueur (le joueur compris).
|
/// un joueur (le joueur compris).
|
||||||
|
|
|
@ -85,7 +85,7 @@ pub enum Tile {
|
||||||
pub fn init_map(width: u8, height: u8, mut commands: Commands) {
|
pub fn init_map(width: u8, height: u8, mut commands: Commands) {
|
||||||
for x in 0..width {
|
for x in 0..width {
|
||||||
for y in 0..height {
|
for y in 0..height {
|
||||||
if !(x % 2 == 0 && y == width - 1) {
|
if !(y % 2 == 1 && x == width - 1) {
|
||||||
commands.spawn((get_random_tile(), Position { x, y }));
|
commands.spawn((get_random_tile(), Position { x, y }));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
//! 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::*;
|
||||||
|
|
||||||
/// Plugin permettant de rendre sur l'ecran le jeu.
|
/// Plugin permettant de rendre sur l'ecran le jeu.
|
||||||
|
@ -7,11 +8,48 @@ 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(render_tiles);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Initialisation de la camera.
|
/// Initialisation de la camera.
|
||||||
fn setup_camera(mut commands: Commands) {
|
fn setup_camera(mut commands: Commands) {
|
||||||
commands.spawn(Camera2dBundle::default());
|
commands.spawn(Camera2dBundle {
|
||||||
|
transform: Transform::from_xyz(5., 0.5, 100.),
|
||||||
|
..default()
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Permet de rendre le tile et de les actualiser.
|
||||||
|
fn render_tiles(
|
||||||
|
tiles: Query<(Entity, &Tile, &Position), Changed<Tile>>,
|
||||||
|
mut commands: Commands,
|
||||||
|
asset_server: Res<AssetServer>,
|
||||||
|
) {
|
||||||
|
for (tile_entity, tile_type, tile_pos) in tiles.iter() {
|
||||||
|
commands.entity(tile_entity).insert(SpriteBundle {
|
||||||
|
texture: asset_server.load(get_tile_path(tile_type)),
|
||||||
|
transform: tile_pos.to_world().with_scale(Vec3::splat(1.0 / 185.0)),
|
||||||
|
..default()
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Recuper le chemin d'acces d'une case.
|
||||||
|
const fn get_tile_path(tile: &Tile) -> &str {
|
||||||
|
match *tile {
|
||||||
|
Tile::Castle => "tiles/castle.png",
|
||||||
|
Tile::Wall => "tiles/wall.png",
|
||||||
|
Tile::Farm => "tiles/farm.png",
|
||||||
|
Tile::Grass => "tiles/grass.png",
|
||||||
|
Tile::Forest => "tiles/forest.png",
|
||||||
|
Tile::Hill => "tiles/hill.png",
|
||||||
|
Tile::Lumberjack => "tiles/lumberjack.png",
|
||||||
|
Tile::Mine => "tiles/mine.png",
|
||||||
|
Tile::Outpost => "tiles/outpost.png",
|
||||||
|
Tile::Barrack => "tiles/barrack.png",
|
||||||
|
Tile::Upgrader => "tiles/upgrader.png",
|
||||||
|
Tile::Tower => "tiles/tower.png",
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue