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), ) }