diff --git a/src/lib.rs b/src/lib.rs index aa5c7c3..7f3617d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,7 +8,7 @@ #![deny(clippy::all)] #![deny(warnings)] -use bevy::prelude::Component; +use bevy::prelude::*; pub mod map; pub mod render; @@ -23,6 +23,15 @@ pub struct Position { 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! { /// Peut ĂȘtre utiliser pour identifier n'importe quel objet appartenant a /// un joueur (le joueur compris). diff --git a/src/map.rs b/src/map.rs index 9e67b03..a67e97c 100644 --- a/src/map.rs +++ b/src/map.rs @@ -85,7 +85,7 @@ pub enum Tile { pub fn init_map(width: u8, height: u8, mut commands: Commands) { for x in 0..width { 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 })); } } diff --git a/src/render.rs b/src/render.rs index 08c1b71..6402afd 100644 --- a/src/render.rs +++ b/src/render.rs @@ -1,5 +1,6 @@ //! Permet de Rendre le jeu sur l'ecran. +use crate::{map::Tile, Position}; use bevy::prelude::*; /// Plugin permettant de rendre sur l'ecran le jeu. @@ -7,11 +8,48 @@ pub struct RenderPlugin; impl Plugin for RenderPlugin { 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. 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>, + mut commands: Commands, + asset_server: Res, +) { + 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", + } }