Adding a map creation plugin #57
|
@ -5,6 +5,7 @@ use noise::{NoiseFn, Perlin};
|
||||||
|
|
||||||
use super::hex::*;
|
use super::hex::*;
|
||||||
use super::Tile;
|
use super::Tile;
|
||||||
|
use super::TilePosition;
|
||||||
|
|
||||||
/// A plugin to handle the map generation.
|
/// A plugin to handle the map generation.
|
||||||
pub struct MapGenerationPlugin;
|
pub struct MapGenerationPlugin;
|
||||||
|
@ -41,23 +42,24 @@ fn generate_map(
|
||||||
mut end_map_event: EventWriter<EndMapGeneration>,
|
mut end_map_event: EventWriter<EndMapGeneration>,
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
CoCo_Sol marked this conversation as resolved
Outdated
|
|||||||
mut noise: Local<Option<Perlin>>,
|
mut noise: Local<Option<Perlin>>,
|
||||||
CoCo_Sol marked this conversation as resolved
Outdated
tipragot
commented
Isize will not be great to use for networking because it's length depend on the system that runs the program. Isize will not be great to use for networking because it's length depend on the system that runs the program.
Also you should use `type` to alias a specific type for the HexPosition used for the map.
CoCo_Sol
commented
I use only one time the hex position, is it really better to create a new type ? I use only one time the hex position, is it really better to create a new type ?
tipragot
commented
You will use it every time you want to make a Query on the HexPosition You will use it every time you want to make a Query on the HexPosition
CoCo_Sol
commented
ok, you've convinced me ok, you've convinced me
|
|||||||
mut map_iterator: Local<Option<HexSpiral<i32>>>,
|
mut spiral: Local<Option<HexSpiral<i32>>>,
|
||||||
) {
|
) {
|
||||||
// Handle map generation events.
|
// Handle map generation events.
|
||||||
for event in event.read() {
|
for event in event.read() {
|
||||||
*noise = Some(Perlin::new(event.seed));
|
*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));
|
||||||
}
|
}
|
||||||
|
|
||||||
CoCo_Sol marked this conversation as resolved
Outdated
tipragot
commented
You can use the let else syntax to remove one level of nesting. You can use the let else syntax to remove one level of nesting.
CoCo_Sol
commented
if I use the "else let" keyword, the nesting level is the same if I use the "else let" keyword, the nesting level is the same
tipragot
commented
No because you do an early return No because you do an early return
CoCo_Sol
commented
I have to write an unwrap so ? I have to write an unwrap so ?
tipragot
commented
No, an let else No, an let else
|
|||||||
if let (Some(perlin), Some(spiral)) = (noise.as_ref(), map_iterator.as_mut()) {
|
if let (None, None) = (noise.as_ref(), spiral.as_mut()) {
|
||||||
if let Some(position) = spiral.next() {
|
return;
|
||||||
|
}
|
||||||
|
if let Some(position) = spiral.as_mut().unwrap().next() {
|
||||||
let pixel_position = position.to_pixel_coordinates((0.2, 0.2));
|
let pixel_position = position.to_pixel_coordinates((0.2, 0.2));
|
||||||
commands.spawn((get_type(pixel_position, perlin), position));
|
commands.spawn((get_type(pixel_position, &noise.unwrap()), position as TilePosition));
|
||||||
} else {
|
} else {
|
||||||
end_map_event.send(EndMapGeneration);
|
end_map_event.send(EndMapGeneration);
|
||||||
*noise = None;
|
*noise = None;
|
||||||
*map_iterator = None;
|
*spiral = None;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
CoCo_Sol marked this conversation as resolved
Outdated
tipragot
commented
Why don't you use HexPosition here? Why don't you use HexPosition here?
CoCo_Sol
commented
because is not a hex position in a hex grid but a position in orthogonal grid because is not a hex position in a hex grid but a position in orthogonal grid
tipragot
commented
You need to change the name of the function or take an HexPosition and convert it in the function, because it can be confusing that you use "tile" to describe the orthogonal position of an HexPosition You need to change the name of the function or take an HexPosition and convert it in the function, because it can be confusing that you use "tile" to describe the orthogonal position of an HexPosition
CoCo_Sol
commented
you are right you are right
CoCo_Sol marked this conversation as resolved
tipragot
commented
This should be in the get_type function and the value shouldn't be hard coded This should be in the get_type function and the value shouldn't be hard coded
|
|||||||
|
|
|
@ -234,6 +234,11 @@ impl<T: Number> Iterator for HexSpiral<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Number> HexPosition<T> {
|
impl<T: Number> HexPosition<T> {
|
||||||
|
/// Creates a new [HexPosition].
|
||||||
|
pub fn new(x: T, y: T) -> Self {
|
||||||
|
Self(x, y)
|
||||||
|
}
|
||||||
|
|
||||||
/// Converts the current [HexPosition] into a pixel coordinate.
|
/// Converts the current [HexPosition] into a pixel coordinate.
|
||||||
/// Input: The size of the hexagon in pixels (witdh, height).
|
/// Input: The size of the hexagon in pixels (witdh, height).
|
||||||
///
|
///
|
||||||
|
|
|
@ -5,8 +5,13 @@ pub mod hex;
|
||||||
|
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
|
||||||
|
use self::hex::*;
|
||||||
|
|
||||||
|
/// The position of a tile in a hexagonal map.
|
||||||
|
pub type TilePosition = HexPosition<i32>;
|
||||||
|
|
||||||
/// The tile of the map.
|
/// The tile of the map.
|
||||||
#[derive(Component)]
|
#[derive(Component, Debug)]
|
||||||
pub enum Tile {
|
pub enum Tile {
|
||||||
/// The hill tile.
|
/// The hill tile.
|
||||||
Hill,
|
Hill,
|
||||||
|
|
Perlin isn't a function