From 982bce7b621d9de106846ded64d87013de9ab32b Mon Sep 17 00:00:00 2001 From: CoCoSol007 Date: Thu, 15 Feb 2024 13:13:20 +0100 Subject: [PATCH] Add temp renderer --- crates/border-wars/src/main.rs | 2 + crates/border-wars/src/map/generation.rs | 18 +++++--- crates/border-wars/src/map/mod.rs | 3 +- crates/border-wars/src/map/renderer.rs | 52 ++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 6 deletions(-) create mode 100644 crates/border-wars/src/map/renderer.rs diff --git a/crates/border-wars/src/main.rs b/crates/border-wars/src/main.rs index 0af1208..c2f9a1f 100644 --- a/crates/border-wars/src/main.rs +++ b/crates/border-wars/src/main.rs @@ -2,6 +2,7 @@ use bevy::prelude::*; use border_wars::map::generation::MapGenerationPlugin; +use border_wars::map::renderer::MapRendererPlugin; use border_wars::scenes::ScenesPlugin; fn main() { @@ -9,5 +10,6 @@ fn main() { .add_plugins(DefaultPlugins) .add_plugins(ScenesPlugin) .add_plugins(MapGenerationPlugin) + .add_plugins(MapRendererPlugin) .run(); } diff --git a/crates/border-wars/src/map/generation.rs b/crates/border-wars/src/map/generation.rs index 00f4d86..15f6fec 100644 --- a/crates/border-wars/src/map/generation.rs +++ b/crates/border-wars/src/map/generation.rs @@ -16,8 +16,9 @@ pub struct MapGenerationPlugin; impl Plugin for MapGenerationPlugin { fn build(&self, app: &mut App) { app.add_event::() - .add_systems(PostUpdate, generate_map_positions) - .add_systems(Last, generate_map_tiles); + .add_systems(Startup, test) + .add_systems(Update, generate_map_positions) + .add_systems(PostUpdate, generate_map_tiles); } } @@ -31,6 +32,13 @@ pub struct MapGenerationEvent { pub seed: u32, } +fn test(mut event: EventWriter) { + event.send(MapGenerationEvent { + range: 24, + seed: 10, + }); +} + /// 1. Generate the positions of the tiles. /// Create entity for each tiles and add there positions. fn generate_map_positions( @@ -63,12 +71,12 @@ fn generate_map_tiles( } /// Gets the type of the tile at the given position with a perlin noise fn get_type_tile(perlin: Perlin, position: HexPosition) -> TypeTile { - let pixel_position = position.to_pixel_coordinates((0.5, 0.5)); + let pixel_position = position.to_pixel_coordinates((0.1, 0.1)); let coordonate = [pixel_position.0 as f64, pixel_position.1 as f64]; let value = perlin.get(coordonate); match value { - v if v <= -0.3 => TypeTile::Hill, - v if v >= 0.3 => TypeTile::Forest, + v if v <= -0.4 => TypeTile::Hill, + v if v >= 0.4 => TypeTile::Forest, _ => TypeTile::Grass, } } diff --git a/crates/border-wars/src/map/mod.rs b/crates/border-wars/src/map/mod.rs index fcf879f..274d55b 100644 --- a/crates/border-wars/src/map/mod.rs +++ b/crates/border-wars/src/map/mod.rs @@ -4,13 +4,14 @@ use bevy::prelude::*; pub mod generation; pub mod hex; +pub mod renderer; /// TODO #[derive(Component)] pub struct Tile; /// TODO -#[derive(Component)] +#[derive(Component, Debug)] pub enum TypeTile { /// TODO Grass, diff --git a/crates/border-wars/src/map/renderer.rs b/crates/border-wars/src/map/renderer.rs new file mode 100644 index 0000000..b5f558c --- /dev/null +++ b/crates/border-wars/src/map/renderer.rs @@ -0,0 +1,52 @@ +//! TODO + +use bevy::prelude::*; +use bevy::sprite::MaterialMesh2dBundle; + +use self::hex::HexPosition; +use super::*; +use crate::CurrentScene; + +/// TODO +pub struct MapRendererPlugin; + +impl Plugin for MapRendererPlugin { + fn build(&self, app: &mut App) { + app.add_systems(Update, tiles_renderer.run_if(in_state(CurrentScene::Game))); + app.add_systems(Startup, setup); + } +} + +/// TODO +fn setup(mut commands: Commands) { + commands.spawn(Camera2dBundle { + transform: Transform::from_xyz(0., 0., 1.), + projection: OrthographicProjection { + scale: 10., + ..default() + }, + ..default() + }); +} + +fn tiles_renderer( + query: Query<(&HexPosition, &TypeTile), Changed>, + mut commands: Commands, + mut meshes: ResMut>, + mut materials: ResMut>, +) { + for (position, type_tile) in query.iter() { + let color = match type_tile { + TypeTile::Grass => Color::GREEN, + TypeTile::Forest => Color::DARK_GREEN, + TypeTile::Hill => Color::BISQUE, + }; + let pos = position.to_pixel_coordinates((50., 50.)); + commands.spawn(MaterialMesh2dBundle { + mesh: meshes.add(shape::RegularPolygon::new(50., 6).into()).into(), + material: materials.add(ColorMaterial::from(color)), + transform: Transform::from_translation(Vec3::new(pos.0, pos.1, 0.)), + ..default() + }); + } +}