diff --git a/Cargo.lock b/Cargo.lock index a3b016e..ae70fac 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1024,6 +1024,7 @@ name = "border-wars" version = "0.1.0" dependencies = [ "bevy", + "rand", "slotmap", ] @@ -2428,6 +2429,12 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "ppv-lite86" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" + [[package]] name = "proc-macro-crate" version = "1.3.1" @@ -2468,6 +2475,36 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "17fd96390ed3feda12e1dfe2645ed587e0bea749e319333f104a33ff62f77a0b" +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + [[package]] name = "range-alloc" version = "0.1.3" diff --git a/Cargo.toml b/Cargo.toml index f6994e7..921aaa7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,4 +5,5 @@ edition = "2021" [dependencies] bevy = "0.10.1" +rand = "0.8.5" slotmap = "1.0.6" diff --git a/src/lib.rs b/src/lib.rs index dfde36f..461cfb3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,10 +8,11 @@ #![deny(clippy::all)] #![deny(warnings)] +use bevy::prelude::Component; pub mod map; /// Position d'un object sur la carte du jeu. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Component)] pub struct Position { /// La position en x. pub x: u8, @@ -22,5 +23,6 @@ pub struct Position { slotmap::new_key_type! { /// Peut être utiliser pour identifier n'importe quel objet appartenant a un joueur (le joueur compris). + #[derive(Component)] pub struct PlayerId; } diff --git a/src/map.rs b/src/map.rs index 7073893..bb4c5b7 100644 --- a/src/map.rs +++ b/src/map.rs @@ -1,6 +1,10 @@ //! Contiens tous ce qui est relatif à la carte du jeu. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] +use bevy::prelude::{Commands, Component}; + +use crate::{PlayerId, Position}; + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Component)] /// Une case sur la carte du jeu. pub enum Tile { /// Un chateau. @@ -70,3 +74,53 @@ pub enum Tile { /// Permet de bloquer le passage des troupes. Wall, } + +/// Permet de crée des entité representant les cases d'une map, sur une longueur et langueur donné +pub fn init_map( + longueur: u8, + largeur: u8, + mut commands: Commands, + id_joueur1: PlayerId, + id_joueur2: PlayerId, +) { + for y in 0..longueur { + for x in 0..largeur { + let mut curent_type = Tile::Grass; + let mut curent_id = None; + if x == 1 && y == 4 { + curent_type = Tile::Castle; + curent_id = Some(id_joueur1); + } else if x == longueur && y == 4 { + curent_type = Tile::Castle; + curent_id = Some(id_joueur2); + } else { + match (x, y) { + (0, 5) | (1, 5) | (2, 4) | (1, 3) | (0, 3) => { + curent_type = Tile::Grass; + curent_id = Some(id_joueur1) + } + + (9, 5) | (8, 5) | (8, 4) | (9, 3) | (8, 3) => { + curent_type = Tile::Grass; + curent_id = Some(id_joueur2) + } + + _ => { + if rand::random() { + if rand::random() { + curent_type = Tile::Forest + } else { + curent_type = Tile::Hill + } + } + } + } + } + if let Some(id) = curent_id { + commands.spawn((curent_type, id, Position { x, y })); + } else { + commands.spawn((curent_type, Position { x, y })); + } + } + } +}