Adding a map creation plugin #57
103
Cargo.lock
generated
|
@ -93,7 +93,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
checksum = "77c3a9648d43b9cd48db467b3f87fdd6e146bcc88ab0180006cef2179fe11d01"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"getrandom",
|
||||
"getrandom 0.2.12",
|
||||
"once_cell",
|
||||
"version_check",
|
||||
"zerocopy",
|
||||
|
@ -1126,7 +1126,7 @@ checksum = "7915222f4a08ccc782e08d10b751b42e5f9d786e697d0cb3fd09333cb7e8b6ea"
|
|||
dependencies = [
|
||||
"ahash",
|
||||
"bevy_utils_proc_macros",
|
||||
"getrandom",
|
||||
"getrandom 0.2.12",
|
||||
"hashbrown 0.14.3",
|
||||
"instant",
|
||||
"nonmax",
|
||||
|
@ -1316,6 +1316,7 @@ version = "0.1.0"
|
|||
dependencies = [
|
||||
"bevy",
|
||||
"bevy_egui",
|
||||
"noise",
|
||||
"num",
|
||||
"partial-min-max",
|
||||
"paste",
|
||||
|
@ -2108,6 +2109,17 @@ dependencies = [
|
|||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "getrandom"
|
||||
version = "0.1.16"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"wasi 0.9.0+wasi-snapshot-preview1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "getrandom"
|
||||
version = "0.2.12"
|
||||
|
@ -2117,7 +2129,7 @@ dependencies = [
|
|||
"cfg-if",
|
||||
"js-sys",
|
||||
"libc",
|
||||
"wasi",
|
||||
"wasi 0.11.0+wasi-snapshot-preview1",
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
|
@ -2847,7 +2859,7 @@ checksum = "8f3d0b296e374a4e6f3c7b0a1f5a51d748a0d34c85e7dc48fc3fa9a87657fe09"
|
|||
dependencies = [
|
||||
"libc",
|
||||
"log",
|
||||
"wasi",
|
||||
"wasi 0.11.0+wasi-snapshot-preview1",
|
||||
"windows-sys 0.48.0",
|
||||
]
|
||||
|
||||
|
@ -2961,6 +2973,17 @@ version = "0.2.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2bf50223579dc7cdcfb3bfcacf7069ff68243f8c363f62ffa99cf000a6b9c451"
|
||||
|
||||
[[package]]
|
||||
name = "noise"
|
||||
version = "0.8.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9ba869e17168793186c10ca82c7079a4ffdeac4f1a7d9e755b9491c028180e40"
|
||||
dependencies = [
|
||||
"num-traits",
|
||||
"rand 0.7.3",
|
||||
"rand_xorshift",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "nom"
|
||||
version = "7.1.3"
|
||||
|
@ -3475,6 +3498,19 @@ version = "0.1.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "17fd96390ed3feda12e1dfe2645ed587e0bea749e319333f104a33ff62f77a0b"
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.7.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03"
|
||||
dependencies = [
|
||||
"getrandom 0.1.16",
|
||||
"libc",
|
||||
"rand_chacha 0.2.2",
|
||||
"rand_core 0.5.1",
|
||||
"rand_hc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.8.5"
|
||||
|
@ -3482,8 +3518,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"rand_chacha",
|
||||
"rand_core",
|
||||
"rand_chacha 0.3.1",
|
||||
"rand_core 0.6.4",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_chacha"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402"
|
||||
dependencies = [
|
||||
"ppv-lite86",
|
||||
"rand_core 0.5.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -3493,7 +3539,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
|
||||
dependencies = [
|
||||
"ppv-lite86",
|
||||
"rand_core",
|
||||
"rand_core 0.6.4",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_core"
|
||||
version = "0.5.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19"
|
||||
dependencies = [
|
||||
"getrandom 0.1.16",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -3502,7 +3557,25 @@ version = "0.6.4"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
|
||||
dependencies = [
|
||||
"getrandom",
|
||||
"getrandom 0.2.12",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_hc"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c"
|
||||
dependencies = [
|
||||
"rand_core 0.5.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_xorshift"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "77d416b86801d23dde1aa643023b775c3a462efc0ed96443add11546cdf1dca8"
|
||||
dependencies = [
|
||||
"rand_core 0.5.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -3607,7 +3680,7 @@ dependencies = [
|
|||
"home",
|
||||
"log",
|
||||
"mio",
|
||||
"rand",
|
||||
"rand 0.8.5",
|
||||
"tungstenite",
|
||||
"uuid",
|
||||
]
|
||||
|
@ -3639,7 +3712,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
checksum = "688c63d65483050968b2a8937f7995f443e27041a0f7700aa59b0822aedebb74"
|
||||
dependencies = [
|
||||
"cc",
|
||||
"getrandom",
|
||||
"getrandom 0.2.12",
|
||||
"libc",
|
||||
"spin",
|
||||
"untrusted",
|
||||
|
@ -4342,7 +4415,7 @@ dependencies = [
|
|||
"http",
|
||||
"httparse",
|
||||
"log",
|
||||
"rand",
|
||||
"rand 0.8.5",
|
||||
"rustls",
|
||||
"rustls-native-certs",
|
||||
"rustls-pki-types",
|
||||
|
@ -4430,7 +4503,7 @@ version = "1.7.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f00cc9702ca12d3c81455259621e676d0f7251cec66a21e98fe2e9a37db93b2a"
|
||||
dependencies = [
|
||||
"getrandom",
|
||||
"getrandom 0.2.12",
|
||||
"serde",
|
||||
]
|
||||
|
||||
|
@ -4468,6 +4541,12 @@ dependencies = [
|
|||
"winapi-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasi"
|
||||
version = "0.9.0+wasi-snapshot-preview1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"
|
||||
|
||||
[[package]]
|
||||
name = "wasi"
|
||||
version = "0.11.0+wasi-snapshot-preview1"
|
||||
|
|
|
@ -13,6 +13,7 @@ workspace = true
|
|||
[dependencies]
|
||||
bevy = "0.12.1"
|
||||
bevy_egui = "0.24.0"
|
||||
noise = "0.8.2"
|
||||
num = "0.4.1"
|
||||
partial-min-max = "0.4.0"
|
||||
paste = "1.0.14"
|
||||
|
|
65
crates/border-wars/src/map/generation.rs
Normal file
|
@ -0,0 +1,65 @@
|
|||
//! All fonctions related to the generation of the map.
|
||||
|
||||
use bevy::prelude::*;
|
||||
use noise::{NoiseFn, Perlin};
|
||||
|
||||
use super::hex::*;
|
||||
use super::Tile;
|
||||
|
||||
/// A plugin to handle the map generation.
|
||||
pub struct MapGenerationPlugin;
|
||||
|
||||
impl Plugin for MapGenerationPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_event::<MapGenerationEvent>().add_systems(
|
||||
Update,
|
||||
generate_map.run_if(in_state(crate::CurrentScene::Game)),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// An event to trigger the generation of the map.
|
||||
#[derive(Event)]
|
||||
pub struct MapGenerationEvent {
|
||||
/// The seed used to generate the map.
|
||||
pub seed: u32,
|
||||
|
||||
CoCo_Sol marked this conversation as resolved
Outdated
|
||||
/// The radius of the map.
|
||||
pub radius: usize,
|
||||
}
|
||||
|
||||
/// spawns the tiles.
|
||||
CoCo_Sol marked this conversation as resolved
Outdated
tipragot
commented
Put an UPPERCASE "s" Put an UPPERCASE "s"
|
||||
fn generate_map(
|
||||
mut event: EventReader<MapGenerationEvent>,
|
||||
mut commands: Commands,
|
||||
mut noise: Local<Option<Perlin>>,
|
||||
mut map_iterator: Local<Option<HexSpiral<isize>>>,
|
||||
CoCo_Sol marked this conversation as resolved
Outdated
tipragot
commented
You should follow the bevy convention. Events in bevy doen't finish with "Event". You should follow the bevy convention. Events in bevy doen't finish with "Event".
|
||||
) {
|
||||
// Handle map generation events.
|
||||
for event in event.read() {
|
||||
*noise = Some(Perlin::new(event.seed));
|
||||
*map_iterator = Some(HexPosition(0, 0).spiral(event.radius));
|
||||
}
|
||||
|
||||
CoCo_Sol marked this conversation as resolved
Outdated
tipragot
commented
Perlin isn't a function Perlin isn't a function
|
||||
if let (Some(perlin), Some(spiral)) = (noise.as_ref(), map_iterator.as_mut()) {
|
||||
CoCo_Sol marked this conversation as resolved
Outdated
tipragot
commented
Isize will not be great to use for networking because it's length depend on the system that runs the program. Isize will not be great to use for networking because it's length depend on the system that runs the program.
Also you should use `type` to alias a specific type for the HexPosition used for the map.
CoCo_Sol
commented
I use only one time the hex position, is it really better to create a new type ? I use only one time the hex position, is it really better to create a new type ?
tipragot
commented
You will use it every time you want to make a Query on the HexPosition You will use it every time you want to make a Query on the HexPosition
CoCo_Sol
commented
ok, you've convinced me ok, you've convinced me
|
||||
if let Some(position) = spiral.next() {
|
||||
let pixel_position = position.to_pixel_coordinates((0.1, 0.1));
|
||||
let type_tile = get_type_tile(pixel_position, perlin);
|
||||
|
||||
commands.spawn((type_tile, position));
|
||||
} else {
|
||||
*noise = None;
|
||||
*map_iterator = None;
|
||||
CoCo_Sol marked this conversation as resolved
Outdated
tipragot
commented
You can use the let else syntax to remove one level of nesting. You can use the let else syntax to remove one level of nesting.
CoCo_Sol
commented
if I use the "else let" keyword, the nesting level is the same if I use the "else let" keyword, the nesting level is the same
tipragot
commented
No because you do an early return No because you do an early return
CoCo_Sol
commented
I have to write an unwrap so ? I have to write an unwrap so ?
tipragot
commented
No, an let else No, an let else
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the type of the tile at the given position with the given noise.
|
||||
fn get_type_tile(position: (f32, f32), noise: &Perlin) -> Tile {
|
||||
let value = noise.get([position.0 as f64, position.1 as f64]);
|
||||
match value {
|
||||
v if v <= -0.4 => Tile::Hill,
|
||||
v if v >= 0.4 => Tile::Forest,
|
||||
_ => Tile::Grass,
|
||||
}
|
||||
}
|
||||
CoCo_Sol marked this conversation as resolved
Outdated
tipragot
commented
Why don't you use HexPosition here? Why don't you use HexPosition here?
CoCo_Sol
commented
because is not a hex position in a hex grid but a position in orthogonal grid because is not a hex position in a hex grid but a position in orthogonal grid
tipragot
commented
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 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
CoCo_Sol
commented
you are right you are right
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
|
|
@ -4,6 +4,7 @@ use std::ops::{
|
|||
Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Rem, RemAssign, Sub, SubAssign,
|
||||
};
|
||||
|
||||
use bevy::prelude::*;
|
||||
use paste::paste;
|
||||
|
||||
/// Represents a number that can be used in calculations for hexagonal grids.
|
||||
|
@ -98,7 +99,7 @@ number_impl! {
|
|||
/// Represents a position in a hexagonal grid.
|
||||
/// We use the axial coordinate system explained in this
|
||||
/// [documentation](https://www.redblobgames.com/grids/hexagons/#coordinates).
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Component)]
|
||||
pub struct HexPosition<T: Number>(pub T, pub T);
|
||||
|
||||
/// All possible directions in a hexagonal grid.
|
||||
|
|
|
@ -1,3 +1,19 @@
|
|||
//! Contains all the logic related to the map.
|
||||
|
||||
pub mod generation;
|
||||
pub mod hex;
|
||||
|
||||
use bevy::prelude::*;
|
||||
|
||||
/// The tile of the map.
|
||||
#[derive(Component)]
|
||||
pub enum Tile {
|
||||
/// The hill tile.
|
||||
Hill,
|
||||
|
||||
/// The grass tile.
|
||||
Grass,
|
||||
|
||||
/// The forest tile.
|
||||
Forest,
|
||||
}
|
||||
|
|
If the End event start with "End" why this don't start with "Start" ?