mirror of
https://github.com/kristoferssolo/maze-ascension.git
synced 2025-10-21 19:20:34 +00:00
feat(grid): draw 3d hex grid
This commit is contained in:
parent
1a8eeb97b5
commit
c471382f6f
@ -63,7 +63,7 @@ impl Plugin for AppPlugin {
|
|||||||
#[cfg(feature = "demo")]
|
#[cfg(feature = "demo")]
|
||||||
demo::plugin,
|
demo::plugin,
|
||||||
#[cfg(not(feature = "demo"))]
|
#[cfg(not(feature = "demo"))]
|
||||||
maze::MazePlugin,
|
maze::plugin::MazePlugin::default(),
|
||||||
screens::plugin,
|
screens::plugin,
|
||||||
theme::plugin,
|
theme::plugin,
|
||||||
));
|
));
|
||||||
@ -91,7 +91,7 @@ fn spawn_camera(mut commands: Commands) {
|
|||||||
commands.spawn((
|
commands.spawn((
|
||||||
Name::new("Camera"),
|
Name::new("Camera"),
|
||||||
Camera3dBundle {
|
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()
|
..default()
|
||||||
},
|
},
|
||||||
// Render all UI to this camera.
|
// Render all UI to this camera.
|
||||||
|
|||||||
@ -1,31 +1,11 @@
|
|||||||
use bevy::{
|
use bevy::{ecs::world::Command, prelude::*};
|
||||||
ecs::{system::RunSystemOnce, world::Command},
|
use plugin::MazePlugin;
|
||||||
prelude::*,
|
|
||||||
};
|
|
||||||
use grid::{generate_maze, plugin, render_maze, spawn_hex_grid};
|
|
||||||
pub mod grid;
|
pub mod grid;
|
||||||
|
pub mod plugin;
|
||||||
pub mod prism;
|
pub mod prism;
|
||||||
pub mod resource;
|
pub mod resource;
|
||||||
pub mod tile;
|
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) {
|
pub fn spawn_grid(world: &mut World) {
|
||||||
MazePlugin.apply(world);
|
MazePlugin::default().apply(world);
|
||||||
}
|
}
|
||||||
|
|||||||
25
src/maze/plugin.rs
Normal file
25
src/maze/plugin.rs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,48 +1,20 @@
|
|||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use std::f32::consts::FRAC_PI_2;
|
use std::f32::consts::FRAC_PI_2;
|
||||||
|
|
||||||
|
use super::{
|
||||||
|
resource::{Layout, MazeConfig},
|
||||||
|
tile::Tile,
|
||||||
|
};
|
||||||
|
|
||||||
pub(super) fn plugin(app: &mut App) {
|
pub(super) fn plugin(app: &mut App) {
|
||||||
app.add_systems(Update, spawn_light);
|
app.add_systems(Startup, 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.,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn spawn_light(mut commands: Commands) {
|
pub(super) fn spawn_light(mut commands: Commands) {
|
||||||
commands.spawn((
|
commands.spawn((
|
||||||
Name::new("Light Source"),
|
Name::new("Light Source"),
|
||||||
PointLightBundle {
|
PointLightBundle {
|
||||||
point_light: PointLight { ..default() },
|
transform: Transform::from_xyz(0., 50., 0.),
|
||||||
transform: Transform::from_xyz(5., 10., 5.),
|
|
||||||
..default()
|
..default()
|
||||||
},
|
},
|
||||||
));
|
));
|
||||||
@ -52,31 +24,51 @@ pub(super) fn setup(
|
|||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
mut meshes: ResMut<Assets<Mesh>>,
|
mut meshes: ResMut<Assets<Mesh>>,
|
||||||
mut materials: ResMut<Assets<StandardMaterial>>,
|
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 {
|
commands
|
||||||
let hexagon = RegularPolygon {
|
.spawn((
|
||||||
sides: 6,
|
Name::new("Floor"),
|
||||||
circumcircle: Circle::new(params.radius),
|
SpatialBundle {
|
||||||
};
|
transform: Transform::from_translation(Vec3::ZERO),
|
||||||
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)),
|
|
||||||
..default()
|
..default()
|
||||||
},
|
},
|
||||||
Prism {
|
))
|
||||||
radius: params.radius,
|
.with_children(|p| {
|
||||||
height: params.height,
|
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)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,6 +7,7 @@ use rand::{thread_rng, Rng};
|
|||||||
pub struct MazeConfig {
|
pub struct MazeConfig {
|
||||||
pub radius: u32,
|
pub radius: u32,
|
||||||
pub size: f32,
|
pub size: f32,
|
||||||
|
pub height: f32,
|
||||||
pub start_pos: Hex,
|
pub start_pos: Hex,
|
||||||
pub end_pos: Hex,
|
pub end_pos: Hex,
|
||||||
}
|
}
|
||||||
@ -27,7 +28,8 @@ impl Default for MazeConfig {
|
|||||||
debug!("End pos: ({},{})", end_pos.x, end_pos.y);
|
debug!("End pos: ({},{})", end_pos.x, end_pos.y);
|
||||||
Self {
|
Self {
|
||||||
radius: radius as u32,
|
radius: radius as u32,
|
||||||
size: 10.,
|
size: 5.,
|
||||||
|
height: 5.,
|
||||||
start_pos,
|
start_pos,
|
||||||
end_pos,
|
end_pos,
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
|
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use hexx::Hex;
|
use hexx::{Hex, HexLayout};
|
||||||
|
|
||||||
#[derive(Debug, Reflect, Component, Default, PartialEq, Eq, Hash, Clone)]
|
#[derive(Debug, Reflect, Component, Default, PartialEq, Eq, Hash, Clone)]
|
||||||
#[reflect(Component)]
|
#[reflect(Component)]
|
||||||
@ -31,6 +31,15 @@ impl Tile {
|
|||||||
pub fn visit(&mut self) {
|
pub fn visit(&mut self) {
|
||||||
self.visited = true;
|
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 {
|
impl Display for Tile {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user