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.
pub struct MapGenerationPlugin;
const MAP_GENERATION_ZOOM: f32 = 0.2;
impl Plugin for MapGenerationPlugin {
fn build(&self, app: &mut App) {
app.add_event::<StartMapGeneration>()
.add_event::<EndMapGeneration>()
.add_systems(
Update,
(generate_map.after(delete_map), delete_map)
(delete_map, generate_map.after(delete_map))
.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
// variables to None.
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() {
let pixel_position = position.to_pixel_coordinates() * 0.2;
commands.spawn((get_type(pixel_position, noise), position as TilePosition));
commands.spawn((get_type_tile(position, noise), position as TilePosition));
} else {
end_generation_writer.send(EndMapGeneration);
*local_noise = None;
@ -72,8 +73,9 @@ fn generate_map(
}
/// Returns the type of the position with the given noise.
fn get_type(position: Vec2, noise: &Perlin) -> Tile {
let value = noise.get([position.x as f64, position.y as f64]);
fn get_type_tile(position: HexPosition<i32>, noise: &Perlin) -> Tile {
let pixel_position = position.to_pixel_coordinates() * MAP_GENERATION_ZOOM;
let value = noise.get([pixel_position.x as f64, pixel_position.y as f64]);
match value {
v if v <= -0.4 => Tile::Hill,
v if v >= 0.4 => Tile::Forest,
@ -81,7 +83,7 @@ fn get_type(position: Vec2, noise: &Perlin) -> Tile {
}
}
/// Despawns the tiles if the event : [StartMapGeneration] is received.
/// Despawns the tiles if the event [StartMapGeneration] is received.
fn delete_map(
mut commands: Commands,
query: Query<Entity, With<Tile>>,