From c8ad4818ab11c168dcaa02526ca20bba5204887c Mon Sep 17 00:00:00 2001 From: CoCoSol007 Date: Fri, 16 Feb 2024 22:40:13 +0100 Subject: [PATCH 01/37] save --- Cargo.lock | 103 ++++++++++++++++++++--- crates/border-wars/Cargo.toml | 1 + crates/border-wars/src/map/generation.rs | 65 ++++++++++++++ crates/border-wars/src/map/hex.rs | 3 +- crates/border-wars/src/map/mod.rs | 16 ++++ 5 files changed, 175 insertions(+), 13 deletions(-) create mode 100644 crates/border-wars/src/map/generation.rs diff --git a/Cargo.lock b/Cargo.lock index bb750c8..33c901a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -93,7 +93,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "77c3a9648d43b9cd48db467b3f87fdd6e146bcc88ab0180006cef2179fe11d01" dependencies = [ "cfg-if", - "getrandom", + "getrandom 0.2.12", "once_cell", "version_check", "zerocopy", @@ -1126,7 +1126,7 @@ checksum = "7915222f4a08ccc782e08d10b751b42e5f9d786e697d0cb3fd09333cb7e8b6ea" dependencies = [ "ahash", "bevy_utils_proc_macros", - "getrandom", + "getrandom 0.2.12", "hashbrown 0.14.3", "instant", "nonmax", @@ -1316,6 +1316,7 @@ version = "0.1.0" dependencies = [ "bevy", "bevy_egui", + "noise", "num", "partial-min-max", "paste", @@ -2108,6 +2109,17 @@ dependencies = [ "winapi", ] +[[package]] +name = "getrandom" +version = "0.1.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce" +dependencies = [ + "cfg-if", + "libc", + "wasi 0.9.0+wasi-snapshot-preview1", +] + [[package]] name = "getrandom" version = "0.2.12" @@ -2117,7 +2129,7 @@ dependencies = [ "cfg-if", "js-sys", "libc", - "wasi", + "wasi 0.11.0+wasi-snapshot-preview1", "wasm-bindgen", ] @@ -2847,7 +2859,7 @@ checksum = "8f3d0b296e374a4e6f3c7b0a1f5a51d748a0d34c85e7dc48fc3fa9a87657fe09" dependencies = [ "libc", "log", - "wasi", + "wasi 0.11.0+wasi-snapshot-preview1", "windows-sys 0.48.0", ] @@ -2961,6 +2973,17 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2bf50223579dc7cdcfb3bfcacf7069ff68243f8c363f62ffa99cf000a6b9c451" +[[package]] +name = "noise" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ba869e17168793186c10ca82c7079a4ffdeac4f1a7d9e755b9491c028180e40" +dependencies = [ + "num-traits", + "rand 0.7.3", + "rand_xorshift", +] + [[package]] name = "nom" version = "7.1.3" @@ -3475,6 +3498,19 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "17fd96390ed3feda12e1dfe2645ed587e0bea749e319333f104a33ff62f77a0b" +[[package]] +name = "rand" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" +dependencies = [ + "getrandom 0.1.16", + "libc", + "rand_chacha 0.2.2", + "rand_core 0.5.1", + "rand_hc", +] + [[package]] name = "rand" version = "0.8.5" @@ -3482,8 +3518,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" dependencies = [ "libc", - "rand_chacha", - "rand_core", + "rand_chacha 0.3.1", + "rand_core 0.6.4", +] + +[[package]] +name = "rand_chacha" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402" +dependencies = [ + "ppv-lite86", + "rand_core 0.5.1", ] [[package]] @@ -3493,7 +3539,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" dependencies = [ "ppv-lite86", - "rand_core", + "rand_core 0.6.4", +] + +[[package]] +name = "rand_core" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" +dependencies = [ + "getrandom 0.1.16", ] [[package]] @@ -3502,7 +3557,25 @@ version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" dependencies = [ - "getrandom", + "getrandom 0.2.12", +] + +[[package]] +name = "rand_hc" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" +dependencies = [ + "rand_core 0.5.1", +] + +[[package]] +name = "rand_xorshift" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77d416b86801d23dde1aa643023b775c3a462efc0ed96443add11546cdf1dca8" +dependencies = [ + "rand_core 0.5.1", ] [[package]] @@ -3607,7 +3680,7 @@ dependencies = [ "home", "log", "mio", - "rand", + "rand 0.8.5", "tungstenite", "uuid", ] @@ -3639,7 +3712,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "688c63d65483050968b2a8937f7995f443e27041a0f7700aa59b0822aedebb74" dependencies = [ "cc", - "getrandom", + "getrandom 0.2.12", "libc", "spin", "untrusted", @@ -4342,7 +4415,7 @@ dependencies = [ "http", "httparse", "log", - "rand", + "rand 0.8.5", "rustls", "rustls-native-certs", "rustls-pki-types", @@ -4430,7 +4503,7 @@ version = "1.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f00cc9702ca12d3c81455259621e676d0f7251cec66a21e98fe2e9a37db93b2a" dependencies = [ - "getrandom", + "getrandom 0.2.12", "serde", ] @@ -4468,6 +4541,12 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "wasi" +version = "0.9.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" diff --git a/crates/border-wars/Cargo.toml b/crates/border-wars/Cargo.toml index 70e1543..ccb73ba 100644 --- a/crates/border-wars/Cargo.toml +++ b/crates/border-wars/Cargo.toml @@ -13,6 +13,7 @@ workspace = true [dependencies] bevy = "0.12.1" bevy_egui = "0.24.0" +noise = "0.8.2" num = "0.4.1" partial-min-max = "0.4.0" paste = "1.0.14" diff --git a/crates/border-wars/src/map/generation.rs b/crates/border-wars/src/map/generation.rs new file mode 100644 index 0000000..5863817 --- /dev/null +++ b/crates/border-wars/src/map/generation.rs @@ -0,0 +1,65 @@ +//! All fonctions related to the generation of the map. + +use bevy::prelude::*; +use noise::{NoiseFn, Perlin}; + +use super::hex::*; +use super::Tile; + +/// A plugin to handle the map generation. +pub struct MapGenerationPlugin; + +impl Plugin for MapGenerationPlugin { + fn build(&self, app: &mut App) { + app.add_event::().add_systems( + Update, + generate_map.run_if(in_state(crate::CurrentScene::Game)), + ); + } +} + +/// An event to trigger the generation of the map. +#[derive(Event)] +pub struct MapGenerationEvent { + /// The seed used to generate the map. + pub seed: u32, + + /// The radius of the map. + pub radius: usize, +} + +/// spawns the tiles. +fn generate_map( + mut event: EventReader, + mut commands: Commands, + mut noise: Local>, + mut map_iterator: Local>>, +) { + // Handle map generation events. + for event in event.read() { + *noise = Some(Perlin::new(event.seed)); + *map_iterator = Some(HexPosition(0, 0).spiral(event.radius)); + } + + if let (Some(perlin), Some(spiral)) = (noise.as_ref(), map_iterator.as_mut()) { + if let Some(position) = spiral.next() { + let pixel_position = position.to_pixel_coordinates((0.1, 0.1)); + let type_tile = get_type_tile(pixel_position, perlin); + + commands.spawn((type_tile, position)); + } else { + *noise = None; + *map_iterator = None; + } + } +} + +/// Returns the type of the tile at the given position with the given noise. +fn get_type_tile(position: (f32, f32), noise: &Perlin) -> Tile { + let value = noise.get([position.0 as f64, position.1 as f64]); + match value { + v if v <= -0.4 => Tile::Hill, + v if v >= 0.4 => Tile::Forest, + _ => Tile::Grass, + } +} diff --git a/crates/border-wars/src/map/hex.rs b/crates/border-wars/src/map/hex.rs index 5410eff..5ee633c 100644 --- a/crates/border-wars/src/map/hex.rs +++ b/crates/border-wars/src/map/hex.rs @@ -4,6 +4,7 @@ use std::ops::{ Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Rem, RemAssign, Sub, SubAssign, }; +use bevy::prelude::*; use paste::paste; /// Represents a number that can be used in calculations for hexagonal grids. @@ -98,7 +99,7 @@ number_impl! { /// Represents a position in a hexagonal grid. /// We use the axial coordinate system explained in this /// [documentation](https://www.redblobgames.com/grids/hexagons/#coordinates). -#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Component)] pub struct HexPosition(pub T, pub T); /// All possible directions in a hexagonal grid. diff --git a/crates/border-wars/src/map/mod.rs b/crates/border-wars/src/map/mod.rs index 515d0c2..00eeeff 100644 --- a/crates/border-wars/src/map/mod.rs +++ b/crates/border-wars/src/map/mod.rs @@ -1,3 +1,19 @@ //! Contains all the logic related to the map. +pub mod generation; pub mod hex; + +use bevy::prelude::*; + +/// The tile of the map. +#[derive(Component)] +pub enum Tile { + /// The hill tile. + Hill, + + /// The grass tile. + Grass, + + /// The forest tile. + Forest, +} -- 2.43.4 From ef693b54d9ce00a734b8737772ca8ede2d33aa89 Mon Sep 17 00:00:00 2001 From: CoCoSol007 Date: Fri, 16 Feb 2024 22:54:16 +0100 Subject: [PATCH 02/37] change doc --- crates/border-wars/src/map/generation.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/border-wars/src/map/generation.rs b/crates/border-wars/src/map/generation.rs index 5863817..0a2b110 100644 --- a/crates/border-wars/src/map/generation.rs +++ b/crates/border-wars/src/map/generation.rs @@ -28,7 +28,7 @@ pub struct MapGenerationEvent { pub radius: usize, } -/// spawns the tiles. +/// spawns the tiles if the event is received. fn generate_map( mut event: EventReader, mut commands: Commands, -- 2.43.4 From 066561ff6c0f6d1e2bd4f8d38a9b618cbd0230b2 Mon Sep 17 00:00:00 2001 From: CoCoSol007 Date: Fri, 16 Feb 2024 22:55:11 +0100 Subject: [PATCH 03/37] chnage disposition --- crates/border-wars/src/map/generation.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/border-wars/src/map/generation.rs b/crates/border-wars/src/map/generation.rs index 0a2b110..b3408e4 100644 --- a/crates/border-wars/src/map/generation.rs +++ b/crates/border-wars/src/map/generation.rs @@ -44,9 +44,8 @@ fn generate_map( if let (Some(perlin), Some(spiral)) = (noise.as_ref(), map_iterator.as_mut()) { if let Some(position) = spiral.next() { let pixel_position = position.to_pixel_coordinates((0.1, 0.1)); - let type_tile = get_type_tile(pixel_position, perlin); - commands.spawn((type_tile, position)); + commands.spawn((get_type_tile(pixel_position, perlin), position)); } else { *noise = None; *map_iterator = None; -- 2.43.4 From 32f09423cb0fb309c9a9c0ed84caf6d3098d7c6e Mon Sep 17 00:00:00 2001 From: CoCoSol007 Date: Fri, 16 Feb 2024 23:21:54 +0100 Subject: [PATCH 04/37] change doc --- crates/border-wars/src/map/generation.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/border-wars/src/map/generation.rs b/crates/border-wars/src/map/generation.rs index b3408e4..0860304 100644 --- a/crates/border-wars/src/map/generation.rs +++ b/crates/border-wars/src/map/generation.rs @@ -28,7 +28,7 @@ pub struct MapGenerationEvent { pub radius: usize, } -/// spawns the tiles if the event is received. +/// Spawns the tiles if the event is received. fn generate_map( mut event: EventReader, mut commands: Commands, -- 2.43.4 From e3c2bc1ac90f586926bfc5d6e12a9866ab4ca579 Mon Sep 17 00:00:00 2001 From: CoCoSol007 Date: Sat, 17 Feb 2024 21:10:40 +0100 Subject: [PATCH 05/37] ajout d'un event de fin de map --- crates/border-wars/src/map/generation.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/crates/border-wars/src/map/generation.rs b/crates/border-wars/src/map/generation.rs index 0860304..dcd92b8 100644 --- a/crates/border-wars/src/map/generation.rs +++ b/crates/border-wars/src/map/generation.rs @@ -28,9 +28,14 @@ pub struct MapGenerationEvent { pub radius: usize, } +/// An event send when the map is generated. +#[derive(Event)] +pub struct EndMapGenerationEvent; + /// Spawns the tiles if the event is received. fn generate_map( mut event: EventReader, + mut end_map_event: EventWriter, mut commands: Commands, mut noise: Local>, mut map_iterator: Local>>, @@ -47,6 +52,7 @@ fn generate_map( commands.spawn((get_type_tile(pixel_position, perlin), position)); } else { + end_map_event.send(EndMapGenerationEvent); *noise = None; *map_iterator = None; } -- 2.43.4 From 1350b04e296e4cf06a09ffbfb50436c0b2bca54e Mon Sep 17 00:00:00 2001 From: CoCoSol007 Date: Sat, 17 Feb 2024 21:42:38 +0100 Subject: [PATCH 06/37] delete the map --- crates/border-wars/src/map/generation.rs | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/crates/border-wars/src/map/generation.rs b/crates/border-wars/src/map/generation.rs index dcd92b8..fa6a8a6 100644 --- a/crates/border-wars/src/map/generation.rs +++ b/crates/border-wars/src/map/generation.rs @@ -11,10 +11,13 @@ pub struct MapGenerationPlugin; impl Plugin for MapGenerationPlugin { fn build(&self, app: &mut App) { - app.add_event::().add_systems( - Update, - generate_map.run_if(in_state(crate::CurrentScene::Game)), - ); + app.add_event::() + .add_event::() + .add_systems( + Update, + (generate_map.after(delete_map), delete_map) + .run_if(in_state(crate::CurrentScene::Game)), + ); } } @@ -68,3 +71,15 @@ fn get_type_tile(position: (f32, f32), noise: &Perlin) -> Tile { _ => Tile::Grass, } } + +fn delete_map( + mut commands: Commands, + query: Query>, + mut event: EventReader, +) { + for _ in event.read() { + for entity in query.iter() { + commands.entity(entity).despawn_recursive(); + } + } +} -- 2.43.4 From 0fdc351356db62980e9b7e20ec01460411109c19 Mon Sep 17 00:00:00 2001 From: CoCoSol007 Date: Sat, 17 Feb 2024 21:43:21 +0100 Subject: [PATCH 07/37] add doc --- crates/border-wars/src/map/generation.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/border-wars/src/map/generation.rs b/crates/border-wars/src/map/generation.rs index fa6a8a6..90a9cd5 100644 --- a/crates/border-wars/src/map/generation.rs +++ b/crates/border-wars/src/map/generation.rs @@ -72,6 +72,7 @@ fn get_type_tile(position: (f32, f32), noise: &Perlin) -> Tile { } } +/// Despawns the tiles if the event : [EndMapGenerationEvent] is received. fn delete_map( mut commands: Commands, query: Query>, -- 2.43.4 From 744d9b646e56780f04ba8c2a4c939592889f5231 Mon Sep 17 00:00:00 2001 From: CoCoSol007 Date: Sun, 18 Feb 2024 11:17:40 +0100 Subject: [PATCH 08/37] update --- crates/border-wars/src/map/generation.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/border-wars/src/map/generation.rs b/crates/border-wars/src/map/generation.rs index 90a9cd5..cd5334b 100644 --- a/crates/border-wars/src/map/generation.rs +++ b/crates/border-wars/src/map/generation.rs @@ -52,7 +52,6 @@ fn generate_map( if let (Some(perlin), Some(spiral)) = (noise.as_ref(), map_iterator.as_mut()) { if let Some(position) = spiral.next() { let pixel_position = position.to_pixel_coordinates((0.1, 0.1)); - commands.spawn((get_type_tile(pixel_position, perlin), position)); } else { end_map_event.send(EndMapGenerationEvent); @@ -76,7 +75,7 @@ fn get_type_tile(position: (f32, f32), noise: &Perlin) -> Tile { fn delete_map( mut commands: Commands, query: Query>, - mut event: EventReader, + mut event: EventReader, ) { for _ in event.read() { for entity in query.iter() { -- 2.43.4 From fcd0bdf9ed98fd184a207808f41e28795c82218d Mon Sep 17 00:00:00 2001 From: CoCoSol007 Date: Sun, 18 Feb 2024 20:59:52 +0100 Subject: [PATCH 09/37] update --- crates/border-wars/src/map/generation.rs | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/crates/border-wars/src/map/generation.rs b/crates/border-wars/src/map/generation.rs index cd5334b..0eca720 100644 --- a/crates/border-wars/src/map/generation.rs +++ b/crates/border-wars/src/map/generation.rs @@ -11,8 +11,8 @@ pub struct MapGenerationPlugin; impl Plugin for MapGenerationPlugin { fn build(&self, app: &mut App) { - app.add_event::() - .add_event::() + app.add_event::() + .add_event::() .add_systems( Update, (generate_map.after(delete_map), delete_map) @@ -23,7 +23,7 @@ impl Plugin for MapGenerationPlugin { /// An event to trigger the generation of the map. #[derive(Event)] -pub struct MapGenerationEvent { +pub struct MapGeneration { /// The seed used to generate the map. pub seed: u32, @@ -33,12 +33,12 @@ pub struct MapGenerationEvent { /// An event send when the map is generated. #[derive(Event)] -pub struct EndMapGenerationEvent; +pub struct EndMapGeneration; /// Spawns the tiles if the event is received. fn generate_map( - mut event: EventReader, - mut end_map_event: EventWriter, + mut event: EventReader, + mut end_map_event: EventWriter, mut commands: Commands, mut noise: Local>, mut map_iterator: Local>>, @@ -51,10 +51,10 @@ fn generate_map( if let (Some(perlin), Some(spiral)) = (noise.as_ref(), map_iterator.as_mut()) { if let Some(position) = spiral.next() { - let pixel_position = position.to_pixel_coordinates((0.1, 0.1)); + let pixel_position = position.to_pixel_coordinates((0.2, 0.2)); // Réduire la taille de la tuile commands.spawn((get_type_tile(pixel_position, perlin), position)); } else { - end_map_event.send(EndMapGenerationEvent); + end_map_event.send(EndMapGeneration); *noise = None; *map_iterator = None; } @@ -65,17 +65,18 @@ fn generate_map( fn get_type_tile(position: (f32, f32), noise: &Perlin) -> Tile { let value = noise.get([position.0 as f64, position.1 as f64]); match value { - v if v <= -0.4 => Tile::Hill, - v if v >= 0.4 => Tile::Forest, + v if v <= -0.5 => Tile::Hill, // Réduire le seuil pour les collines + v if v >= 0.5 => Tile::Forest, // Réduire le seuil pour les forêts _ => Tile::Grass, } } + /// Despawns the tiles if the event : [EndMapGenerationEvent] is received. fn delete_map( mut commands: Commands, query: Query>, - mut event: EventReader, + mut event: EventReader, ) { for _ in event.read() { for entity in query.iter() { -- 2.43.4 From d3c89956a1ea0080fae645e5a43644e853f6cab3 Mon Sep 17 00:00:00 2001 From: CoCoSol007 Date: Sun, 18 Feb 2024 21:00:42 +0100 Subject: [PATCH 10/37] update --- crates/border-wars/src/map/generation.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/border-wars/src/map/generation.rs b/crates/border-wars/src/map/generation.rs index 0eca720..9cf9fff 100644 --- a/crates/border-wars/src/map/generation.rs +++ b/crates/border-wars/src/map/generation.rs @@ -65,13 +65,12 @@ fn generate_map( fn get_type_tile(position: (f32, f32), noise: &Perlin) -> Tile { let value = noise.get([position.0 as f64, position.1 as f64]); match value { - v if v <= -0.5 => Tile::Hill, // Réduire le seuil pour les collines + v if v <= -0.5 => Tile::Hill, // Réduire le seuil pour les collines v if v >= 0.5 => Tile::Forest, // Réduire le seuil pour les forêts _ => Tile::Grass, } } - /// Despawns the tiles if the event : [EndMapGenerationEvent] is received. fn delete_map( mut commands: Commands, -- 2.43.4 From 135f1b73472f0ee9a7bb0f75d33974b8626e920a Mon Sep 17 00:00:00 2001 From: CoCoSol007 Date: Sun, 18 Feb 2024 21:02:32 +0100 Subject: [PATCH 11/37] update doc --- crates/border-wars/src/map/generation.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/border-wars/src/map/generation.rs b/crates/border-wars/src/map/generation.rs index 9cf9fff..34ccf49 100644 --- a/crates/border-wars/src/map/generation.rs +++ b/crates/border-wars/src/map/generation.rs @@ -51,7 +51,7 @@ fn generate_map( if let (Some(perlin), Some(spiral)) = (noise.as_ref(), map_iterator.as_mut()) { if let Some(position) = spiral.next() { - let pixel_position = position.to_pixel_coordinates((0.2, 0.2)); // Réduire la taille de la tuile + let pixel_position = position.to_pixel_coordinates((0.2, 0.2)); commands.spawn((get_type_tile(pixel_position, perlin), position)); } else { end_map_event.send(EndMapGeneration); @@ -65,8 +65,8 @@ fn generate_map( fn get_type_tile(position: (f32, f32), noise: &Perlin) -> Tile { let value = noise.get([position.0 as f64, position.1 as f64]); match value { - v if v <= -0.5 => Tile::Hill, // Réduire le seuil pour les collines - v if v >= 0.5 => Tile::Forest, // Réduire le seuil pour les forêts + v if v <= -0.5 => Tile::Hill, + v if v >= 0.5 => Tile::Forest, _ => Tile::Grass, } } -- 2.43.4 From f5ffcffb131109b462397dcdaefaf88ea901cbd8 Mon Sep 17 00:00:00 2001 From: CoCoSol007 Date: Sun, 18 Feb 2024 22:04:01 +0100 Subject: [PATCH 12/37] update --- crates/border-wars/src/map/generation.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/border-wars/src/map/generation.rs b/crates/border-wars/src/map/generation.rs index 34ccf49..31aabba 100644 --- a/crates/border-wars/src/map/generation.rs +++ b/crates/border-wars/src/map/generation.rs @@ -65,8 +65,8 @@ fn generate_map( fn get_type_tile(position: (f32, f32), noise: &Perlin) -> Tile { let value = noise.get([position.0 as f64, position.1 as f64]); match value { - v if v <= -0.5 => Tile::Hill, - v if v >= 0.5 => Tile::Forest, + v if v <= -0.4 => Tile::Hill, + v if v >= 0.4 => Tile::Forest, _ => Tile::Grass, } } -- 2.43.4 From 73c25c8bd5bc7192c02bf0948529fb64e51be6c8 Mon Sep 17 00:00:00 2001 From: CoCoSol007 Date: Tue, 20 Feb 2024 11:20:41 +0100 Subject: [PATCH 13/37] rename --- crates/border-wars/src/map/generation.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/border-wars/src/map/generation.rs b/crates/border-wars/src/map/generation.rs index 31aabba..9efd643 100644 --- a/crates/border-wars/src/map/generation.rs +++ b/crates/border-wars/src/map/generation.rs @@ -11,7 +11,7 @@ pub struct MapGenerationPlugin; impl Plugin for MapGenerationPlugin { fn build(&self, app: &mut App) { - app.add_event::() + app.add_event::() .add_event::() .add_systems( Update, @@ -23,7 +23,7 @@ impl Plugin for MapGenerationPlugin { /// An event to trigger the generation of the map. #[derive(Event)] -pub struct MapGeneration { +pub struct StartMapGeneration { /// The seed used to generate the map. pub seed: u32, @@ -37,7 +37,7 @@ pub struct EndMapGeneration; /// Spawns the tiles if the event is received. fn generate_map( - mut event: EventReader, + mut event: EventReader, mut end_map_event: EventWriter, mut commands: Commands, mut noise: Local>, @@ -75,7 +75,7 @@ fn get_type_tile(position: (f32, f32), noise: &Perlin) -> Tile { fn delete_map( mut commands: Commands, query: Query>, - mut event: EventReader, + mut event: EventReader, ) { for _ in event.read() { for entity in query.iter() { -- 2.43.4 From 748bde27b4a08a4342a23e0434d5902d00172c35 Mon Sep 17 00:00:00 2001 From: CoCoSol007 Date: Tue, 20 Feb 2024 11:22:18 +0100 Subject: [PATCH 14/37] change base --- crates/border-wars/src/map/generation.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/border-wars/src/map/generation.rs b/crates/border-wars/src/map/generation.rs index 9efd643..2d320ca 100644 --- a/crates/border-wars/src/map/generation.rs +++ b/crates/border-wars/src/map/generation.rs @@ -28,7 +28,7 @@ pub struct StartMapGeneration { pub seed: u32, /// The radius of the map. - pub radius: usize, + pub radius: u16, } /// An event send when the map is generated. @@ -41,12 +41,12 @@ fn generate_map( mut end_map_event: EventWriter, mut commands: Commands, mut noise: Local>, - mut map_iterator: Local>>, + mut map_iterator: Local>>, ) { // Handle map generation events. for event in event.read() { *noise = Some(Perlin::new(event.seed)); - *map_iterator = Some(HexPosition(0, 0).spiral(event.radius)); + *map_iterator = Some(HexPosition(0, 0).spiral(event.radius as usize)); } if let (Some(perlin), Some(spiral)) = (noise.as_ref(), map_iterator.as_mut()) { -- 2.43.4 From 07ffb9e45c1c6c531369e4fc1e50aabbd701f2f3 Mon Sep 17 00:00:00 2001 From: CoCoSol007 Date: Tue, 20 Feb 2024 11:31:08 +0100 Subject: [PATCH 15/37] change doc --- crates/border-wars/src/map/generation.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/border-wars/src/map/generation.rs b/crates/border-wars/src/map/generation.rs index 2d320ca..63013da 100644 --- a/crates/border-wars/src/map/generation.rs +++ b/crates/border-wars/src/map/generation.rs @@ -71,7 +71,7 @@ fn get_type_tile(position: (f32, f32), noise: &Perlin) -> Tile { } } -/// Despawns the tiles if the event : [EndMapGenerationEvent] is received. +/// Despawns the tiles if the event : [MapGenerationEvent] is received. fn delete_map( mut commands: Commands, query: Query>, -- 2.43.4 From acd2de265c100133443618b3cc7fc1782d73fec0 Mon Sep 17 00:00:00 2001 From: CoCoSol007 Date: Tue, 20 Feb 2024 12:16:05 +0100 Subject: [PATCH 16/37] rename --- crates/border-wars/src/map/generation.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/border-wars/src/map/generation.rs b/crates/border-wars/src/map/generation.rs index 63013da..ab0b2b1 100644 --- a/crates/border-wars/src/map/generation.rs +++ b/crates/border-wars/src/map/generation.rs @@ -52,17 +52,17 @@ fn generate_map( if let (Some(perlin), Some(spiral)) = (noise.as_ref(), map_iterator.as_mut()) { if let Some(position) = spiral.next() { let pixel_position = position.to_pixel_coordinates((0.2, 0.2)); - commands.spawn((get_type_tile(pixel_position, perlin), position)); + commands.spawn((get_type(pixel_position, perlin), position)); } else { end_map_event.send(EndMapGeneration); *noise = None; *map_iterator = None; } - } + } } -/// Returns the type of the tile at the given position with the given noise. -fn get_type_tile(position: (f32, f32), noise: &Perlin) -> Tile { +/// Returns the type of the position with the given noise. +fn get_type(position: (f32, f32), noise: &Perlin) -> Tile { let value = noise.get([position.0 as f64, position.1 as f64]); match value { v if v <= -0.4 => Tile::Hill, -- 2.43.4 From 915d5ba5a45658afb90eadd1cdef040f4085fd24 Mon Sep 17 00:00:00 2001 From: CoCoSol007 Date: Wed, 21 Feb 2024 11:15:54 +0100 Subject: [PATCH 17/37] update --- crates/border-wars/src/map/generation.rs | 26 +++++++++++++----------- crates/border-wars/src/map/hex.rs | 5 +++++ crates/border-wars/src/map/mod.rs | 7 ++++++- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/crates/border-wars/src/map/generation.rs b/crates/border-wars/src/map/generation.rs index ab0b2b1..bbb0092 100644 --- a/crates/border-wars/src/map/generation.rs +++ b/crates/border-wars/src/map/generation.rs @@ -5,6 +5,7 @@ use noise::{NoiseFn, Perlin}; use super::hex::*; use super::Tile; +use super::TilePosition; /// A plugin to handle the map generation. pub struct MapGenerationPlugin; @@ -41,24 +42,25 @@ fn generate_map( mut end_map_event: EventWriter, mut commands: Commands, mut noise: Local>, - mut map_iterator: Local>>, + mut spiral: Local>>, ) { // Handle map generation events. for event in event.read() { *noise = Some(Perlin::new(event.seed)); - *map_iterator = Some(HexPosition(0, 0).spiral(event.radius as usize)); + *spiral = Some(TilePosition::new(0,0).spiral(event.radius as usize)); } - if let (Some(perlin), Some(spiral)) = (noise.as_ref(), map_iterator.as_mut()) { - if let Some(position) = spiral.next() { - let pixel_position = position.to_pixel_coordinates((0.2, 0.2)); - commands.spawn((get_type(pixel_position, perlin), position)); - } else { - end_map_event.send(EndMapGeneration); - *noise = None; - *map_iterator = None; - } - } + if let (None, None) = (noise.as_ref(), spiral.as_mut()) { + return; + } + if let Some(position) = spiral.as_mut().unwrap().next() { + let pixel_position = position.to_pixel_coordinates((0.2, 0.2)); + commands.spawn((get_type(pixel_position, &noise.unwrap()), position as TilePosition)); + } else { + end_map_event.send(EndMapGeneration); + *noise = None; + *spiral = None; + } } /// Returns the type of the position with the given noise. diff --git a/crates/border-wars/src/map/hex.rs b/crates/border-wars/src/map/hex.rs index 5ee633c..3edb991 100644 --- a/crates/border-wars/src/map/hex.rs +++ b/crates/border-wars/src/map/hex.rs @@ -234,6 +234,11 @@ impl Iterator for HexSpiral { } impl HexPosition { + /// Creates a new [HexPosition]. + pub fn new(x: T, y: T) -> Self { + Self(x, y) + } + /// Converts the current [HexPosition] into a pixel coordinate. /// Input: The size of the hexagon in pixels (witdh, height). /// diff --git a/crates/border-wars/src/map/mod.rs b/crates/border-wars/src/map/mod.rs index 00eeeff..5265d18 100644 --- a/crates/border-wars/src/map/mod.rs +++ b/crates/border-wars/src/map/mod.rs @@ -5,8 +5,13 @@ pub mod hex; use bevy::prelude::*; +use self::hex::*; + +/// The position of a tile in a hexagonal map. +pub type TilePosition = HexPosition; + /// The tile of the map. -#[derive(Component)] +#[derive(Component, Debug)] pub enum Tile { /// The hill tile. Hill, -- 2.43.4 From 74d81721c768cc33879550b32545826932db45cf Mon Sep 17 00:00:00 2001 From: CoCoSol007 Date: Wed, 21 Feb 2024 11:35:11 +0100 Subject: [PATCH 18/37] add let else --- crates/border-wars/src/map/generation.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/crates/border-wars/src/map/generation.rs b/crates/border-wars/src/map/generation.rs index bbb0092..d466c1b 100644 --- a/crates/border-wars/src/map/generation.rs +++ b/crates/border-wars/src/map/generation.rs @@ -41,25 +41,25 @@ fn generate_map( mut event: EventReader, mut end_map_event: EventWriter, mut commands: Commands, - mut noise: Local>, - mut spiral: Local>>, + mut local_noise: Local>, + mut local_spiral: Local>>, ) { // Handle map generation events. for event in event.read() { - *noise = Some(Perlin::new(event.seed)); - *spiral = Some(TilePosition::new(0,0).spiral(event.radius as usize)); + *local_noise = Some(Perlin::new(event.seed)); + *local_spiral = Some(TilePosition::new(0,0).spiral(event.radius as usize)); } - if let (None, None) = (noise.as_ref(), spiral.as_mut()) { + let (Some(noise), Some(spiral)) = (local_noise.as_ref(), local_spiral.as_mut()) else { return; - } - if let Some(position) = spiral.as_mut().unwrap().next() { + }; + if let Some(position) = spiral.next() { let pixel_position = position.to_pixel_coordinates((0.2, 0.2)); - commands.spawn((get_type(pixel_position, &noise.unwrap()), position as TilePosition)); + commands.spawn((get_type(pixel_position, &noise), position as TilePosition)); } else { end_map_event.send(EndMapGeneration); - *noise = None; - *spiral = None; + *local_noise = None; + *local_spiral = None; } } -- 2.43.4 From 3ebe60d6cd80967b1be17dc7ad349d1ac2bb96f4 Mon Sep 17 00:00:00 2001 From: Corentin Date: Wed, 21 Feb 2024 10:38:51 +0000 Subject: [PATCH 19/37] Update crates/border-wars/src/map/generation.rs --- crates/border-wars/src/map/generation.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/border-wars/src/map/generation.rs b/crates/border-wars/src/map/generation.rs index d466c1b..ed56c4e 100644 --- a/crates/border-wars/src/map/generation.rs +++ b/crates/border-wars/src/map/generation.rs @@ -73,7 +73,7 @@ fn get_type(position: (f32, f32), noise: &Perlin) -> Tile { } } -/// Despawns the tiles if the event : [MapGenerationEvent] is received. +/// Despawns the tiles if the event : [StartMapGeneration] is received. fn delete_map( mut commands: Commands, query: Query>, -- 2.43.4 From 6f061deb343189bcf7dc3ab70856f20c3b8936ee Mon Sep 17 00:00:00 2001 From: CoCoSol007 Date: Wed, 21 Feb 2024 11:42:22 +0100 Subject: [PATCH 20/37] fmt --- crates/border-wars/src/map/generation.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/crates/border-wars/src/map/generation.rs b/crates/border-wars/src/map/generation.rs index d466c1b..160a1c4 100644 --- a/crates/border-wars/src/map/generation.rs +++ b/crates/border-wars/src/map/generation.rs @@ -4,8 +4,7 @@ use bevy::prelude::*; use noise::{NoiseFn, Perlin}; use super::hex::*; -use super::Tile; -use super::TilePosition; +use super::{Tile, TilePosition}; /// A plugin to handle the map generation. pub struct MapGenerationPlugin; @@ -47,7 +46,7 @@ fn generate_map( // Handle map generation events. for event in event.read() { *local_noise = Some(Perlin::new(event.seed)); - *local_spiral = Some(TilePosition::new(0,0).spiral(event.radius as usize)); + *local_spiral = Some(TilePosition::new(0, 0).spiral(event.radius as usize)); } let (Some(noise), Some(spiral)) = (local_noise.as_ref(), local_spiral.as_mut()) else { -- 2.43.4 From 2f0875d60e650c9b7f92f8ce5532ee696fa9ae72 Mon Sep 17 00:00:00 2001 From: CoCoSol007 Date: Wed, 21 Feb 2024 11:50:47 +0100 Subject: [PATCH 21/37] fix clippy --- crates/border-wars/src/map/generation.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/border-wars/src/map/generation.rs b/crates/border-wars/src/map/generation.rs index af611c0..6e03333 100644 --- a/crates/border-wars/src/map/generation.rs +++ b/crates/border-wars/src/map/generation.rs @@ -54,7 +54,7 @@ fn generate_map( }; if let Some(position) = spiral.next() { let pixel_position = position.to_pixel_coordinates((0.2, 0.2)); - commands.spawn((get_type(pixel_position, &noise), position as TilePosition)); + commands.spawn((get_type(pixel_position, noise), position as TilePosition)); } else { end_map_event.send(EndMapGeneration); *local_noise = None; -- 2.43.4 From 83c36c704dbb34509fd2ea00adf0221c6008a4c5 Mon Sep 17 00:00:00 2001 From: CoCoSol007 Date: Wed, 21 Feb 2024 11:54:45 +0100 Subject: [PATCH 22/37] fix clippy --- crates/border-wars/src/map/hex.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/border-wars/src/map/hex.rs b/crates/border-wars/src/map/hex.rs index 3edb991..1dc4301 100644 --- a/crates/border-wars/src/map/hex.rs +++ b/crates/border-wars/src/map/hex.rs @@ -235,7 +235,7 @@ impl Iterator for HexSpiral { impl HexPosition { /// Creates a new [HexPosition]. - pub fn new(x: T, y: T) -> Self { + pub const fn new(x: T, y: T) -> Self { Self(x, y) } @@ -262,7 +262,7 @@ impl HexPosition { size.0 * 3f32 .sqrt() - .mul_add(T::to_f32(self.0), 3f32.sqrt() / 2.0 * T::to_f32(self.0)), + .mul_add(T::to_f32(self.0), 3f32.sqrt() / 2.0 * T::to_f32(self.1)), size.1 * (3.0 / 2.0 * T::to_f32(self.1)), ) } -- 2.43.4 From 0d9b7c0ce2d5c96f781d0234bbd812106fac5ffe Mon Sep 17 00:00:00 2001 From: CoCoSol007 Date: Wed, 21 Feb 2024 12:01:21 +0100 Subject: [PATCH 23/37] update --- crates/border-wars/src/map/generation.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/border-wars/src/map/generation.rs b/crates/border-wars/src/map/generation.rs index 6e03333..4a948a4 100644 --- a/crates/border-wars/src/map/generation.rs +++ b/crates/border-wars/src/map/generation.rs @@ -1,4 +1,4 @@ -//! All fonctions related to the generation of the map. +//! All functions related to the generation of the map. use bevy::prelude::*; use noise::{NoiseFn, Perlin}; -- 2.43.4 From 3cf5d76d00d8eaf2470ed2f04ac3dc25d5bda72b Mon Sep 17 00:00:00 2001 From: CoCoSol007 Date: Wed, 21 Feb 2024 17:42:20 +0100 Subject: [PATCH 24/37] update --- crates/border-wars/src/map/generation.rs | 27 ++++++++++++++++-------- crates/border-wars/src/map/hex.rs | 18 ++++++---------- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/crates/border-wars/src/map/generation.rs b/crates/border-wars/src/map/generation.rs index 4a948a4..4239923 100644 --- a/crates/border-wars/src/map/generation.rs +++ b/crates/border-wars/src/map/generation.rs @@ -35,36 +35,45 @@ pub struct StartMapGeneration { #[derive(Event)] pub struct EndMapGeneration; -/// Spawns the tiles if the event is received. +/// Generate each tiles of the map if the [StartMapGeneration] is received. +/// +/// The map is generated using the [Perlin] noise function and the [HexSpiral]. +/// +/// It's generated one tile at a time, until the spiral is finished. fn generate_map( - mut event: EventReader, - mut end_map_event: EventWriter, + mut start_generation_events: EventReader, + mut end_generation_writer: EventWriter, mut commands: Commands, mut local_noise: Local>, mut local_spiral: Local>>, ) { - // Handle map generation events. - for event in event.read() { + // Handle map generation events and create the spiral and the noise. + for event in start_generation_events.read() { *local_noise = Some(Perlin::new(event.seed)); *local_spiral = Some(TilePosition::new(0, 0).spiral(event.radius as usize)); } + // Check if the map is being generated. let (Some(noise), Some(spiral)) = (local_noise.as_ref(), local_spiral.as_mut()) else { return; }; + + // Spawn a tile until the spiral is finished + // If the map is generated, we send [EndMapGeneration] and set the local + // variables to None. if let Some(position) = spiral.next() { - let pixel_position = position.to_pixel_coordinates((0.2, 0.2)); + let pixel_position = position.to_pixel_coordinates() * 0.2; commands.spawn((get_type(pixel_position, noise), position as TilePosition)); } else { - end_map_event.send(EndMapGeneration); + end_generation_writer.send(EndMapGeneration); *local_noise = None; *local_spiral = None; } } /// Returns the type of the position with the given noise. -fn get_type(position: (f32, f32), noise: &Perlin) -> Tile { - let value = noise.get([position.0 as f64, position.1 as f64]); +fn get_type(position: Vec2, noise: &Perlin) -> Tile { + let value = noise.get([position.x as f64, position.y as f64]); match value { v if v <= -0.4 => Tile::Hill, v if v >= 0.4 => Tile::Forest, diff --git a/crates/border-wars/src/map/hex.rs b/crates/border-wars/src/map/hex.rs index 1dc4301..11d2eca 100644 --- a/crates/border-wars/src/map/hex.rs +++ b/crates/border-wars/src/map/hex.rs @@ -240,7 +240,6 @@ impl HexPosition { } /// Converts the current [HexPosition] into a pixel coordinate. - /// Input: The size of the hexagon in pixels (witdh, height). /// /// If you want to learn more about pixel coordinates conversion, /// you can check the @@ -252,18 +251,13 @@ impl HexPosition { /// use border_wars::map::hex::HexPosition; /// /// let position = HexPosition(1, 0); - /// assert_eq!( - /// position.to_pixel_coordinates((1.0, 1.0)), - /// (3f32.sqrt(), 0.0) - /// ); + /// assert_eq!(position.to_pixel_coordinates(), (3f32.sqrt(), 0.0)); /// ``` - pub fn to_pixel_coordinates(&self, size: (f32, f32)) -> (f32, f32) { - ( - size.0 - * 3f32 - .sqrt() - .mul_add(T::to_f32(self.0), 3f32.sqrt() / 2.0 * T::to_f32(self.1)), - size.1 * (3.0 / 2.0 * T::to_f32(self.1)), + pub fn to_pixel_coordinates(&self) -> Vec2 { + Vec2::new( + 3f32.sqrt() + .mul_add(T::to_f32(self.0), 3f32.sqrt() / 2.0 * T::to_f32(self.1)), + 3.0 / 2.0 * T::to_f32(self.1), ) } -- 2.43.4 From d64d0bb25815862d0a418b12f1ee76ea8882b500 Mon Sep 17 00:00:00 2001 From: CoCoSol007 Date: Wed, 21 Feb 2024 17:45:08 +0100 Subject: [PATCH 25/37] update --- crates/border-wars/src/map/hex.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/border-wars/src/map/hex.rs b/crates/border-wars/src/map/hex.rs index 11d2eca..b31f29c 100644 --- a/crates/border-wars/src/map/hex.rs +++ b/crates/border-wars/src/map/hex.rs @@ -249,9 +249,10 @@ impl HexPosition { /// /// ```no_run /// use border_wars::map::hex::HexPosition; + /// use bevy::math::Vec2; /// /// let position = HexPosition(1, 0); - /// assert_eq!(position.to_pixel_coordinates(), (3f32.sqrt(), 0.0)); + /// assert_eq!(position.to_pixel_coordinates(), Vec2::new(3f32.sqrt(), 0.0)); /// ``` pub fn to_pixel_coordinates(&self) -> Vec2 { Vec2::new( -- 2.43.4 From 4633db96092d5f191a044ee89f2947256ab06f8d Mon Sep 17 00:00:00 2001 From: CoCoSol007 Date: Wed, 21 Feb 2024 17:48:00 +0100 Subject: [PATCH 26/37] update --- crates/border-wars/src/map/hex.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/border-wars/src/map/hex.rs b/crates/border-wars/src/map/hex.rs index b31f29c..7ffb02c 100644 --- a/crates/border-wars/src/map/hex.rs +++ b/crates/border-wars/src/map/hex.rs @@ -252,7 +252,7 @@ impl HexPosition { /// use bevy::math::Vec2; /// /// let position = HexPosition(1, 0); - /// assert_eq!(position.to_pixel_coordinates(), Vec2::new(3f32.sqrt(), 0.0)); + /// assert_eq!(position.to_pixel_coordinates(), (3f32.sqrt(), 0.0).into()); /// ``` pub fn to_pixel_coordinates(&self) -> Vec2 { Vec2::new( -- 2.43.4 From 4b1f7008fd6bbde61d069793b35ea6c94d719dad Mon Sep 17 00:00:00 2001 From: CoCoSol007 Date: Wed, 21 Feb 2024 17:49:20 +0100 Subject: [PATCH 27/37] fix fmt --- crates/border-wars/src/map/hex.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/border-wars/src/map/hex.rs b/crates/border-wars/src/map/hex.rs index 7ffb02c..415bdfc 100644 --- a/crates/border-wars/src/map/hex.rs +++ b/crates/border-wars/src/map/hex.rs @@ -248,8 +248,8 @@ impl HexPosition { /// # Example /// /// ```no_run - /// use border_wars::map::hex::HexPosition; /// use bevy::math::Vec2; + /// use border_wars::map::hex::HexPosition; /// /// let position = HexPosition(1, 0); /// assert_eq!(position.to_pixel_coordinates(), (3f32.sqrt(), 0.0).into()); -- 2.43.4 From 9e9eec3914e525f2f48af6cbc6fb0b1c88664782 Mon Sep 17 00:00:00 2001 From: CoCoSol007 Date: Wed, 21 Feb 2024 17:57:43 +0100 Subject: [PATCH 28/37] save --- crates/border-wars/src/map/generation.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/border-wars/src/map/generation.rs b/crates/border-wars/src/map/generation.rs index 4239923..dd263b3 100644 --- a/crates/border-wars/src/map/generation.rs +++ b/crates/border-wars/src/map/generation.rs @@ -85,9 +85,9 @@ fn get_type(position: Vec2, noise: &Perlin) -> Tile { fn delete_map( mut commands: Commands, query: Query>, - mut event: EventReader, + mut start_generation_events: EventReader, ) { - for _ in event.read() { + for _ in start_generation_events.read() { for entity in query.iter() { commands.entity(entity).despawn_recursive(); } -- 2.43.4 From c11a797d38f8655cdf3432c48f576dcef511af50 Mon Sep 17 00:00:00 2001 From: CoCoSol007 Date: Wed, 21 Feb 2024 18:37:47 +0100 Subject: [PATCH 29/37] save --- crates/border-wars/src/map/generation.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/crates/border-wars/src/map/generation.rs b/crates/border-wars/src/map/generation.rs index dd263b3..9587d74 100644 --- a/crates/border-wars/src/map/generation.rs +++ b/crates/border-wars/src/map/generation.rs @@ -9,13 +9,15 @@ use super::{Tile, TilePosition}; /// A plugin to handle the map generation. pub struct MapGenerationPlugin; +const MAP_GENERATION_ZOOM: f32 = 0.2; + impl Plugin for MapGenerationPlugin { fn build(&self, app: &mut App) { app.add_event::() .add_event::() .add_systems( Update, - (generate_map.after(delete_map), delete_map) + (delete_map, generate_map.after(delete_map)) .run_if(in_state(crate::CurrentScene::Game)), ); } @@ -62,8 +64,7 @@ fn generate_map( // If the map is generated, we send [EndMapGeneration] and set the local // variables to None. if let Some(position) = spiral.next() { - let pixel_position = position.to_pixel_coordinates() * 0.2; - commands.spawn((get_type(pixel_position, noise), position as TilePosition)); + commands.spawn((get_type_tile(position, noise), position as TilePosition)); } else { end_generation_writer.send(EndMapGeneration); *local_noise = None; @@ -72,8 +73,9 @@ fn generate_map( } /// Returns the type of the position with the given noise. -fn get_type(position: Vec2, noise: &Perlin) -> Tile { - let value = noise.get([position.x as f64, position.y as f64]); +fn get_type_tile(position: HexPosition, noise: &Perlin) -> Tile { + let pixel_position = position.to_pixel_coordinates() * MAP_GENERATION_ZOOM; + let value = noise.get([pixel_position.x as f64, pixel_position.y as f64]); match value { v if v <= -0.4 => Tile::Hill, v if v >= 0.4 => Tile::Forest, @@ -81,7 +83,7 @@ fn get_type(position: Vec2, noise: &Perlin) -> Tile { } } -/// Despawns the tiles if the event : [StartMapGeneration] is received. +/// Despawns the tiles if the event [StartMapGeneration] is received. fn delete_map( mut commands: Commands, query: Query>, -- 2.43.4 From 62eacf2c84fea64fa853f484b88f6ecb6fb6b63b Mon Sep 17 00:00:00 2001 From: CoCoSol007 Date: Wed, 21 Feb 2024 18:38:36 +0100 Subject: [PATCH 30/37] add doc --- crates/border-wars/src/map/generation.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/border-wars/src/map/generation.rs b/crates/border-wars/src/map/generation.rs index 9587d74..2de36e5 100644 --- a/crates/border-wars/src/map/generation.rs +++ b/crates/border-wars/src/map/generation.rs @@ -9,6 +9,7 @@ use super::{Tile, TilePosition}; /// A plugin to handle the map generation. pub struct MapGenerationPlugin; +/// The zoom of the map during the generation. const MAP_GENERATION_ZOOM: f32 = 0.2; impl Plugin for MapGenerationPlugin { -- 2.43.4 From f184c938042da5cdad3feef009c749b09da1e8d8 Mon Sep 17 00:00:00 2001 From: CoCoSol007 Date: Wed, 21 Feb 2024 18:40:20 +0100 Subject: [PATCH 31/37] change doc --- crates/border-wars/src/map/generation.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/border-wars/src/map/generation.rs b/crates/border-wars/src/map/generation.rs index 2de36e5..610a895 100644 --- a/crates/border-wars/src/map/generation.rs +++ b/crates/border-wars/src/map/generation.rs @@ -73,7 +73,7 @@ fn generate_map( } } -/// Returns the type of the position with the given noise. +/// Returns the type of the [HexPosition] with the given noise. fn get_type_tile(position: HexPosition, noise: &Perlin) -> Tile { let pixel_position = position.to_pixel_coordinates() * MAP_GENERATION_ZOOM; let value = noise.get([pixel_position.x as f64, pixel_position.y as f64]); -- 2.43.4 From 7f255d081c97a72eca187212634310977ca343d2 Mon Sep 17 00:00:00 2001 From: CoCoSol007 Date: Wed, 21 Feb 2024 18:41:47 +0100 Subject: [PATCH 32/37] update --- crates/border-wars/src/map/generation.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/border-wars/src/map/generation.rs b/crates/border-wars/src/map/generation.rs index 610a895..2dd0824 100644 --- a/crates/border-wars/src/map/generation.rs +++ b/crates/border-wars/src/map/generation.rs @@ -10,7 +10,7 @@ use super::{Tile, TilePosition}; pub struct MapGenerationPlugin; /// The zoom of the map during the generation. -const MAP_GENERATION_ZOOM: f32 = 0.2; +const MAP_GENERATION_ZOOM: f32 = 5.; impl Plugin for MapGenerationPlugin { fn build(&self, app: &mut App) { @@ -75,7 +75,7 @@ fn generate_map( /// Returns the type of the [HexPosition] with the given noise. fn get_type_tile(position: HexPosition, noise: &Perlin) -> Tile { - let pixel_position = position.to_pixel_coordinates() * MAP_GENERATION_ZOOM; + let pixel_position = position.to_pixel_coordinates() / MAP_GENERATION_ZOOM; let value = noise.get([pixel_position.x as f64, pixel_position.y as f64]); match value { v if v <= -0.4 => Tile::Hill, -- 2.43.4 From 65e81d493b7468a0daaf0fe4dd49327fbeea8ef5 Mon Sep 17 00:00:00 2001 From: CoCoSol007 Date: Wed, 21 Feb 2024 18:47:28 +0100 Subject: [PATCH 33/37] change doc --- crates/border-wars/src/map/renderer.rs | 56 ++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 crates/border-wars/src/map/renderer.rs diff --git a/crates/border-wars/src/map/renderer.rs b/crates/border-wars/src/map/renderer.rs new file mode 100644 index 0000000..ad38cce --- /dev/null +++ b/crates/border-wars/src/map/renderer.rs @@ -0,0 +1,56 @@ +//! All functions related to the rendering of the map. + +use bevy::prelude::*; + +use super::Tile; +use crate::map::TilePosition; + +/// A plugin to render the map. +pub struct RendererPlugin; + +impl Plugin for RendererPlugin { + fn build(&self, app: &mut App) { + app.add_systems(Startup, setup_camera).add_systems( + Update, + render_map.run_if(in_state(crate::CurrentScene::Game)), + ); + } +} + +/// Sets up the camera. +fn setup_camera(mut commands: Commands) { + commands.spawn(Camera2dBundle::default()); +} + +impl Tile { + /// Returns the texture handle of the tile. + pub fn get_texture(&self, asset_server: &AssetServer) -> Handle { + match self { + Tile::Grass => asset_server.load("tiles/grass.png"), + Tile::Forest => asset_server.load("tiles/forest.png"), + Tile::Hill => asset_server.load("tiles/hill.png"), + } + } +} + +/// Renders the map. +fn render_map( + query: Query<(Entity, &TilePosition, &Tile), Changed>, + mut commands: Commands, + asset_server: Res, +) { + for (entity, position, tile) in query.iter() { + let pixel_position_ratio = position.to_pixel_coordinates(); + let pixel_position = (50. * pixel_position_ratio.x, pixel_position_ratio.y * 30.); + + commands.entity(entity).insert(SpriteBundle { + transform: Transform::from_translation(Vec3 { + x: pixel_position.0, + y: pixel_position.1, + z: 0., + }), + texture: tile.get_texture(&*asset_server), + ..default() + }); + } +} -- 2.43.4 From 0caf97d9b8b32ea44df2c306ecf8e1b89eec5aaf Mon Sep 17 00:00:00 2001 From: CoCoSol007 Date: Wed, 21 Feb 2024 18:49:45 +0100 Subject: [PATCH 34/37] change doc --- crates/border-wars/src/map/generation.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/border-wars/src/map/generation.rs b/crates/border-wars/src/map/generation.rs index 2dd0824..0869672 100644 --- a/crates/border-wars/src/map/generation.rs +++ b/crates/border-wars/src/map/generation.rs @@ -40,7 +40,7 @@ pub struct EndMapGeneration; /// Generate each tiles of the map if the [StartMapGeneration] is received. /// -/// The map is generated using the [Perlin] noise function and the [HexSpiral]. +/// The map is generated using a [Perlin] noise and a [HexSpiral]. /// /// It's generated one tile at a time, until the spiral is finished. fn generate_map( @@ -74,7 +74,7 @@ fn generate_map( } /// Returns the type of the [HexPosition] with the given noise. -fn get_type_tile(position: HexPosition, noise: &Perlin) -> Tile { +fn get_tile_type(position: HexPosition, noise: &Perlin) -> Tile { let pixel_position = position.to_pixel_coordinates() / MAP_GENERATION_ZOOM; let value = noise.get([pixel_position.x as f64, pixel_position.y as f64]); match value { -- 2.43.4 From b77061fed2c6c8fe8376ee38c722c79f2a39f38e Mon Sep 17 00:00:00 2001 From: CoCoSol007 Date: Wed, 21 Feb 2024 18:50:58 +0100 Subject: [PATCH 35/37] save --- crates/border-wars/src/map/generation.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/border-wars/src/map/generation.rs b/crates/border-wars/src/map/generation.rs index 0869672..0d40357 100644 --- a/crates/border-wars/src/map/generation.rs +++ b/crates/border-wars/src/map/generation.rs @@ -65,7 +65,7 @@ fn generate_map( // If the map is generated, we send [EndMapGeneration] and set the local // variables to None. if let Some(position) = spiral.next() { - commands.spawn((get_type_tile(position, noise), position as TilePosition)); + commands.spawn((get_tile_type(position, noise), position as TilePosition)); } else { end_generation_writer.send(EndMapGeneration); *local_noise = None; -- 2.43.4 From c34fc16753992e0264c496063585680e0759a4b4 Mon Sep 17 00:00:00 2001 From: Corentin Date: Wed, 21 Feb 2024 17:53:04 +0000 Subject: [PATCH 36/37] Delete crates/border-wars/src/map/renderer.rs --- crates/border-wars/src/map/renderer.rs | 56 -------------------------- 1 file changed, 56 deletions(-) delete mode 100644 crates/border-wars/src/map/renderer.rs diff --git a/crates/border-wars/src/map/renderer.rs b/crates/border-wars/src/map/renderer.rs deleted file mode 100644 index ad38cce..0000000 --- a/crates/border-wars/src/map/renderer.rs +++ /dev/null @@ -1,56 +0,0 @@ -//! All functions related to the rendering of the map. - -use bevy::prelude::*; - -use super::Tile; -use crate::map::TilePosition; - -/// A plugin to render the map. -pub struct RendererPlugin; - -impl Plugin for RendererPlugin { - fn build(&self, app: &mut App) { - app.add_systems(Startup, setup_camera).add_systems( - Update, - render_map.run_if(in_state(crate::CurrentScene::Game)), - ); - } -} - -/// Sets up the camera. -fn setup_camera(mut commands: Commands) { - commands.spawn(Camera2dBundle::default()); -} - -impl Tile { - /// Returns the texture handle of the tile. - pub fn get_texture(&self, asset_server: &AssetServer) -> Handle { - match self { - Tile::Grass => asset_server.load("tiles/grass.png"), - Tile::Forest => asset_server.load("tiles/forest.png"), - Tile::Hill => asset_server.load("tiles/hill.png"), - } - } -} - -/// Renders the map. -fn render_map( - query: Query<(Entity, &TilePosition, &Tile), Changed>, - mut commands: Commands, - asset_server: Res, -) { - for (entity, position, tile) in query.iter() { - let pixel_position_ratio = position.to_pixel_coordinates(); - let pixel_position = (50. * pixel_position_ratio.x, pixel_position_ratio.y * 30.); - - commands.entity(entity).insert(SpriteBundle { - transform: Transform::from_translation(Vec3 { - x: pixel_position.0, - y: pixel_position.1, - z: 0., - }), - texture: tile.get_texture(&*asset_server), - ..default() - }); - } -} -- 2.43.4 From 34557956e4b5e99a2ee50ef902142c768b63acfd Mon Sep 17 00:00:00 2001 From: CoCoSol007 Date: Wed, 21 Feb 2024 18:59:38 +0100 Subject: [PATCH 37/37] change name variable --- crates/border-wars/src/map/generation.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/border-wars/src/map/generation.rs b/crates/border-wars/src/map/generation.rs index 0d40357..f1ce9ee 100644 --- a/crates/border-wars/src/map/generation.rs +++ b/crates/border-wars/src/map/generation.rs @@ -10,7 +10,7 @@ use super::{Tile, TilePosition}; pub struct MapGenerationPlugin; /// The zoom of the map during the generation. -const MAP_GENERATION_ZOOM: f32 = 5.; +const MAP_GENERATION_SCALE: f32 = 5.; impl Plugin for MapGenerationPlugin { fn build(&self, app: &mut App) { @@ -75,7 +75,7 @@ fn generate_map( /// Returns the type of the [HexPosition] with the given noise. fn get_tile_type(position: HexPosition, noise: &Perlin) -> Tile { - let pixel_position = position.to_pixel_coordinates() / MAP_GENERATION_ZOOM; + let pixel_position = position.to_pixel_coordinates() / MAP_GENERATION_SCALE; let value = noise.get([pixel_position.x as f64, pixel_position.y as f64]); match value { v if v <= -0.4 => Tile::Hill, -- 2.43.4