Adding a map creation plugin #57

Merged
CoCo_Sol merged 39 commits from map-generation into main 2024-02-21 20:10:03 +00:00
Showing only changes of commit c11a797d38 - Show all commits

View file

@ -9,13 +9,15 @@ use super::{Tile, TilePosition};
/// A plugin to handle the map generation. /// A plugin to handle the map generation.
pub struct MapGenerationPlugin; pub struct MapGenerationPlugin;
const MAP_GENERATION_ZOOM: f32 = 0.2;
impl Plugin for MapGenerationPlugin { impl Plugin for MapGenerationPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_event::<StartMapGeneration>() app.add_event::<StartMapGeneration>()
.add_event::<EndMapGeneration>() .add_event::<EndMapGeneration>()
.add_systems( .add_systems(
Update, Update,
(generate_map.after(delete_map), delete_map) (delete_map, generate_map.after(delete_map))
.run_if(in_state(crate::CurrentScene::Game)), .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 // If the map is generated, we send [EndMapGeneration] and set the local
// variables to None. // variables to None.
CoCo_Sol marked this conversation as resolved Outdated

Why don't you use HexPosition here?

Why don't you use HexPosition here?

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

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

you are right

you are right
CoCo_Sol marked this conversation as resolved
Review

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
if let Some(position) = spiral.next() { if let Some(position) = spiral.next() {
let pixel_position = position.to_pixel_coordinates() * 0.2; commands.spawn((get_type_tile(position, noise), position as TilePosition));
commands.spawn((get_type(pixel_position, noise), position as TilePosition));
} else { } else {
end_generation_writer.send(EndMapGeneration); end_generation_writer.send(EndMapGeneration);
*local_noise = None; *local_noise = None;
@ -72,8 +73,9 @@ fn generate_map(
} }
CoCo_Sol marked this conversation as resolved Outdated

This comment is wrong.

This comment is wrong.
/// Returns the type of the position with the given noise. /// Returns the type of the position with the given noise.
CoCo_Sol marked this conversation as resolved Outdated

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 get_type(position: Vec2, noise: &Perlin) -> Tile { fn get_type_tile(position: HexPosition<i32>, noise: &Perlin) -> Tile {
let value = noise.get([position.x as f64, position.y as f64]); let pixel_position = position.to_pixel_coordinates() * MAP_GENERATION_ZOOM;
CoCo_Sol marked this conversation as resolved Outdated

get_tile_type

get_tile_type
let value = noise.get([pixel_position.x as f64, pixel_position.y as f64]);
CoCo_Sol marked this conversation as resolved Outdated

This is not a Zoom, it is a Scale

This is not a Zoom, it is a Scale
match value { match value {
v if v <= -0.4 => Tile::Hill, v if v <= -0.4 => Tile::Hill,
v if v >= 0.4 => Tile::Forest, v if v >= 0.4 => Tile::Forest,
@ -81,7 +83,7 @@ fn get_type(position: Vec2, noise: &Perlin) -> Tile {
} }
} }
CoCo_Sol marked this conversation as resolved Outdated

What this ":" doing here ?

What this ":" doing here ?
/// Despawns the tiles if the event : [StartMapGeneration] is received. /// Despawns the tiles if the event [StartMapGeneration] is received.
fn delete_map( fn delete_map(
mut commands: Commands, mut commands: Commands,
query: Query<Entity, With<Tile>>, query: Query<Entity, With<Tile>>,