feat(grid): draw 3d hex grid

This commit is contained in:
Kristofers Solo 2024-10-12 15:59:17 +03:00
parent 1a8eeb97b5
commit c471382f6f
6 changed files with 93 additions and 85 deletions

View File

@ -63,7 +63,7 @@ impl Plugin for AppPlugin {
#[cfg(feature = "demo")]
demo::plugin,
#[cfg(not(feature = "demo"))]
maze::MazePlugin,
maze::plugin::MazePlugin::default(),
screens::plugin,
theme::plugin,
));
@ -91,7 +91,7 @@ fn spawn_camera(mut commands: Commands) {
commands.spawn((
Name::new("Camera"),
Camera3dBundle {
transform: Transform::from_xyz(0., 15., 10.).looking_at(Vec3::ZERO, Vec3::Y),
transform: Transform::from_xyz(0., 100., 0.).looking_at(Vec3::ZERO, Vec3::Y),
..default()
},
// Render all UI to this camera.

View File

@ -1,31 +1,11 @@
use bevy::{
ecs::{system::RunSystemOnce, world::Command},
prelude::*,
};
use grid::{generate_maze, plugin, render_maze, spawn_hex_grid};
use bevy::{ecs::world::Command, prelude::*};
use plugin::MazePlugin;
pub mod grid;
pub mod plugin;
pub mod prism;
pub mod resource;
pub mod tile;
pub struct MazePlugin;
impl Plugin for MazePlugin {
fn build(&self, app: &mut App) {
app.add_plugins(prism::plugin);
app.add_plugins(plugin);
}
}
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(prism::setup);
}
}
pub fn spawn_grid(world: &mut World) {
MazePlugin.apply(world);
MazePlugin::default().apply(world);
}

25
src/maze/plugin.rs Normal file
View File

@ -0,0 +1,25 @@
use bevy::{
ecs::{system::RunSystemOnce, world::Command},
prelude::*,
};
use super::{grid, prism};
#[derive(Default)]
pub(crate) struct MazePlugin;
impl Plugin for MazePlugin {
fn build(&self, app: &mut App) {
app.add_plugins(prism::plugin);
app.add_plugins(grid::plugin);
}
}
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(prism::setup);
}
}

View File

@ -1,48 +1,20 @@
use bevy::prelude::*;
use std::f32::consts::FRAC_PI_2;
use super::{
resource::{Layout, MazeConfig},
tile::Tile,
};
pub(super) fn plugin(app: &mut App) {
app.add_systems(Update, spawn_light);
}
#[derive(Debug, Reflect, Component)]
#[reflect(Component)]
struct Prism {
radius: f32,
height: f32,
}
struct PrismParams {
position: Vec3,
radius: f32,
height: f32,
}
impl From<Vec3> for PrismParams {
fn from(value: Vec3) -> Self {
Self {
position: value,
..default()
}
}
}
impl Default for PrismParams {
fn default() -> Self {
Self {
position: Vec3::ZERO,
radius: 2.,
height: 4.,
}
}
app.add_systems(Startup, spawn_light);
}
pub(super) fn spawn_light(mut commands: Commands) {
commands.spawn((
Name::new("Light Source"),
PointLightBundle {
point_light: PointLight { ..default() },
transform: Transform::from_xyz(5., 10., 5.),
transform: Transform::from_xyz(0., 50., 0.),
..default()
},
));
@ -52,31 +24,51 @@ pub(super) fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
config: Res<MazeConfig>,
layout: Res<Layout>,
) {
let prism_material = materials.add(Color::WHITE);
let radius = config.radius as i32;
let prisms: Vec<PrismParams> = vec![Vec3::ZERO.into()];
let rotation = Quat::from_rotation_x(-FRAC_PI_2);
let material = materials.add(Color::WHITE);
let prism_mesh = generate_hex_prism_mesh(layout.hex_size.x, config.height);
let mesh = meshes.add(prism_mesh);
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.position)
.with_rotation(Quat::from_rotation_x(FRAC_PI_2)),
commands
.spawn((
Name::new("Floor"),
SpatialBundle {
transform: Transform::from_translation(Vec3::ZERO),
..default()
},
Prism {
radius: params.radius,
height: params.height,
},
));
}
))
.with_children(|p| {
for q in -radius..=radius {
let r1 = (-radius).max(-q - radius);
let r2 = radius.min(-q + radius);
for r in r1..=r2 {
let tile = Tile::new(q, r);
let pos = tile.to_vec3(&layout);
p.spawn((
Name::new(format!("Hex {}", &tile.to_string())),
PbrBundle {
mesh: mesh.clone(),
material: material.clone(),
transform: Transform::from_translation(pos).with_rotation(rotation),
..default()
},
));
}
}
});
}
fn generate_hex_prism_mesh(radius: f32, depth: f32) -> Mesh {
let hexagon = RegularPolygon {
sides: 6,
circumcircle: Circle::new(radius),
};
let prism_shape = Extrusion::new(hexagon, depth);
Mesh::from(prism_shape)
}

View File

@ -7,6 +7,7 @@ use rand::{thread_rng, Rng};
pub struct MazeConfig {
pub radius: u32,
pub size: f32,
pub height: f32,
pub start_pos: Hex,
pub end_pos: Hex,
}
@ -27,7 +28,8 @@ impl Default for MazeConfig {
debug!("End pos: ({},{})", end_pos.x, end_pos.y);
Self {
radius: radius as u32,
size: 10.,
size: 5.,
height: 5.,
start_pos,
end_pos,
}

View File

@ -1,7 +1,7 @@
use std::fmt::Display;
use bevy::prelude::*;
use hexx::Hex;
use hexx::{Hex, HexLayout};
#[derive(Debug, Reflect, Component, Default, PartialEq, Eq, Hash, Clone)]
#[reflect(Component)]
@ -31,6 +31,15 @@ impl Tile {
pub fn visit(&mut self) {
self.visited = true;
}
pub fn to_vec2(&self, layout: &HexLayout) -> Vec2 {
layout.hex_to_world_pos(self.hex)
}
pub fn to_vec3(&self, layout: &HexLayout) -> Vec3 {
let pos = self.to_vec2(layout);
Vec3::new(pos.x, 0., pos.y)
}
}
impl Display for Tile {