feat(hex): draw hex prisms

This commit is contained in:
Kristofers Solo 2024-09-25 20:29:58 +03:00
parent c5f8dede6d
commit ec9ac21b8f
7 changed files with 446 additions and 61 deletions

319
Cargo.lock generated
View File

@ -180,6 +180,24 @@ dependencies = [
"num-traits",
]
[[package]]
name = "arboard"
version = "3.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df099ccb16cd014ff054ac1bf392c67feeef57164b05c42f037cd40f5d4357f4"
dependencies = [
"clipboard-win",
"core-graphics",
"image 0.25.2",
"log",
"objc2",
"objc2-app-kit",
"objc2-foundation",
"parking_lot",
"windows-sys 0.48.0",
"x11rb",
]
[[package]]
name = "arrayref"
version = "0.3.8"
@ -304,6 +322,50 @@ dependencies = [
"bevy_internal",
]
[[package]]
name = "bevy-inspector-egui"
version = "0.26.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d46ba955761969dbc9a79d3583a175609b4e370303bc0f9a5846397dcb7f05fd"
dependencies = [
"bevy-inspector-egui-derive",
"bevy_app",
"bevy_asset",
"bevy_color",
"bevy_core",
"bevy_core_pipeline",
"bevy_ecs",
"bevy_egui",
"bevy_hierarchy",
"bevy_log",
"bevy_math",
"bevy_pbr",
"bevy_reflect",
"bevy_render",
"bevy_state",
"bevy_time",
"bevy_utils",
"bevy_window",
"bytemuck",
"egui",
"fuzzy-matcher",
"image 0.24.9",
"once_cell",
"pretty-type-name",
"smallvec",
]
[[package]]
name = "bevy-inspector-egui-derive"
version = "0.26.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06683f27065a27065ef8cebe77d7b347c9d0511a091deafd6dd77c5535434934"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.72",
]
[[package]]
name = "bevy_a11y"
version = "0.14.1"
@ -579,6 +641,29 @@ dependencies = [
"syn 2.0.72",
]
[[package]]
name = "bevy_egui"
version = "0.29.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "128438a8163e49528207aabf20d3ff0890fd6be0f0054626915995efac87922b"
dependencies = [
"arboard",
"bevy",
"bytemuck",
"console_log",
"crossbeam-channel",
"egui",
"js-sys",
"log",
"thread_local",
"wasm-bindgen",
"wasm-bindgen-futures",
"web-sys",
"webbrowser",
"wgpu-types",
"winit",
]
[[package]]
name = "bevy_encase_derive"
version = "0.14.1"
@ -901,7 +986,7 @@ dependencies = [
"encase",
"futures-lite",
"hexasphere",
"image",
"image 0.25.2",
"js-sys",
"ktx2",
"naga",
@ -1380,6 +1465,15 @@ dependencies = [
"libloading 0.8.5",
]
[[package]]
name = "clipboard-win"
version = "5.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "15efe7a882b08f34e38556b14f2fb3daa98769d06c7f0c1b076dfd0d983bc892"
dependencies = [
"error-code",
]
[[package]]
name = "codespan-reporting"
version = "0.11.1"
@ -1390,6 +1484,12 @@ dependencies = [
"unicode-width",
]
[[package]]
name = "color_quant"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b"
[[package]]
name = "com"
version = "0.6.0"
@ -1450,6 +1550,16 @@ dependencies = [
"wasm-bindgen",
]
[[package]]
name = "console_log"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be8aed40e4edbf4d3b4431ab260b63fdc40f5780a4766824329ea0f1eefe3c0f"
dependencies = [
"log",
"web-sys",
]
[[package]]
name = "const-fnv1a-hash"
version = "1.1.0"
@ -1494,10 +1604,20 @@ dependencies = [
]
[[package]]
name = "core-foundation-sys"
version = "0.8.6"
name = "core-foundation"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f"
checksum = "b55271e5c8c478ad3f38ad24ef34923091e0548492a266d19b3c0b4d82574c63"
dependencies = [
"core-foundation-sys",
"libc",
]
[[package]]
name = "core-foundation-sys"
version = "0.8.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b"
[[package]]
name = "core-graphics"
@ -1506,7 +1626,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c07782be35f9e1140080c6b96f0d44b739e2278479f64e02fdab4e32dfd8b081"
dependencies = [
"bitflags 1.3.2",
"core-foundation",
"core-foundation 0.9.4",
"core-graphics-types",
"foreign-types",
"libc",
@ -1519,7 +1639,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "45390e6114f68f718cc7a830514a96f903cccd70d02a8f6d9f643ac4ba45afaf"
dependencies = [
"bitflags 1.3.2",
"core-foundation",
"core-foundation 0.9.4",
"libc",
]
@ -1655,12 +1775,43 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f25c0e292a7ca6d6498557ff1df68f32c99850012b6ea401cf8daf771f22ff53"
[[package]]
name = "ecolor"
version = "0.28.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2e6b451ff1143f6de0f33fc7f1b68fecfd2c7de06e104de96c4514de3f5396f8"
dependencies = [
"bytemuck",
"emath",
]
[[package]]
name = "egui"
version = "0.28.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "20c97e70a2768de630f161bb5392cbd3874fcf72868f14df0e002e82e06cb798"
dependencies = [
"ahash",
"emath",
"epaint",
"nohash-hasher",
]
[[package]]
name = "either"
version = "1.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0"
[[package]]
name = "emath"
version = "0.28.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0a6a21708405ea88f63d8309650b4d77431f4bc28fb9d8e6f77d3963b51249e6"
dependencies = [
"bytemuck",
]
[[package]]
name = "encase"
version = "0.8.0"
@ -1693,6 +1844,21 @@ dependencies = [
"syn 2.0.72",
]
[[package]]
name = "epaint"
version = "0.28.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f0dcc0a0771e7500e94cd1cb797bd13c9f23b9409bdc3c824e2cbc562b7fa01"
dependencies = [
"ab_glyph",
"ahash",
"bytemuck",
"ecolor",
"emath",
"nohash-hasher",
"parking_lot",
]
[[package]]
name = "equivalent"
version = "1.0.1"
@ -1719,6 +1885,12 @@ dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "error-code"
version = "3.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a5d9305ccc6942a704f4335694ecd3de2ea531b114ac2d51f5f843750787a92f"
[[package]]
name = "euclid"
version = "0.22.10"
@ -1852,6 +2024,15 @@ version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa9a19cbb55df58761df49b23516a86d432839add4af60fc256da840f66ed35b"
[[package]]
name = "form_urlencoded"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e13624c2627564efccf4934284bdd98cbaa14e79b0b5a141218e507b3a823456"
dependencies = [
"percent-encoding",
]
[[package]]
name = "fsevent-sys"
version = "4.1.0"
@ -1886,6 +2067,15 @@ dependencies = [
"pin-project-lite",
]
[[package]]
name = "fuzzy-matcher"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "54614a3312934d066701a80f20f15fa3b56d67ac7722b39eea5b4c9dd1d66c94"
dependencies = [
"thread_local",
]
[[package]]
name = "gethostname"
version = "0.4.3"
@ -1928,7 +2118,7 @@ version = "0.5.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbb5e8d912059b33b463831c16b838d15c4772d584ce332e4a80f6dffdae2bc1"
dependencies = [
"core-foundation",
"core-foundation 0.9.4",
"inotify 0.10.2",
"io-kit-sys",
"js-sys",
@ -2166,6 +2356,37 @@ dependencies = [
"serde",
]
[[package]]
name = "home"
version = "0.5.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3d1354bf6b7235cb4a0576c2619fd4ed18183f689b12b006a0ee7329eeff9a5"
dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "idna"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "634d9b1461af396cad843f47fdba5597a4f9e6ddd4bfb6ff5d85028c25cb12f6"
dependencies = [
"unicode-bidi",
"unicode-normalization",
]
[[package]]
name = "image"
version = "0.24.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5690139d2f55868e080017335e4b94cb7414274c74f1669c84fb5feba2c9f69d"
dependencies = [
"bytemuck",
"byteorder",
"color_quant",
"num-traits",
]
[[package]]
name = "image"
version = "0.25.2"
@ -2176,6 +2397,7 @@ dependencies = [
"byteorder-lite",
"num-traits",
"png",
"tiff",
]
[[package]]
@ -2290,6 +2512,12 @@ dependencies = [
"libc",
]
[[package]]
name = "jpeg-decoder"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f5d4a7da358eff58addd2877a45865158f0d78c911d43a5784ceb7bbf52833b0"
[[package]]
name = "js-sys"
version = "0.3.69"
@ -2692,6 +2920,12 @@ dependencies = [
"libc",
]
[[package]]
name = "nohash-hasher"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2bf50223579dc7cdcfb3bfcacf7069ff68243f8c363f62ffa99cf000a6b9c451"
[[package]]
name = "nom"
version = "7.1.3"
@ -3224,6 +3458,12 @@ version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e8cf8e6a8aa66ce33f63993ffc4ea4271eb5b0530a9002db8455ea6050c77bfa"
[[package]]
name = "pretty-type-name"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0f73cdaf19b52e6143685c3606206e114a4dfa969d6b14ec3894c88eb38bd4b"
[[package]]
name = "proc-macro-crate"
version = "3.1.0"
@ -3703,6 +3943,7 @@ name = "the-labyrinth-of-echoes"
version = "0.0.3"
dependencies = [
"bevy",
"bevy-inspector-egui",
"bevy_prototype_lyon",
"hexx",
"log",
@ -3740,6 +3981,17 @@ dependencies = [
"once_cell",
]
[[package]]
name = "tiff"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba1310fcea54c6a9a4fd1aad794ecc02c31682f6bfbecdf460bf19533eed1e3e"
dependencies = [
"flate2",
"jpeg-decoder",
"weezl",
]
[[package]]
name = "tiny-skia"
version = "0.11.4"
@ -3902,12 +4154,27 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "059d83cc991e7a42fc37bd50941885db0888e34209f8cfd9aab07ddec03bc9cf"
[[package]]
name = "unicode-bidi"
version = "0.3.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "08f95100a766bf4f8f28f90d77e0a5461bbdb219042e7679bebe79004fed8d75"
[[package]]
name = "unicode-ident"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "unicode-normalization"
version = "0.1.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5033c97c4262335cded6d6fc3e5c18ab755e1a3dc96376350f3d8e9f009ad956"
dependencies = [
"tinyvec",
]
[[package]]
name = "unicode-segmentation"
version = "1.11.0"
@ -3926,6 +4193,17 @@ version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f962df74c8c05a667b5ee8bcf162993134c104e96440b663c8daa176dc772d8c"
[[package]]
name = "url"
version = "2.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22784dbdf76fdde8af1aeda5622b546b422b6fc585325248a2bf9f5e41e94d6c"
dependencies = [
"form_urlencoded",
"idna",
"percent-encoding",
]
[[package]]
name = "uuid"
version = "1.10.0"
@ -4141,6 +4419,7 @@ checksum = "43676fe2daf68754ecf1d72026e4e6c15483198b5d24e888b74d3f22f887a148"
dependencies = [
"dlib",
"log",
"once_cell",
"pkg-config",
]
@ -4164,6 +4443,30 @@ dependencies = [
"wasm-bindgen",
]
[[package]]
name = "webbrowser"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2e5f07fb9bc8de2ddfe6b24a71a75430673fd679e568c48b52716cef1cfae923"
dependencies = [
"block2",
"core-foundation 0.10.0",
"home",
"jni",
"log",
"ndk-context",
"objc2",
"objc2-foundation",
"url",
"web-sys",
]
[[package]]
name = "weezl"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53a85b86a771b1c87058196170769dd264f66c0782acf1ae6cc51bfd64b39082"
[[package]]
name = "wgpu"
version = "0.20.1"
@ -4675,7 +4978,7 @@ dependencies = [
"calloop",
"cfg_aliases 0.2.1",
"concurrent-queue",
"core-foundation",
"core-foundation 0.9.4",
"core-graphics",
"cursor-icon",
"dpi",

View File

@ -20,6 +20,9 @@ tracing = { version = "0.1", features = [
hexx = { version = "0.18", features = ["bevy_reflect", "grid"] }
bevy_prototype_lyon = "0.12"
[dev-dependencies]
bevy-inspector-egui = "0.26"
[features]
default = [
# Default to a native dev build.

View File

@ -1,14 +0,0 @@
use bevy::{color::palettes::css::BLACK, prelude::*};
use bevy_prototype_lyon::{
draw::{Fill, Stroke},
entity::ShapeBundle,
path::PathBuilder,
plugin::ShapePlugin,
};
use rand::{thread_rng, Rng};
pub(super) fn plugin(app: &mut App) {
app.add_plugins(ShapePlugin);
app.add_systems(Startup, setup_system);
}

View File

@ -9,6 +9,8 @@ mod maze;
mod screens;
mod theme;
use std::f32::consts::FRAC_PI_4;
use bevy::{
asset::AssetMetaCheck,
audio::{AudioPlugin, Volume},
@ -90,7 +92,11 @@ enum AppSet {
fn spawn_camera(mut commands: Commands) {
commands.spawn((
Name::new("Camera"),
Camera2dBundle::default(),
Camera3dBundle {
transform: Transform::from_xyz(0., 5., 10.).looking_at(Vec3::ZERO, Vec3::Y),
..default()
},
// Camera2dBundle::default(),
// Render all UI to this camera.
// Not strictly necessary since we only use one camera,
// but if we don't use this component, our UI will disappear as soon

View File

@ -1,11 +1,12 @@
use std::usize;
use std::{f32::consts::PI, usize};
use bevy::{
color::palettes::css::{BLACK, GREEN, RED},
color::palettes::css::{BLACK, GREEN, RED, SILVER},
pbr::wireframe::{Wireframe, WireframeConfig, WireframePlugin},
prelude::*,
render::{mesh::PrimitiveTopology, render_asset::RenderAssetUsages},
utils::hashbrown::HashMap,
};
use bevy_prototype_lyon::{
draw::{Fill, Stroke},
entity::ShapeBundle,
@ -13,7 +14,6 @@ use bevy_prototype_lyon::{
plugin::ShapePlugin,
};
use hexx::{EdgeDirection, Hex};
use log::info;
use rand::{prelude::SliceRandom, rngs::ThreadRng, thread_rng};
use super::{
@ -22,9 +22,28 @@ use super::{
};
pub(super) fn plugin(app: &mut App) {
app.add_plugins(ShapePlugin);
app.add_plugins((ShapePlugin, WireframePlugin));
app.init_resource::<MazeConfig>();
app.init_resource::<Layout>();
app.insert_resource(WireframeConfig {
global: false,
..default()
});
}
pub(super) fn spawn_light(mut commands: Commands) {
commands.spawn((
Name::new("Light Source"),
PointLightBundle {
point_light: PointLight {
intensity: 5000.,
shadows_enabled: true,
..default()
},
transform: Transform::from_xyz(5., 10., 5.),
..default()
},
));
}
pub(super) fn spawn_hex_grid(mut commands: Commands, config: Res<MazeConfig>) {
@ -109,7 +128,7 @@ fn remove_wall_between(
fn add_hex_tile(
commands: &mut Commands,
position: Vec2,
position: Vec3,
size: f32,
tile: &Tile,
walls: &Walls,
@ -122,7 +141,7 @@ fn add_hex_tile(
.into_iter()
.map(|v| {
let mut layout = layout.clone();
layout.origin = position;
layout.origin = position.xy();
layout.hex_size = Vec2::splat(size);
layout.hex_to_world_pos(v.origin + v.direction)
})
@ -137,7 +156,8 @@ fn add_hex_tile(
let hexagon = path_builder.build();
// Create the hexagon fill
commands.spawn((
commands
.spawn((
ShapeBundle {
path: hexagon,
spatial: SpatialBundle {
@ -147,24 +167,24 @@ fn add_hex_tile(
..default()
},
Fill::color(fill_color),
));
// .with_children(|p| {
// p.spawn(Text2dBundle {
// text: Text {
// sections: vec![TextSection {
// value: tile.to_string(),
// style: TextStyle {
// font_size: 16.,
// color: Color::BLACK,
// ..default()
// },
// }],
// ..default()
// },
// transform: Transform::from_xyz(position.x * 2., position.y * 2., 1.),
// ..default()
// });
// });
))
.with_children(|p| {
p.spawn(Text2dBundle {
text: Text {
sections: vec![TextSection {
value: tile.to_string(),
style: TextStyle {
font_size: 16.,
color: Color::BLACK,
..default()
},
}],
..default()
},
transform: Transform::from_xyz(position.x * 2., position.y * 2., 1.),
..default()
});
});
// Draw walls
for direction in EdgeDirection::iter() {
@ -199,7 +219,7 @@ pub(super) fn render_maze(
config: Res<MazeConfig>,
) {
for (tile, walls) in query.iter() {
let world_pos = layout.hex_to_world_pos(tile.hex);
let world_pos = layout.hex_to_world_pos(tile.hex).extend(0.);
let fill_color = match tile.hex {
pos if pos == config.start_pos => GREEN.into(),
pos if pos == config.end_pos => RED.into(),

View File

@ -2,8 +2,9 @@ use bevy::{
ecs::{system::RunSystemOnce, world::Command},
prelude::*,
};
use grid::{generate_maze, plugin, render_maze, spawn_hex_grid};
use grid::{generate_maze, plugin, render_maze, spawn_hex_grid, spawn_light};
pub mod grid;
pub mod prism;
pub mod resource;
pub mod tile;
@ -17,9 +18,11 @@ impl Plugin for MazePlugin {
impl Command for MazePlugin {
fn apply(self, world: &mut World) {
world.run_system_once(spawn_hex_grid);
world.run_system_once(generate_maze);
world.run_system_once(render_maze);
// world.run_system_once(spawn_hex_grid);
// world.run_system_once(generate_maze);
// world.run_system_once(render_maze);
world.run_system_once(spawn_light);
world.run_system_once(prism::setup);
}
}

64
src/maze/prism.rs Normal file
View File

@ -0,0 +1,64 @@
use bevy::prelude::*;
use std::f32::consts::FRAC_PI_2;
#[derive(Debug, Reflect, Component)]
#[reflect(Component)]
struct Prism {
radius: f32,
height: f32,
}
struct PrismParams {
positions: Vec3,
radius: f32,
height: f32,
}
pub(super) fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
let prism_material = materials.add(Color::WHITE);
let prisms = vec![
PrismParams {
positions: Vec3::new(-3., 0., 0.),
radius: 1.,
height: 2.,
},
PrismParams {
positions: Vec3::new(0., 0., 0.),
radius: 1.5,
height: 2.5,
},
PrismParams {
positions: Vec3::new(3., 0., 0.),
radius: 0.8,
height: 1.5,
},
];
for params in prisms {
let hexagon = RegularPolygon {
sides: 6,
circumcircle: Circle::new(params.radius),
};
let prism_shape = Extrusion::new(hexagon, params.height);
let prism_mesh = meshes.add(Mesh::from(prism_shape));
commands.spawn((
PbrBundle {
mesh: prism_mesh,
material: prism_material.clone(),
transform: Transform::from_translation(params.positions)
.with_rotation(Quat::from_rotation_x(FRAC_PI_2)),
..default()
},
Prism {
radius: params.radius,
height: params.height,
},
));
}
}