generated from tipragot/rust
Add a renderer system with temp assets #60
7
.vscode/settings.json
vendored
Normal file
7
.vscode/settings.json
vendored
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
{
|
||||||
|
"cSpell.words": [
|
||||||
|
"despawn",
|
||||||
|
"Despawns",
|
||||||
|
"Perlin"
|
||||||
|
]
|
||||||
|
}
|
103
Cargo.lock
generated
103
Cargo.lock
generated
|
@ -93,7 +93,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "77c3a9648d43b9cd48db467b3f87fdd6e146bcc88ab0180006cef2179fe11d01"
|
checksum = "77c3a9648d43b9cd48db467b3f87fdd6e146bcc88ab0180006cef2179fe11d01"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"cfg-if",
|
"cfg-if",
|
||||||
"getrandom",
|
"getrandom 0.2.12",
|
||||||
"once_cell",
|
"once_cell",
|
||||||
"version_check",
|
"version_check",
|
||||||
"zerocopy",
|
"zerocopy",
|
||||||
|
@ -1126,7 +1126,7 @@ checksum = "7915222f4a08ccc782e08d10b751b42e5f9d786e697d0cb3fd09333cb7e8b6ea"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"ahash",
|
"ahash",
|
||||||
"bevy_utils_proc_macros",
|
"bevy_utils_proc_macros",
|
||||||
"getrandom",
|
"getrandom 0.2.12",
|
||||||
"hashbrown 0.14.3",
|
"hashbrown 0.14.3",
|
||||||
"instant",
|
"instant",
|
||||||
"nonmax",
|
"nonmax",
|
||||||
|
@ -1316,6 +1316,7 @@ version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bevy",
|
"bevy",
|
||||||
"bevy_egui",
|
"bevy_egui",
|
||||||
|
"noise",
|
||||||
"num",
|
"num",
|
||||||
"partial-min-max",
|
"partial-min-max",
|
||||||
"paste",
|
"paste",
|
||||||
|
@ -2108,6 +2109,17 @@ dependencies = [
|
||||||
"winapi",
|
"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]]
|
[[package]]
|
||||||
name = "getrandom"
|
name = "getrandom"
|
||||||
version = "0.2.12"
|
version = "0.2.12"
|
||||||
|
@ -2117,7 +2129,7 @@ dependencies = [
|
||||||
"cfg-if",
|
"cfg-if",
|
||||||
"js-sys",
|
"js-sys",
|
||||||
"libc",
|
"libc",
|
||||||
"wasi",
|
"wasi 0.11.0+wasi-snapshot-preview1",
|
||||||
"wasm-bindgen",
|
"wasm-bindgen",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -2847,7 +2859,7 @@ checksum = "8f3d0b296e374a4e6f3c7b0a1f5a51d748a0d34c85e7dc48fc3fa9a87657fe09"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"libc",
|
"libc",
|
||||||
"log",
|
"log",
|
||||||
"wasi",
|
"wasi 0.11.0+wasi-snapshot-preview1",
|
||||||
"windows-sys 0.48.0",
|
"windows-sys 0.48.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -2961,6 +2973,17 @@ version = "0.2.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "2bf50223579dc7cdcfb3bfcacf7069ff68243f8c363f62ffa99cf000a6b9c451"
|
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]]
|
[[package]]
|
||||||
name = "nom"
|
name = "nom"
|
||||||
version = "7.1.3"
|
version = "7.1.3"
|
||||||
|
@ -3475,6 +3498,19 @@ version = "0.1.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "17fd96390ed3feda12e1dfe2645ed587e0bea749e319333f104a33ff62f77a0b"
|
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]]
|
[[package]]
|
||||||
name = "rand"
|
name = "rand"
|
||||||
version = "0.8.5"
|
version = "0.8.5"
|
||||||
|
@ -3482,8 +3518,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
|
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"libc",
|
"libc",
|
||||||
"rand_chacha",
|
"rand_chacha 0.3.1",
|
||||||
"rand_core",
|
"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]]
|
[[package]]
|
||||||
|
@ -3493,7 +3539,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
|
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"ppv-lite86",
|
"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]]
|
[[package]]
|
||||||
|
@ -3502,7 +3557,25 @@ version = "0.6.4"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
|
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
|
||||||
dependencies = [
|
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]]
|
[[package]]
|
||||||
|
@ -3607,7 +3680,7 @@ dependencies = [
|
||||||
"home",
|
"home",
|
||||||
"log",
|
"log",
|
||||||
"mio",
|
"mio",
|
||||||
"rand",
|
"rand 0.8.5",
|
||||||
"tungstenite",
|
"tungstenite",
|
||||||
"uuid",
|
"uuid",
|
||||||
]
|
]
|
||||||
|
@ -3639,7 +3712,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "688c63d65483050968b2a8937f7995f443e27041a0f7700aa59b0822aedebb74"
|
checksum = "688c63d65483050968b2a8937f7995f443e27041a0f7700aa59b0822aedebb74"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"cc",
|
"cc",
|
||||||
"getrandom",
|
"getrandom 0.2.12",
|
||||||
"libc",
|
"libc",
|
||||||
"spin",
|
"spin",
|
||||||
"untrusted",
|
"untrusted",
|
||||||
|
@ -4342,7 +4415,7 @@ dependencies = [
|
||||||
"http",
|
"http",
|
||||||
"httparse",
|
"httparse",
|
||||||
"log",
|
"log",
|
||||||
"rand",
|
"rand 0.8.5",
|
||||||
"rustls",
|
"rustls",
|
||||||
"rustls-native-certs",
|
"rustls-native-certs",
|
||||||
"rustls-pki-types",
|
"rustls-pki-types",
|
||||||
|
@ -4430,7 +4503,7 @@ version = "1.7.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "f00cc9702ca12d3c81455259621e676d0f7251cec66a21e98fe2e9a37db93b2a"
|
checksum = "f00cc9702ca12d3c81455259621e676d0f7251cec66a21e98fe2e9a37db93b2a"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"getrandom",
|
"getrandom 0.2.12",
|
||||||
"serde",
|
"serde",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -4468,6 +4541,12 @@ dependencies = [
|
||||||
"winapi-util",
|
"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]]
|
[[package]]
|
||||||
name = "wasi"
|
name = "wasi"
|
||||||
version = "0.11.0+wasi-snapshot-preview1"
|
version = "0.11.0+wasi-snapshot-preview1"
|
||||||
|
|
|
@ -13,6 +13,7 @@ workspace = true
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bevy = "0.12.1"
|
bevy = "0.12.1"
|
||||||
bevy_egui = "0.24.0"
|
bevy_egui = "0.24.0"
|
||||||
|
noise = "0.8.2"
|
||||||
num = "0.4.1"
|
num = "0.4.1"
|
||||||
partial-min-max = "0.4.0"
|
partial-min-max = "0.4.0"
|
||||||
paste = "1.0.14"
|
paste = "1.0.14"
|
||||||
|
|
BIN
crates/border-wars/assets/tiles/forest.png
Normal file
BIN
crates/border-wars/assets/tiles/forest.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 11 KiB |
BIN
crates/border-wars/assets/tiles/grass.png
Normal file
BIN
crates/border-wars/assets/tiles/grass.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.7 KiB |
BIN
crates/border-wars/assets/tiles/hill.png
Normal file
BIN
crates/border-wars/assets/tiles/hill.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 12 KiB |
BIN
crates/border-wars/src/.main.rs.swp
Normal file
BIN
crates/border-wars/src/.main.rs.swp
Normal file
Binary file not shown.
|
@ -1,11 +1,24 @@
|
||||||
//! The main entry point of the game.
|
//! The main entry point of the game.
|
||||||
|
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
use border_wars::map::generation::{MapGenerationPlugin, StartMapGeneration};
|
||||||
|
use border_wars::map::renderer::RendererPlugin;
|
||||||
use border_wars::scenes::ScenesPlugin;
|
use border_wars::scenes::ScenesPlugin;
|
||||||
|
use border_wars::CurrentScene;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
App::new()
|
App::new()
|
||||||
.add_plugins(DefaultPlugins)
|
.add_plugins(DefaultPlugins)
|
||||||
.add_plugins(ScenesPlugin)
|
.add_plugins(ScenesPlugin)
|
||||||
|
.add_plugins(MapGenerationPlugin)
|
||||||
|
.add_plugins(RendererPlugin)
|
||||||
|
.add_systems(OnEnter(CurrentScene::Game), gen_map)
|
||||||
.run();
|
.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn gen_map(mut event: EventWriter<StartMapGeneration>) {
|
||||||
|
event.send(StartMapGeneration {
|
||||||
|
seed: 5667,
|
||||||
|
radius: 5,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
BIN
crates/border-wars/src/map/.renderer.rs.swp
Normal file
BIN
crates/border-wars/src/map/.renderer.rs.swp
Normal file
Binary file not shown.
98
crates/border-wars/src/map/generation.rs
Normal file
98
crates/border-wars/src/map/generation.rs
Normal file
|
@ -0,0 +1,98 @@
|
||||||
|
//! All functions related to the generation of the map.
|
||||||
|
|
||||||
|
use bevy::prelude::*;
|
||||||
|
use noise::{NoiseFn, Perlin};
|
||||||
|
|
||||||
|
use super::hex::*;
|
||||||
|
use super::{Tile, TilePosition};
|
||||||
|
|
||||||
|
/// A plugin to handle the map generation.
|
||||||
|
pub struct MapGenerationPlugin;
|
||||||
|
|
||||||
|
/// The zoom of the map during the generation.
|
||||||
|
const MAP_GENERATION_SCALE: f32 = 5.;
|
||||||
|
|
||||||
|
impl Plugin for MapGenerationPlugin {
|
||||||
|
fn build(&self, app: &mut App) {
|
||||||
|
app.add_event::<StartMapGeneration>()
|
||||||
|
.add_event::<EndMapGeneration>()
|
||||||
|
.add_systems(
|
||||||
|
Update,
|
||||||
|
(delete_map, generate_map.after(delete_map))
|
||||||
|
.run_if(in_state(crate::CurrentScene::Game)),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// An event to trigger the generation of the map.
|
||||||
|
#[derive(Event)]
|
||||||
|
pub struct StartMapGeneration {
|
||||||
|
/// The seed used to generate the map.
|
||||||
|
pub seed: u32,
|
||||||
|
|
||||||
|
/// The radius of the map.
|
||||||
|
pub radius: u16,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// An event send when the map is generated.
|
||||||
|
#[derive(Event)]
|
||||||
|
pub struct EndMapGeneration;
|
||||||
|
|
||||||
|
/// Generate each tiles of the map if the [StartMapGeneration] is received.
|
||||||
|
///
|
||||||
|
/// The map is generated using a [Perlin] noise and a [HexSpiral].
|
||||||
|
///
|
||||||
|
/// It's generated one tile at a time, until the spiral is finished.
|
||||||
|
fn generate_map(
|
||||||
|
mut start_generation_events: EventReader<StartMapGeneration>,
|
||||||
|
mut end_generation_writer: EventWriter<EndMapGeneration>,
|
||||||
|
mut commands: Commands,
|
||||||
|
mut local_noise: Local<Option<Perlin>>,
|
||||||
|
mut local_spiral: Local<Option<HexSpiral<i32>>>,
|
||||||
|
) {
|
||||||
|
// Handle map generation events and create the spiral and the noise.
|
||||||
|
for event in start_generation_events.read() {
|
||||||
|
*local_noise = Some(Perlin::new(event.seed));
|
||||||
|
*local_spiral = Some(TilePosition::new(0, 0).spiral(event.radius as usize));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the map is being generated.
|
||||||
|
let (Some(noise), Some(spiral)) = (local_noise.as_ref(), local_spiral.as_mut()) else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Spawn a tile until the spiral is finished
|
||||||
|
// If the map is generated, we send [EndMapGeneration] and set the local
|
||||||
|
// variables to None.
|
||||||
|
if let Some(position) = spiral.next() {
|
||||||
|
commands.spawn((get_tile_type(position, noise), position as TilePosition));
|
||||||
|
} else {
|
||||||
|
end_generation_writer.send(EndMapGeneration);
|
||||||
|
*local_noise = None;
|
||||||
|
*local_spiral = None;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the type of the [HexPosition] with the given noise.
|
||||||
|
fn get_tile_type(position: HexPosition<i32>, noise: &Perlin) -> Tile {
|
||||||
|
let pixel_position = position.to_pixel_coordinates() / MAP_GENERATION_SCALE;
|
||||||
|
let value = noise.get([pixel_position.x as f64, pixel_position.y as f64]);
|
||||||
|
match value {
|
||||||
|
v if v <= -0.4 => Tile::Hill,
|
||||||
|
v if v >= 0.4 => Tile::Forest,
|
||||||
|
_ => Tile::Grass,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Despawns the tiles if the event [StartMapGeneration] is received.
|
||||||
|
fn delete_map(
|
||||||
|
mut commands: Commands,
|
||||||
|
query: Query<Entity, With<Tile>>,
|
||||||
|
mut start_generation_events: EventReader<StartMapGeneration>,
|
||||||
|
) {
|
||||||
|
for _ in start_generation_events.read() {
|
||||||
|
for entity in query.iter() {
|
||||||
|
commands.entity(entity).despawn_recursive();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,6 +4,7 @@ use std::ops::{
|
||||||
Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Rem, RemAssign, Sub, SubAssign,
|
Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Rem, RemAssign, Sub, SubAssign,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use bevy::prelude::*;
|
||||||
use paste::paste;
|
use paste::paste;
|
||||||
|
|
||||||
/// Represents a number that can be used in calculations for hexagonal grids.
|
/// 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.
|
/// Represents a position in a hexagonal grid.
|
||||||
/// We use the axial coordinate system explained in this
|
/// We use the axial coordinate system explained in this
|
||||||
/// [documentation](https://www.redblobgames.com/grids/hexagons/#coordinates).
|
/// [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);
|
pub struct HexPosition<T: Number>(pub T, pub T);
|
||||||
|
|
||||||
/// All possible directions in a hexagonal grid.
|
/// All possible directions in a hexagonal grid.
|
||||||
|
@ -233,8 +234,12 @@ impl<T: Number> Iterator for HexSpiral<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Number> HexPosition<T> {
|
impl<T: Number> HexPosition<T> {
|
||||||
|
/// Creates a new [HexPosition].
|
||||||
|
pub const fn new(x: T, y: T) -> Self {
|
||||||
|
Self(x, y)
|
||||||
|
}
|
||||||
|
|
||||||
/// Converts the current [HexPosition] into a pixel coordinate.
|
/// Converts the current [HexPosition] into a pixel coordinate.
|
||||||
/// Input: The size of the hexagon in pixels (witdh, height).
|
|
||||||
///
|
///
|
||||||
/// If you want to learn more about pixel coordinates conversion,
|
/// If you want to learn more about pixel coordinates conversion,
|
||||||
/// you can check the
|
/// you can check the
|
||||||
|
@ -243,21 +248,17 @@ impl<T: Number> HexPosition<T> {
|
||||||
/// # Example
|
/// # Example
|
||||||
///
|
///
|
||||||
/// ```no_run
|
/// ```no_run
|
||||||
|
/// use bevy::math::Vec2;
|
||||||
/// use border_wars::map::hex::HexPosition;
|
/// use border_wars::map::hex::HexPosition;
|
||||||
///
|
///
|
||||||
/// let position = HexPosition(1, 0);
|
/// let position = HexPosition(1, 0);
|
||||||
/// assert_eq!(
|
/// assert_eq!(position.to_pixel_coordinates(), (3f32.sqrt(), 0.0).into());
|
||||||
/// position.to_pixel_coordinates((1.0, 1.0)),
|
|
||||||
/// (3f32.sqrt(), 0.0)
|
|
||||||
/// );
|
|
||||||
/// ```
|
/// ```
|
||||||
pub fn to_pixel_coordinates(&self, size: (f32, f32)) -> (f32, f32) {
|
pub fn to_pixel_coordinates(&self) -> Vec2 {
|
||||||
(
|
Vec2::new(
|
||||||
size.0
|
3f32.sqrt()
|
||||||
* 3f32
|
.mul_add(T::to_f32(self.0), 3f32.sqrt() / 2.0 * T::to_f32(self.1)),
|
||||||
.sqrt()
|
3.0 / 2.0 * T::to_f32(self.1),
|
||||||
.mul_add(T::to_f32(self.0), 3f32.sqrt() / 2.0 * T::to_f32(self.1)),
|
|
||||||
size.1 * (3.0 / 2.0 * T::to_f32(self.1)),
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,25 @@
|
||||||
//! Contains all the logic related to the map.
|
//! Contains all the logic related to the map.
|
||||||
|
|
||||||
|
pub mod generation;
|
||||||
pub mod hex;
|
pub mod hex;
|
||||||
|
pub mod renderer;
|
||||||
|
|
||||||
|
use bevy::prelude::*;
|
||||||
|
|
||||||
|
use self::hex::*;
|
||||||
|
|
||||||
|
/// The position of a tile in a hexagonal map.
|
||||||
|
pub type TilePosition = HexPosition<i32>;
|
||||||
|
|
||||||
|
/// The tile of the map.
|
||||||
|
#[derive(Component, Debug)]
|
||||||
|
pub enum Tile {
|
||||||
|
/// The hill tile.
|
||||||
|
Hill,
|
||||||
|
|
||||||
|
/// The grass tile.
|
||||||
|
Grass,
|
||||||
|
|
||||||
|
/// The forest tile.
|
||||||
|
Forest,
|
||||||
|
}
|
||||||
|
|
56
crates/border-wars/src/map/renderer.rs
Normal file
56
crates/border-wars/src/map/renderer.rs
Normal file
|
@ -0,0 +1,56 @@
|
||||||
|
//! All functions related to the rendering of the map.
|
||||||
|
|
||||||
|
use bevy::prelude::*;
|
||||||
|
|
||||||
|
use super::Tile;
|
||||||
|
use crate::map::TilePosition;
|
||||||
|
|
||||||
|
/// A plugin to render the map.
|
||||||
|
pub struct RendererPlugin;
|
||||||
|
|
||||||
|
impl Plugin for RendererPlugin {
|
||||||
|
fn build(&self, app: &mut App) {
|
||||||
|
app.add_systems(Startup, setup_camera).add_systems(
|
||||||
|
Update,
|
||||||
|
render_map.run_if(in_state(crate::CurrentScene::Game)),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sets up the camera.
|
||||||
|
fn setup_camera(mut commands: Commands) {
|
||||||
|
commands.spawn(Camera2dBundle::default());
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Tile {
|
||||||
|
/// Returns the texture handle of the tile.
|
||||||
|
pub fn get_texture(&self, asset_server: &AssetServer) -> Handle<Image> {
|
||||||
|
match self {
|
||||||
|
Tile::Grass => asset_server.load("tiles/grass.png"),
|
||||||
|
Tile::Forest => asset_server.load("tiles/forest.png"),
|
||||||
|
Tile::Hill => asset_server.load("tiles/hill.png"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Renders the map.
|
||||||
|
fn render_map(
|
||||||
|
query: Query<(Entity, &TilePosition, &Tile), Changed<Tile>>,
|
||||||
|
mut commands: Commands,
|
||||||
|
asset_server: Res<AssetServer>,
|
||||||
|
) {
|
||||||
|
for (entity, position, tile) in query.iter() {
|
||||||
|
let pixel_position_ratio = position.to_pixel_coordinates();
|
||||||
|
let pixel_position = (50. * pixel_position_ratio.x, pixel_position_ratio.y * 30.);
|
||||||
|
|
||||||
|
commands.entity(entity).insert(SpriteBundle {
|
||||||
|
transform: Transform::from_translation(Vec3 {
|
||||||
|
x: pixel_position.0,
|
||||||
|
y: pixel_position.1,
|
||||||
|
z: 0.,
|
||||||
|
}),
|
||||||
|
texture: tile.get_texture(&*asset_server),
|
||||||
|
..default()
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue