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,