generated from tipragot/rust
Adding a map creation plugin #57
|
@ -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
Outdated
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
|
||||
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(
|
|||
}
|
||||
|
||||
CoCo_Sol marked this conversation as resolved
Outdated
tipragot
commented
This comment is wrong. This comment is wrong.
|
||||
/// Returns the type of the position with the given noise.
|
||||
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 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;
|
||||
CoCo_Sol marked this conversation as resolved
Outdated
tipragot
commented
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
tipragot
commented
This is not a Zoom, it is a Scale This is not a Zoom, it is a Scale
|
||||
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 {
|
|||
}
|
||||
}
|
||||
CoCo_Sol marked this conversation as resolved
Outdated
tipragot
commented
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(
|
||||
mut commands: Commands,
|
||||
query: Query<Entity, With<Tile>>,
|
||||
|
|
Loading…
Reference in a new issue
Why don't you use HexPosition here?
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 are right