Add temp renderer
Some checks failed
Rust Checks / checks (push) Failing after 1m45s

This commit is contained in:
CoCo_Sol 2024-02-15 13:13:20 +01:00
parent 178cf417cf
commit 982bce7b62
4 changed files with 69 additions and 6 deletions

View file

@ -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();
}

View file

@ -16,8 +16,9 @@ pub struct MapGenerationPlugin;
impl Plugin for MapGenerationPlugin {
fn build(&self, app: &mut App) {
app.add_event::<MapGenerationEvent>()
.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<MapGenerationEvent>) {
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<i32>) -> 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,
}
}

View file

@ -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,

View file

@ -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<i32>, &TypeTile), Changed<TypeTile>>,
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
) {
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()
});
}
}