From ac1fb70b3280696931dfe985d4f27f4345b0818c Mon Sep 17 00:00:00 2001 From: CoCoSol007 Date: Sun, 11 Feb 2024 22:40:02 +0100 Subject: [PATCH] WIP: Save --- Cargo.lock | 108 ++++++++++++++++++++++--- crates/border-wars/Cargo.toml | 2 + crates/border-wars/src/main.rs | 9 +-- crates/border-wars/src/map.rs | 143 +++++++++++++++++++++++++++++++++ 4 files changed, 246 insertions(+), 16 deletions(-) create mode 100644 crates/border-wars/src/map.rs diff --git a/Cargo.lock b/Cargo.lock index cb89c3f..e55164c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -128,7 +128,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", @@ -1096,7 +1096,7 @@ checksum = "7915222f4a08ccc782e08d10b751b42e5f9d786e697d0cb3fd09333cb7e8b6ea" dependencies = [ "ahash", "bevy_utils_proc_macros", - "getrandom", + "getrandom 0.2.12", "hashbrown 0.14.3", "instant", "nonmax", @@ -1268,6 +1268,8 @@ version = "0.1.0" dependencies = [ "bevy", "bevy_egui", + "noise", + "round", ] [[package]] @@ -1580,7 +1582,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" dependencies = [ "generic-array", - "rand_core", + "rand_core 0.6.4", "typenum", ] @@ -1949,6 +1951,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" @@ -1958,7 +1971,7 @@ dependencies = [ "cfg-if", "js-sys", "libc", - "wasi", + "wasi 0.11.0+wasi-snapshot-preview1", "wasm-bindgen", ] @@ -2241,7 +2254,7 @@ checksum = "556b5a75cd4adb7c4ea21c64af1c48cefb2ce7d43dc4352c720a1fe47c21f355" dependencies = [ "attohttpc", "log", - "rand", + "rand 0.8.5", "url", "xmltree", ] @@ -2625,7 +2638,7 @@ checksum = "8f3d0b296e374a4e6f3c7b0a1f5a51d748a0d34c85e7dc48fc3fa9a87657fe09" dependencies = [ "libc", "log", - "wasi", + "wasi 0.11.0+wasi-snapshot-preview1", "windows-sys 0.48.0", ] @@ -2764,6 +2777,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" @@ -3157,6 +3181,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" @@ -3164,8 +3201,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]] @@ -3175,7 +3222,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]] @@ -3184,7 +3240,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]] @@ -3301,6 +3375,12 @@ dependencies = [ "serde_derive", ] +[[package]] +name = "round" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b02705db4fa872ae37e464fc803b7ffa574e6c4a5e112c52a82550f9dd63b657" + [[package]] name = "rustc-demangle" version = "0.1.23" @@ -3793,7 +3873,7 @@ version = "1.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f00cc9702ca12d3c81455259621e676d0f7251cec66a21e98fe2e9a37db93b2a" dependencies = [ - "getrandom", + "getrandom 0.2.12", "serde", ] @@ -3831,6 +3911,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" diff --git a/crates/border-wars/Cargo.toml b/crates/border-wars/Cargo.toml index 1c977be..f19e9ae 100644 --- a/crates/border-wars/Cargo.toml +++ b/crates/border-wars/Cargo.toml @@ -12,3 +12,5 @@ workspace = true [dependencies] bevy = "0.12.1" bevy_egui = "0.24.0" +noise = "0.8.2" +round = "0.1.2" diff --git a/crates/border-wars/src/main.rs b/crates/border-wars/src/main.rs index ef67c5e..2f333fc 100644 --- a/crates/border-wars/src/main.rs +++ b/crates/border-wars/src/main.rs @@ -1,11 +1,10 @@ //! The main entry point of the game. use bevy::prelude::*; -use border_wars::scenes::ScenesPlugin; +// use border_wars::scenes::ScenesPlugin; fn main() { - App::new() - .add_plugins(DefaultPlugins) - .add_plugins(ScenesPlugin) - .run(); + App::new().run(); + //.add_plugins(DefaultPlugins) + //.add_plugins(ScenesPlugin) } diff --git a/crates/border-wars/src/map.rs b/crates/border-wars/src/map.rs new file mode 100644 index 0000000..46d1f43 --- /dev/null +++ b/crates/border-wars/src/map.rs @@ -0,0 +1,143 @@ +//! Maps program for the game. + +use round::round_down; +use bevy::ecs::component; +use noise::{NoiseFn, Perlin}; + + +#[derive(Clone, Copy, component::Component, Debug, PartialEq, Eq)] +enum Tile { + Forest, + Hill, + Grass, +} + +#[derive(component::Component, Debug)] +struct Position(f64, f64); + +#[derive(component::Component)] +struct AdjacentCases { + entities: Vec, +} + +fn generate_map_coordinates(size: i32) -> Vec> { + let mut all_tiles = Vec::new(); + // Iterate over horizontal lines of the map + for y in 0..size * 2 - 1 { + // Calculate the size of the horizontal line based on the vertical position + let x_size = size + y.min(size * 2 - y - 2); + + // Initialize a vector to store tile coordinates + let mut tiles = Vec::with_capacity(x_size.abs() as usize); + + // Calculate half of the size of the horizontal line + let size_half = x_size as f64 / 2.0; + + // Calculate the offset to center the tiles + let offset = 0.5 * ((x_size + 1) as f64 % 2.0); + + // Iterate over tiles on the horizontal line + for x in 0..x_size.abs() { + // Calculate tile coordinates and add to the vector + let pos = ( + x as f64 + offset - round_down(size_half, 0), + (size - y - 1) as f64 * 0.75_f64.sqrt(), + ); + + tiles.push(pos); + } + + // Add tiles of the line to the vector of all tiles + all_tiles.push(tiles); + } + all_tiles +} + +/// Get the adjacent entities of an entity in the map. +fn get_adjacent_entities(map: &Vec>, index_x: usize, index_y: usize) -> Vec { + + // Initialize an empty vector to store the adjacent entities + let mut adjacent_tiles = Vec::with_capacity(6); + + // Calculate the x_offset + let x_offset = index_x + ((index_y + 1) % 2) as usize; + + // Calculate all indexs of the adjacent tiles + let indexs = [ + (index_x + 1, index_y), + (index_x.checked_sub(1).unwrap_or(index_x), index_y), + (index_x, index_y + 1), + (index_x, index_y.checked_sub(1).unwrap_or(index_y)), + (x_offset, index_y + 1), + (x_offset, index_y.checked_sub(1).unwrap_or(index_y)), + ]; + + // Check if the adjacent tiles are in the map and add them to the vector + for (x, y) in indexs.iter().filter_map(|&(x, y)| map.get(y).and_then(|line| line.get(x))) { + adjacent_tiles.push(*x); + } + + adjacent_tiles +} + +/// Get the type of the tile at a given position wiith a perlin noise. +fn get_type(pos: (usize, usize), perlin: &Perlin) -> MapTile { + + // Get the value of the perlin noise at the position + let value = perlin.get([pos.0 as f64 / 2.0, pos.1 as f64 / 2.0]); + + // Map the value to a tile and return it. + match value { + v if v <= -0.3 => MapTile::Forest, + v if v >= 0.3 => MapTile::Hill, + _ => MapTile::Grass, + } +} + +/// Generate the map. +fn generate_map(mut commands: Commands) { + + // The size of the map. + let size = 3; + // Generate the perlin noise + let perlin = Perlin::new(12); + + // A vector to store the entities of the map. + let mut all_entities = Vec::new(); + + // Generate the map coordinates + let positions = generate_map_coordinates(5); + + // Spawn the entities of the map + for (y, line) in positions.iter().enumerate() { + let mut line_entities = Vec::new(); + for (x, pos) in line.iter().enumerate() { + + // Spawn the entity with a position and a type. + let mut entity = commands.spawn(Position(pos.0, pos.1)).insert(get_type((x, y), &perlin)); + + // Push the entity to the vector of the line of entities. + line_entities.push(entity.id()); + } + + // Push the vector the line of entities to the vector of all entities. + all_entities.push(line_entities); + } + + // Add the adjacent cases to the entities + for (y, ligne_entities) in all_entities.iter().enumerate() { + for (x, entity) in ligne_entities.iter().enumerate() { + // Get the adjacent entities and insert them + commands.entity(*entity).insert(AdjacentCases { + entities: get_adjacent_entities(&all_entities, x, y), + }); + } + } +} + +fn test(query: Query<(&Position, &MapTile)>) { + for (pos, map) in query.iter() { + println!("class : {:?}", map); + println!("pos : {:?}", pos); + } +}