generated from tipragot/rust
Add a renderer system with temp assets #60
|
@ -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.
|
||||||
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(
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the type of the position with the given noise.
|
/// Returns the type of the position with the given noise.
|
||||||
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;
|
||||||
|
let value = noise.get([pixel_position.x as f64, pixel_position.y as f64]);
|
||||||
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 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 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>>,
|
||||||
|
|
Loading…
Reference in a new issue