generated from tipragot/rust
This commit is contained in:
parent
178cf417cf
commit
982bce7b62
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use border_wars::map::generation::MapGenerationPlugin;
|
use border_wars::map::generation::MapGenerationPlugin;
|
||||||
|
use border_wars::map::renderer::MapRendererPlugin;
|
||||||
use border_wars::scenes::ScenesPlugin;
|
use border_wars::scenes::ScenesPlugin;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
@ -9,5 +10,6 @@ fn main() {
|
||||||
.add_plugins(DefaultPlugins)
|
.add_plugins(DefaultPlugins)
|
||||||
.add_plugins(ScenesPlugin)
|
.add_plugins(ScenesPlugin)
|
||||||
.add_plugins(MapGenerationPlugin)
|
.add_plugins(MapGenerationPlugin)
|
||||||
|
.add_plugins(MapRendererPlugin)
|
||||||
.run();
|
.run();
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,8 +16,9 @@ pub struct MapGenerationPlugin;
|
||||||
impl Plugin for MapGenerationPlugin {
|
impl Plugin for MapGenerationPlugin {
|
||||||
fn build(&self, app: &mut App) {
|
fn build(&self, app: &mut App) {
|
||||||
app.add_event::<MapGenerationEvent>()
|
app.add_event::<MapGenerationEvent>()
|
||||||
.add_systems(PostUpdate, generate_map_positions)
|
.add_systems(Startup, test)
|
||||||
.add_systems(Last, generate_map_tiles);
|
.add_systems(Update, generate_map_positions)
|
||||||
|
.add_systems(PostUpdate, generate_map_tiles);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,6 +32,13 @@ pub struct MapGenerationEvent {
|
||||||
pub seed: u32,
|
pub seed: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn test(mut event: EventWriter<MapGenerationEvent>) {
|
||||||
|
event.send(MapGenerationEvent {
|
||||||
|
range: 24,
|
||||||
|
seed: 10,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/// 1. Generate the positions of the tiles.
|
/// 1. Generate the positions of the tiles.
|
||||||
/// Create entity for each tiles and add there positions.
|
/// Create entity for each tiles and add there positions.
|
||||||
fn generate_map_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
|
/// Gets the type of the tile at the given position with a perlin noise
|
||||||
fn get_type_tile(perlin: Perlin, position: HexPosition<i32>) -> TypeTile {
|
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 coordonate = [pixel_position.0 as f64, pixel_position.1 as f64];
|
||||||
let value = perlin.get(coordonate);
|
let value = perlin.get(coordonate);
|
||||||
match value {
|
match value {
|
||||||
v if v <= -0.3 => TypeTile::Hill,
|
v if v <= -0.4 => TypeTile::Hill,
|
||||||
v if v >= 0.3 => TypeTile::Forest,
|
v if v >= 0.4 => TypeTile::Forest,
|
||||||
_ => TypeTile::Grass,
|
_ => TypeTile::Grass,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,13 +4,14 @@ use bevy::prelude::*;
|
||||||
|
|
||||||
pub mod generation;
|
pub mod generation;
|
||||||
pub mod hex;
|
pub mod hex;
|
||||||
|
pub mod renderer;
|
||||||
|
|
||||||
/// TODO
|
/// TODO
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub struct Tile;
|
pub struct Tile;
|
||||||
|
|
||||||
/// TODO
|
/// TODO
|
||||||
#[derive(Component)]
|
#[derive(Component, Debug)]
|
||||||
pub enum TypeTile {
|
pub enum TypeTile {
|
||||||
/// TODO
|
/// TODO
|
||||||
Grass,
|
Grass,
|
||||||
|
|
52
crates/border-wars/src/map/renderer.rs
Normal file
52
crates/border-wars/src/map/renderer.rs
Normal 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()
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue