Adding a map creation plugin #57
|
@ -11,8 +11,8 @@ pub struct MapGenerationPlugin;
|
||||||
|
|
||||||
impl Plugin for MapGenerationPlugin {
|
impl Plugin for MapGenerationPlugin {
|
||||||
fn build(&self, app: &mut App) {
|
fn build(&self, app: &mut App) {
|
||||||
app.add_event::<MapGenerationEvent>()
|
app.add_event::<MapGeneration>()
|
||||||
.add_event::<EndMapGenerationEvent>()
|
.add_event::<EndMapGeneration>()
|
||||||
.add_systems(
|
.add_systems(
|
||||||
Update,
|
Update,
|
||||||
(generate_map.after(delete_map), delete_map)
|
(generate_map.after(delete_map), delete_map)
|
||||||
|
@ -23,7 +23,7 @@ impl Plugin for MapGenerationPlugin {
|
||||||
|
|
||||||
/// An event to trigger the generation of the map.
|
/// An event to trigger the generation of the map.
|
||||||
#[derive(Event)]
|
#[derive(Event)]
|
||||||
pub struct MapGenerationEvent {
|
pub struct MapGeneration {
|
||||||
CoCo_Sol marked this conversation as resolved
Outdated
|
|||||||
/// The seed used to generate the map.
|
/// The seed used to generate the map.
|
||||||
pub seed: u32,
|
pub seed: u32,
|
||||||
|
|
||||||
|
@ -33,12 +33,12 @@ pub struct MapGenerationEvent {
|
||||||
|
|
||||||
/// An event send when the map is generated.
|
/// An event send when the map is generated.
|
||||||
#[derive(Event)]
|
#[derive(Event)]
|
||||||
pub struct EndMapGenerationEvent;
|
pub struct EndMapGeneration;
|
||||||
CoCo_Sol marked this conversation as resolved
Outdated
tipragot
commented
You should follow the bevy convention. Events in bevy doen't finish with "Event". You should follow the bevy convention. Events in bevy doen't finish with "Event".
|
|||||||
|
|
||||||
/// Spawns the tiles if the event is received.
|
/// Spawns the tiles if the event is received.
|
||||||
fn generate_map(
|
fn generate_map(
|
||||||
mut event: EventReader<MapGenerationEvent>,
|
mut event: EventReader<MapGeneration>,
|
||||||
mut end_map_event: EventWriter<EndMapGenerationEvent>,
|
mut end_map_event: EventWriter<EndMapGeneration>,
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
mut noise: Local<Option<Perlin>>,
|
mut noise: Local<Option<Perlin>>,
|
||||||
CoCo_Sol marked this conversation as resolved
Outdated
tipragot
commented
Perlin isn't a function Perlin isn't a function
|
|||||||
mut map_iterator: Local<Option<HexSpiral<isize>>>,
|
mut map_iterator: Local<Option<HexSpiral<isize>>>,
|
||||||
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
|
|||||||
|
@ -51,10 +51,10 @@ fn generate_map(
|
||||||
|
|
||||||
if let (Some(perlin), Some(spiral)) = (noise.as_ref(), map_iterator.as_mut()) {
|
if let (Some(perlin), Some(spiral)) = (noise.as_ref(), map_iterator.as_mut()) {
|
||||||
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(position) = spiral.next() {
|
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));
|
commands.spawn((get_type_tile(pixel_position, perlin), position));
|
||||||
} else {
|
} else {
|
||||||
end_map_event.send(EndMapGenerationEvent);
|
end_map_event.send(EndMapGeneration);
|
||||||
*noise = None;
|
*noise = None;
|
||||||
*map_iterator = None;
|
*map_iterator = None;
|
||||||
}
|
}
|
||||||
|
@ -65,17 +65,18 @@ fn generate_map(
|
||||||
fn get_type_tile(position: (f32, f32), noise: &Perlin) -> Tile {
|
fn get_type_tile(position: (f32, f32), noise: &Perlin) -> Tile {
|
||||||
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
|
|||||||
let value = noise.get([position.0 as f64, position.1 as f64]);
|
let value = noise.get([position.0 as f64, position.1 as f64]);
|
||||||
match value {
|
match value {
|
||||||
v if v <= -0.4 => Tile::Hill,
|
v if v <= -0.5 => Tile::Hill, // Réduire le seuil pour les collines
|
||||||
v if v >= 0.4 => Tile::Forest,
|
v if v >= 0.5 => Tile::Forest, // Réduire le seuil pour les forêts
|
||||||
_ => Tile::Grass,
|
_ => Tile::Grass,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
CoCo_Sol marked this conversation as resolved
Outdated
tipragot
commented
This comment is wrong. This comment is wrong.
|
|||||||
/// Despawns the tiles if the event : [EndMapGenerationEvent] is received.
|
/// Despawns the tiles if the event : [EndMapGenerationEvent] is received.
|
||||||
CoCo_Sol marked this conversation as resolved
Outdated
tipragot
commented
I still think you should take an HexPosition, this will be simpler and will make this function simpler to use. I still think you should take an HexPosition, this will be simpler and will make this function simpler to use.
|
|||||||
fn delete_map(
|
fn delete_map(
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
CoCo_Sol marked this conversation as resolved
Outdated
tipragot
commented
get_tile_type get_tile_type
|
|||||||
query: Query<Entity, With<Tile>>,
|
query: Query<Entity, With<Tile>>,
|
||||||
CoCo_Sol marked this conversation as resolved
Outdated
tipragot
commented
This is not a Zoom, it is a Scale This is not a Zoom, it is a Scale
|
|||||||
mut event: EventReader<MapGenerationEvent>,
|
mut event: EventReader<MapGeneration>,
|
||||||
) {
|
) {
|
||||||
for _ in event.read() {
|
for _ in event.read() {
|
||||||
for entity in query.iter() {
|
for entity in query.iter() {
|
||||||
|
|
If the End event start with "End" why this don't start with "Start" ?