mirror of
https://github.com/kristoferssolo/maze-ascension.git
synced 2025-10-21 19:20:34 +00:00
feat(floor): create floor module
This commit is contained in:
parent
72b16dc8bb
commit
3770dcd395
@ -1,5 +1,6 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
maze::{events::RecreateMazeEvent, MazeConfig, MazePluginLoaded},
|
floor::components::{CurrentFloor, Floor},
|
||||||
|
maze::{components::MazeConfig, events::RecreateMazeEvent, GlobalMazeConfig, MazePluginLoaded},
|
||||||
player::events::RespawnPlayer,
|
player::events::RespawnPlayer,
|
||||||
};
|
};
|
||||||
use bevy::{prelude::*, window::PrimaryWindow};
|
use bevy::{prelude::*, window::PrimaryWindow};
|
||||||
@ -22,11 +23,18 @@ pub(crate) fn maze_controls_ui(world: &mut World) {
|
|||||||
else {
|
else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut egui_context = egui_context.clone();
|
let mut egui_context = egui_context.clone();
|
||||||
|
|
||||||
|
let Ok((mut maze_config, floor)) = world
|
||||||
|
.query_filtered::<(&mut MazeConfig, &Floor), With<CurrentFloor>>()
|
||||||
|
.get_single(world)
|
||||||
|
else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
let mut maze_config = (*maze_config).clone();
|
||||||
|
|
||||||
egui::Window::new("Maze Controls").show(egui_context.get_mut(), |ui| {
|
egui::Window::new("Maze Controls").show(egui_context.get_mut(), |ui| {
|
||||||
if let Some(mut maze_config) = world.get_resource_mut::<MazeConfig>() {
|
if let Some(mut global_config) = world.get_resource_mut::<GlobalMazeConfig>() {
|
||||||
let mut changed = false;
|
let mut changed = false;
|
||||||
ui.heading("Maze Configuration");
|
ui.heading("Maze Configuration");
|
||||||
|
|
||||||
@ -34,11 +42,11 @@ pub(crate) fn maze_controls_ui(world: &mut World) {
|
|||||||
|
|
||||||
changed |= add_drag_value_control(ui, "Radius:", &mut maze_config.radius, 1.0, 1..=100);
|
changed |= add_drag_value_control(ui, "Radius:", &mut maze_config.radius, 1.0, 1..=100);
|
||||||
changed |=
|
changed |=
|
||||||
add_drag_value_control(ui, "Height:", &mut maze_config.height, 0.5, 1.0..=50.0);
|
add_drag_value_control(ui, "Height:", &mut global_config.height, 0.5, 1.0..=50.0);
|
||||||
changed |= add_drag_value_control(
|
changed |= add_drag_value_control(
|
||||||
ui,
|
ui,
|
||||||
"Hex Size:",
|
"Hex Size:",
|
||||||
&mut maze_config.hex_size,
|
&mut global_config.hex_size,
|
||||||
1.0,
|
1.0,
|
||||||
1.0..=100.0,
|
1.0..=100.0,
|
||||||
);
|
);
|
||||||
@ -50,11 +58,19 @@ pub(crate) fn maze_controls_ui(world: &mut World) {
|
|||||||
|
|
||||||
// Trigger recreation if any value changed
|
// Trigger recreation if any value changed
|
||||||
if changed {
|
if changed {
|
||||||
maze_config.update();
|
maze_config.update(&global_config);
|
||||||
|
|
||||||
|
if let Ok(mut actual_maze_config) = world
|
||||||
|
.query_filtered::<&mut MazeConfig, With<CurrentFloor>>()
|
||||||
|
.get_single_mut(world)
|
||||||
|
{
|
||||||
|
*actual_maze_config = maze_config;
|
||||||
|
}
|
||||||
|
|
||||||
if let Some(mut event_writer) =
|
if let Some(mut event_writer) =
|
||||||
world.get_resource_mut::<Events<RecreateMazeEvent>>()
|
world.get_resource_mut::<Events<RecreateMazeEvent>>()
|
||||||
{
|
{
|
||||||
event_writer.send(RecreateMazeEvent { floor: 1 });
|
event_writer.send(RecreateMazeEvent { floor: floor.0 });
|
||||||
}
|
}
|
||||||
if let Some(mut event_writer) = world.get_resource_mut::<Events<RespawnPlayer>>() {
|
if let Some(mut event_writer) = world.get_resource_mut::<Events<RespawnPlayer>>() {
|
||||||
event_writer.send(RespawnPlayer);
|
event_writer.send(RespawnPlayer);
|
||||||
|
|||||||
19
src/floor/components.rs
Normal file
19
src/floor/components.rs
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
use bevy::prelude::*;
|
||||||
|
|
||||||
|
#[derive(Debug, Reflect, Component)]
|
||||||
|
#[reflect(Component)]
|
||||||
|
pub struct Floor(pub u8);
|
||||||
|
|
||||||
|
#[derive(Debug, Reflect, Component)]
|
||||||
|
#[reflect(Component)]
|
||||||
|
pub struct TargetFloor(pub u8);
|
||||||
|
|
||||||
|
#[derive(Debug, Reflect, Component)]
|
||||||
|
#[reflect(Component)]
|
||||||
|
pub struct CurrentFloor;
|
||||||
|
|
||||||
|
impl Default for Floor {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
5
src/floor/mod.rs
Normal file
5
src/floor/mod.rs
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
use bevy::prelude::*;
|
||||||
|
|
||||||
|
pub mod components;
|
||||||
|
|
||||||
|
pub(super) fn plugin(app: &mut App) {}
|
||||||
@ -2,8 +2,9 @@ mod asset_tracking;
|
|||||||
pub mod audio;
|
pub mod audio;
|
||||||
#[cfg(feature = "dev")]
|
#[cfg(feature = "dev")]
|
||||||
mod dev_tools;
|
mod dev_tools;
|
||||||
mod maze;
|
pub mod floor;
|
||||||
mod player;
|
pub mod maze;
|
||||||
|
pub mod player;
|
||||||
mod screens;
|
mod screens;
|
||||||
pub mod theme;
|
pub mod theme;
|
||||||
|
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
use super::MazeConfig;
|
use super::resources::GlobalMazeConfig;
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use std::f32::consts::FRAC_PI_2;
|
use std::f32::consts::FRAC_PI_2;
|
||||||
|
|
||||||
@ -17,13 +17,16 @@ impl MazeAssets {
|
|||||||
pub(crate) fn new(
|
pub(crate) fn new(
|
||||||
meshes: &mut ResMut<Assets<Mesh>>,
|
meshes: &mut ResMut<Assets<Mesh>>,
|
||||||
materials: &mut ResMut<Assets<StandardMaterial>>,
|
materials: &mut ResMut<Assets<StandardMaterial>>,
|
||||||
config: &MazeConfig,
|
global_config: &GlobalMazeConfig,
|
||||||
) -> MazeAssets {
|
) -> MazeAssets {
|
||||||
MazeAssets {
|
MazeAssets {
|
||||||
hex_mesh: meshes.add(generate_hex_mesh(config.hex_size, config.height)),
|
hex_mesh: meshes.add(generate_hex_mesh(
|
||||||
|
global_config.hex_size,
|
||||||
|
global_config.height,
|
||||||
|
)),
|
||||||
wall_mesh: meshes.add(generate_square_mesh(
|
wall_mesh: meshes.add(generate_square_mesh(
|
||||||
config.hex_size + config.wall_size() / WALL_OVERLAP_MODIFIER,
|
global_config.hex_size + global_config.wall_size() / WALL_OVERLAP_MODIFIER,
|
||||||
config.wall_size(),
|
global_config.wall_size(),
|
||||||
)),
|
)),
|
||||||
hex_material: materials.add(white_material()),
|
hex_material: materials.add(white_material()),
|
||||||
wall_material: materials.add(Color::BLACK),
|
wall_material: materials.add(Color::BLACK),
|
||||||
|
|||||||
@ -1,18 +1,90 @@
|
|||||||
|
use crate::floor::components::Floor;
|
||||||
|
|
||||||
|
use super::{errors::MazeConfigError, GlobalMazeConfig};
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use hexlab::HexMaze;
|
use hexlab::HexMaze;
|
||||||
|
use hexx::{Hex, HexLayout, HexOrientation};
|
||||||
|
use rand::{rngs::StdRng, thread_rng, Rng, SeedableRng};
|
||||||
|
|
||||||
#[derive(Debug, Reflect, Component)]
|
#[derive(Debug, Reflect, Component)]
|
||||||
#[reflect(Component)]
|
#[reflect(Component)]
|
||||||
pub(crate) struct Maze(pub(crate) HexMaze);
|
#[require(MazeConfig, Floor)]
|
||||||
|
pub struct Maze(pub HexMaze);
|
||||||
|
|
||||||
#[derive(Debug, Reflect, Component)]
|
#[derive(Debug, Reflect, Component)]
|
||||||
#[reflect(Component)]
|
#[reflect(Component)]
|
||||||
pub(crate) struct Floor(pub(crate) u8);
|
pub struct Tile;
|
||||||
|
|
||||||
#[derive(Debug, Reflect, Component)]
|
#[derive(Debug, Reflect, Component)]
|
||||||
#[reflect(Component)]
|
#[reflect(Component)]
|
||||||
pub(crate) struct Tile;
|
pub struct Wall;
|
||||||
|
|
||||||
#[derive(Debug, Reflect, Component)]
|
#[derive(Debug, Reflect, Component, Clone)]
|
||||||
#[reflect(Component)]
|
#[reflect(Component)]
|
||||||
pub(crate) struct Wall;
|
pub struct MazeConfig {
|
||||||
|
pub radius: u32,
|
||||||
|
pub start_pos: Hex,
|
||||||
|
pub end_pos: Hex,
|
||||||
|
pub seed: u64,
|
||||||
|
pub layout: HexLayout,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MazeConfig {
|
||||||
|
fn new(
|
||||||
|
radius: u32,
|
||||||
|
orientation: HexOrientation,
|
||||||
|
seed: Option<u64>,
|
||||||
|
global_conig: &GlobalMazeConfig,
|
||||||
|
) -> Result<Self, MazeConfigError> {
|
||||||
|
let seed = seed.unwrap_or_else(|| thread_rng().gen());
|
||||||
|
let mut rng = StdRng::seed_from_u64(seed);
|
||||||
|
|
||||||
|
let start_pos = generate_pos(radius, &mut rng)?;
|
||||||
|
let end_pos = generate_pos(radius, &mut rng)?;
|
||||||
|
|
||||||
|
info!("Start pos: (q={}, r={})", start_pos.x, start_pos.y);
|
||||||
|
info!("End pos: (q={}, r={})", end_pos.x, end_pos.y);
|
||||||
|
|
||||||
|
let layout = HexLayout {
|
||||||
|
orientation,
|
||||||
|
hex_size: Vec2::splat(global_conig.hex_size),
|
||||||
|
..default()
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(Self {
|
||||||
|
radius,
|
||||||
|
start_pos,
|
||||||
|
end_pos,
|
||||||
|
seed,
|
||||||
|
layout,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_unchecked(
|
||||||
|
radius: u32,
|
||||||
|
orientation: HexOrientation,
|
||||||
|
seed: Option<u64>,
|
||||||
|
global_conig: &GlobalMazeConfig,
|
||||||
|
) -> Self {
|
||||||
|
Self::new(radius, orientation, seed, global_conig)
|
||||||
|
.expect("Failed to create MazeConfig with supposedly safe values")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn update(&mut self, global_conig: &GlobalMazeConfig) {
|
||||||
|
self.layout.hex_size = Vec2::splat(global_conig.hex_size);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for MazeConfig {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::new_unchecked(7, HexOrientation::Flat, None, &GlobalMazeConfig::default())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn generate_pos<R: Rng>(radius: u32, rng: &mut R) -> Result<Hex, MazeConfigError> {
|
||||||
|
let radius = i32::try_from(radius)?;
|
||||||
|
Ok(Hex::new(
|
||||||
|
rng.gen_range(-radius..radius),
|
||||||
|
rng.gen_range(-radius..radius),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|||||||
10
src/maze/errors.rs
Normal file
10
src/maze/errors.rs
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
use std::num::TryFromIntError;
|
||||||
|
use thiserror::Error;
|
||||||
|
|
||||||
|
#[derive(Debug, Error)]
|
||||||
|
pub enum MazeConfigError {
|
||||||
|
#[error("Failed to convert radius from u32 to i32: {0}")]
|
||||||
|
RadiusConverions(#[from] TryFromIntError),
|
||||||
|
#[error("Invalid maze configuration: {0}")]
|
||||||
|
InvalidConfig(String),
|
||||||
|
}
|
||||||
@ -1,6 +1,6 @@
|
|||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
|
||||||
#[derive(Debug, Event)]
|
#[derive(Debug, Event)]
|
||||||
pub(crate) struct RecreateMazeEvent {
|
pub struct RecreateMazeEvent {
|
||||||
pub(crate) floor: u8,
|
pub floor: u8,
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,14 +2,15 @@ use bevy::{ecs::system::RunSystemOnce, prelude::*};
|
|||||||
use events::RecreateMazeEvent;
|
use events::RecreateMazeEvent;
|
||||||
mod assets;
|
mod assets;
|
||||||
pub mod components;
|
pub mod components;
|
||||||
|
pub mod errors;
|
||||||
pub mod events;
|
pub mod events;
|
||||||
mod resources;
|
pub mod resources;
|
||||||
mod systems;
|
mod systems;
|
||||||
|
|
||||||
pub use resources::{MazeConfig, MazePluginLoaded};
|
pub use resources::{GlobalMazeConfig, MazePluginLoaded};
|
||||||
|
|
||||||
pub(super) fn plugin(app: &mut App) {
|
pub(super) fn plugin(app: &mut App) {
|
||||||
app.init_resource::<MazeConfig>()
|
app.init_resource::<GlobalMazeConfig>()
|
||||||
.add_event::<RecreateMazeEvent>()
|
.add_event::<RecreateMazeEvent>()
|
||||||
.add_plugins(systems::plugin);
|
.add_plugins(systems::plugin);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,76 +1,18 @@
|
|||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use hexx::{Hex, HexLayout, HexOrientation};
|
|
||||||
use rand::{rngs::StdRng, thread_rng, Rng, SeedableRng};
|
|
||||||
use std::num::TryFromIntError;
|
|
||||||
use thiserror::Error;
|
|
||||||
|
|
||||||
#[derive(Debug, Default, Reflect, Resource)]
|
#[derive(Debug, Default, Reflect, Resource)]
|
||||||
#[reflect(Resource)]
|
#[reflect(Resource)]
|
||||||
pub struct MazePluginLoaded;
|
pub struct MazePluginLoaded;
|
||||||
|
|
||||||
#[derive(Debug, Error)]
|
|
||||||
pub enum MazeConfigError {
|
|
||||||
#[error("Failed to convert radius from u32 to i32: {0}")]
|
|
||||||
RadiusConverions(#[from] TryFromIntError),
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Reflect, Resource)]
|
#[derive(Debug, Reflect, Resource)]
|
||||||
#[reflect(Resource)]
|
#[reflect(Resource)]
|
||||||
pub struct MazeConfig {
|
pub struct GlobalMazeConfig {
|
||||||
pub radius: u32,
|
|
||||||
pub height: f32,
|
|
||||||
pub hex_size: f32,
|
pub hex_size: f32,
|
||||||
pub start_pos: Hex,
|
pub wall_thickness: f32,
|
||||||
pub end_pos: Hex,
|
pub height: f32,
|
||||||
pub seed: u64,
|
|
||||||
pub layout: HexLayout,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl MazeConfig {
|
|
||||||
fn new(
|
|
||||||
radius: u32,
|
|
||||||
height: f32,
|
|
||||||
hex_size: f32,
|
|
||||||
orientation: HexOrientation,
|
|
||||||
seed: Option<u64>,
|
|
||||||
) -> Result<Self, MazeConfigError> {
|
|
||||||
let seed = seed.unwrap_or_else(|| thread_rng().gen());
|
|
||||||
let mut rng = StdRng::seed_from_u64(seed);
|
|
||||||
|
|
||||||
let start_pos = generate_pos(radius, &mut rng)?;
|
|
||||||
let end_pos = generate_pos(radius, &mut rng)?;
|
|
||||||
|
|
||||||
info!("Start pos: (q={}, r={})", start_pos.x, start_pos.y);
|
|
||||||
info!("End pos: (q={}, r={})", end_pos.x, end_pos.y);
|
|
||||||
|
|
||||||
let layout = HexLayout {
|
|
||||||
orientation,
|
|
||||||
hex_size: Vec2::splat(hex_size),
|
|
||||||
..default()
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(Self {
|
|
||||||
radius,
|
|
||||||
height,
|
|
||||||
hex_size,
|
|
||||||
start_pos,
|
|
||||||
end_pos,
|
|
||||||
seed,
|
|
||||||
layout,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn new_unchecked(
|
|
||||||
radius: u32,
|
|
||||||
height: f32,
|
|
||||||
hex_size: f32,
|
|
||||||
orientation: HexOrientation,
|
|
||||||
seed: Option<u64>,
|
|
||||||
) -> Self {
|
|
||||||
Self::new(radius, height, hex_size, orientation, seed)
|
|
||||||
.expect("Failed to create MazeConfig with supposedly safe values")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl GlobalMazeConfig {
|
||||||
pub fn wall_size(&self) -> f32 {
|
pub fn wall_size(&self) -> f32 {
|
||||||
self.hex_size / 6.
|
self.hex_size / 6.
|
||||||
}
|
}
|
||||||
@ -78,22 +20,14 @@ impl MazeConfig {
|
|||||||
pub fn wall_offset(&self) -> f32 {
|
pub fn wall_offset(&self) -> f32 {
|
||||||
self.hex_size - self.wall_size()
|
self.hex_size - self.wall_size()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(&mut self) {
|
|
||||||
self.layout.hex_size = Vec2::splat(self.hex_size);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for MazeConfig {
|
impl Default for GlobalMazeConfig {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self::new_unchecked(7, 20., 6., HexOrientation::Flat, None)
|
Self {
|
||||||
|
hex_size: 6.,
|
||||||
|
wall_thickness: 1.,
|
||||||
|
height: 20.,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn generate_pos<R: Rng>(radius: u32, rng: &mut R) -> Result<Hex, MazeConfigError> {
|
|
||||||
let radius = i32::try_from(radius)?;
|
|
||||||
Ok(Hex::new(
|
|
||||||
rng.gen_range(-radius..radius),
|
|
||||||
rng.gen_range(-radius..radius),
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
use crate::maze::components::Floor;
|
use crate::floor::components::Floor;
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
|
||||||
pub(crate) fn despawn_floor(
|
pub(crate) fn despawn_floor(
|
||||||
|
|||||||
@ -1,19 +1,29 @@
|
|||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
|
||||||
use crate::maze::{components::Floor, events::RecreateMazeEvent, MazeConfig};
|
use crate::{
|
||||||
|
floor::components::Floor,
|
||||||
|
maze::{components::MazeConfig, events::RecreateMazeEvent, GlobalMazeConfig},
|
||||||
|
};
|
||||||
|
|
||||||
use super::{despawn::despawn_floor, spawn::spawn_floor};
|
use super::{despawn::despawn_floor, spawn::spawn_floor};
|
||||||
|
|
||||||
pub(crate) fn recreate_maze(
|
pub(crate) fn recreate_maze(
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
mut meshes: ResMut<Assets<Mesh>>,
|
mut meshes: ResMut<Assets<Mesh>>,
|
||||||
mut materials: ResMut<Assets<StandardMaterial>>,
|
|
||||||
config: Res<MazeConfig>,
|
|
||||||
query: Query<(Entity, &Floor)>,
|
query: Query<(Entity, &Floor)>,
|
||||||
mut event_reader: EventReader<RecreateMazeEvent>,
|
mut event_reader: EventReader<RecreateMazeEvent>,
|
||||||
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
||||||
|
global_config: Res<GlobalMazeConfig>,
|
||||||
) {
|
) {
|
||||||
|
let maze_config = MazeConfig::default();
|
||||||
for event in event_reader.read() {
|
for event in event_reader.read() {
|
||||||
despawn_floor(&mut commands, &query, event.floor);
|
despawn_floor(&mut commands, &query, event.floor);
|
||||||
spawn_floor(&mut commands, &mut meshes, &mut materials, &config);
|
spawn_floor(
|
||||||
|
&mut commands,
|
||||||
|
&mut meshes,
|
||||||
|
&mut materials,
|
||||||
|
&maze_config,
|
||||||
|
&global_config,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,12 +1,19 @@
|
|||||||
use super::spawn::spawn_floor;
|
use super::spawn::spawn_floor;
|
||||||
use crate::maze::MazeConfig;
|
use crate::maze::{components::MazeConfig, resources::GlobalMazeConfig};
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
|
||||||
pub(crate) fn setup(
|
pub(crate) 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>,
|
global_config: Res<GlobalMazeConfig>,
|
||||||
) {
|
) {
|
||||||
spawn_floor(&mut commands, &mut meshes, &mut materials, &config);
|
let maze_config = MazeConfig::default();
|
||||||
|
spawn_floor(
|
||||||
|
&mut commands,
|
||||||
|
&mut meshes,
|
||||||
|
&mut materials,
|
||||||
|
&maze_config,
|
||||||
|
&global_config,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,7 +1,10 @@
|
|||||||
use crate::maze::{
|
use crate::{
|
||||||
|
floor::components::Floor,
|
||||||
|
maze::{
|
||||||
assets::MazeAssets,
|
assets::MazeAssets,
|
||||||
components::{Floor, Maze, Tile, Wall},
|
components::{Maze, MazeConfig, Tile, Wall},
|
||||||
MazeConfig,
|
resources::GlobalMazeConfig,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use hexlab::prelude::*;
|
use hexlab::prelude::*;
|
||||||
@ -12,16 +15,17 @@ pub(super) fn spawn_floor(
|
|||||||
commands: &mut Commands,
|
commands: &mut Commands,
|
||||||
meshes: &mut ResMut<Assets<Mesh>>,
|
meshes: &mut ResMut<Assets<Mesh>>,
|
||||||
materials: &mut ResMut<Assets<StandardMaterial>>,
|
materials: &mut ResMut<Assets<StandardMaterial>>,
|
||||||
config: &MazeConfig,
|
maze_config: &MazeConfig,
|
||||||
|
global_config: &GlobalMazeConfig,
|
||||||
) {
|
) {
|
||||||
let maze = MazeBuilder::new()
|
let maze = MazeBuilder::new()
|
||||||
.with_radius(config.radius)
|
.with_radius(maze_config.radius)
|
||||||
.with_seed(config.seed)
|
.with_seed(maze_config.seed)
|
||||||
.with_generator(GeneratorType::RecursiveBacktracking)
|
.with_generator(GeneratorType::RecursiveBacktracking)
|
||||||
.build()
|
.build()
|
||||||
.expect("Something went wrong while creating maze");
|
.expect("Something went wrong while creating maze");
|
||||||
|
|
||||||
let assets = MazeAssets::new(meshes, materials, config);
|
let assets = MazeAssets::new(meshes, materials, global_config);
|
||||||
commands
|
commands
|
||||||
.spawn((
|
.spawn((
|
||||||
Name::new("Floor"),
|
Name::new("Floor"),
|
||||||
@ -32,7 +36,7 @@ pub(super) fn spawn_floor(
|
|||||||
))
|
))
|
||||||
.with_children(|parent| {
|
.with_children(|parent| {
|
||||||
for tile in maze.values() {
|
for tile in maze.values() {
|
||||||
spawn_single_hex_tile(parent, &assets, tile, config)
|
spawn_single_hex_tile(parent, &assets, tile, maze_config, global_config)
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -41,10 +45,11 @@ pub(super) fn spawn_single_hex_tile(
|
|||||||
parent: &mut ChildBuilder,
|
parent: &mut ChildBuilder,
|
||||||
assets: &MazeAssets,
|
assets: &MazeAssets,
|
||||||
tile: &HexTile,
|
tile: &HexTile,
|
||||||
config: &MazeConfig,
|
maze_config: &MazeConfig,
|
||||||
|
global_config: &GlobalMazeConfig,
|
||||||
) {
|
) {
|
||||||
let world_pos = tile.to_vec3(&config.layout);
|
let world_pos = tile.to_vec3(&maze_config.layout);
|
||||||
let rotation = match config.layout.orientation {
|
let rotation = match maze_config.layout.orientation {
|
||||||
HexOrientation::Pointy => Quat::from_rotation_y(0.0),
|
HexOrientation::Pointy => Quat::from_rotation_y(0.0),
|
||||||
HexOrientation::Flat => Quat::from_rotation_y(FRAC_PI_6), // 30 degrees rotation
|
HexOrientation::Flat => Quat::from_rotation_y(FRAC_PI_6), // 30 degrees rotation
|
||||||
};
|
};
|
||||||
@ -57,12 +62,17 @@ pub(super) fn spawn_single_hex_tile(
|
|||||||
MeshMaterial3d(assets.hex_material.clone()),
|
MeshMaterial3d(assets.hex_material.clone()),
|
||||||
Transform::from_translation(world_pos).with_rotation(rotation),
|
Transform::from_translation(world_pos).with_rotation(rotation),
|
||||||
))
|
))
|
||||||
.with_children(|parent| spawn_walls(parent, assets, config, tile.walls()));
|
.with_children(|parent| spawn_walls(parent, assets, tile.walls(), global_config));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn spawn_walls(parent: &mut ChildBuilder, assets: &MazeAssets, config: &MazeConfig, walls: &Walls) {
|
fn spawn_walls(
|
||||||
|
parent: &mut ChildBuilder,
|
||||||
|
assets: &MazeAssets,
|
||||||
|
walls: &Walls,
|
||||||
|
global_config: &GlobalMazeConfig,
|
||||||
|
) {
|
||||||
let z_rotation = Quat::from_rotation_z(-FRAC_PI_2);
|
let z_rotation = Quat::from_rotation_z(-FRAC_PI_2);
|
||||||
let y_offset = config.height / 2.;
|
let y_offset = global_config.height / 2.;
|
||||||
|
|
||||||
for i in 0..6 {
|
for i in 0..6 {
|
||||||
if !walls.contains(i) {
|
if !walls.contains(i) {
|
||||||
@ -71,8 +81,8 @@ fn spawn_walls(parent: &mut ChildBuilder, assets: &MazeAssets, config: &MazeConf
|
|||||||
|
|
||||||
let wall_angle = -FRAC_PI_3 * i as f32;
|
let wall_angle = -FRAC_PI_3 * i as f32;
|
||||||
|
|
||||||
let x_offset = config.wall_offset() * f32::cos(wall_angle);
|
let x_offset = global_config.wall_offset() * f32::cos(wall_angle);
|
||||||
let z_offset = config.wall_offset() * f32::sin(wall_angle);
|
let z_offset = global_config.wall_offset() * f32::sin(wall_angle);
|
||||||
let pos = Vec3::new(x_offset, y_offset, z_offset);
|
let pos = Vec3::new(x_offset, y_offset, z_offset);
|
||||||
|
|
||||||
let x_rotation = Quat::from_rotation_x(wall_angle + FRAC_PI_2);
|
let x_rotation = Quat::from_rotation_x(wall_angle + FRAC_PI_2);
|
||||||
|
|||||||
@ -1,8 +1,6 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
maze::{
|
floor::components::{CurrentFloor, Floor},
|
||||||
components::{Floor, Maze},
|
maze::components::{Maze, MazeConfig},
|
||||||
MazeConfig,
|
|
||||||
},
|
|
||||||
player::components::{CurrentPosition, MovementTarget, Player},
|
player::components::{CurrentPosition, MovementTarget, Player},
|
||||||
};
|
};
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
@ -47,10 +45,9 @@ fn create_direction(
|
|||||||
pub(super) fn player_input(
|
pub(super) fn player_input(
|
||||||
input: Res<ButtonInput<KeyCode>>,
|
input: Res<ButtonInput<KeyCode>>,
|
||||||
mut player_query: Query<(&mut MovementTarget, &CurrentPosition), With<Player>>,
|
mut player_query: Query<(&mut MovementTarget, &CurrentPosition), With<Player>>,
|
||||||
maze_query: Query<(&Maze, &Floor)>,
|
maze_query: Query<(&Maze, &MazeConfig), With<CurrentFloor>>,
|
||||||
maze_config: Res<MazeConfig>,
|
|
||||||
) {
|
) {
|
||||||
let Ok((maze, _floor)) = maze_query.get_single() else {
|
let Ok((maze, maze_config)) = maze_query.get_single() else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
maze::MazeConfig,
|
floor::components::CurrentFloor,
|
||||||
|
maze::components::MazeConfig,
|
||||||
player::components::{CurrentPosition, MovementSpeed, MovementTarget, Player},
|
player::components::{CurrentPosition, MovementSpeed, MovementTarget, Player},
|
||||||
};
|
};
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
@ -16,8 +17,9 @@ pub(super) fn player_movement(
|
|||||||
),
|
),
|
||||||
With<Player>,
|
With<Player>,
|
||||||
>,
|
>,
|
||||||
maze_config: Res<MazeConfig>,
|
maze_config_query: Query<&MazeConfig, With<CurrentFloor>>,
|
||||||
) {
|
) {
|
||||||
|
let maze_config = maze_config_query.single();
|
||||||
for (mut target, speed, mut current_hex, mut transform) in query.iter_mut() {
|
for (mut target, speed, mut current_hex, mut transform) in query.iter_mut() {
|
||||||
if let Some(target_hex) = target.0 {
|
if let Some(target_hex) = target.0 {
|
||||||
let current_pos = transform.translation;
|
let current_pos = transform.translation;
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
maze::MazeConfig,
|
floor::components::CurrentFloor,
|
||||||
|
maze::{components::MazeConfig, GlobalMazeConfig},
|
||||||
player::{components::Player, events::RespawnPlayer},
|
player::{components::Player, events::RespawnPlayer},
|
||||||
};
|
};
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
@ -8,14 +9,22 @@ use super::{despawn::despawn_players, spawn::spawn_player};
|
|||||||
|
|
||||||
pub(crate) fn respawn_player(
|
pub(crate) fn respawn_player(
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
|
query: Query<Entity, With<Player>>,
|
||||||
|
maze_config_query: Query<&MazeConfig, With<CurrentFloor>>,
|
||||||
|
mut event_reader: EventReader<RespawnPlayer>,
|
||||||
mut meshes: ResMut<Assets<Mesh>>,
|
mut meshes: ResMut<Assets<Mesh>>,
|
||||||
mut materials: ResMut<Assets<StandardMaterial>>,
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
||||||
config: Res<MazeConfig>,
|
global_config: Res<GlobalMazeConfig>,
|
||||||
query: Query<Entity, With<Player>>,
|
|
||||||
mut event_reader: EventReader<RespawnPlayer>,
|
|
||||||
) {
|
) {
|
||||||
|
let maze_config = maze_config_query.single();
|
||||||
for _ in event_reader.read() {
|
for _ in event_reader.read() {
|
||||||
despawn_players(&mut commands, &query);
|
despawn_players(&mut commands, &query);
|
||||||
spawn_player(&mut commands, &mut meshes, &mut materials, &config);
|
spawn_player(
|
||||||
|
&mut commands,
|
||||||
|
&mut meshes,
|
||||||
|
&mut materials,
|
||||||
|
&maze_config,
|
||||||
|
&global_config,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +1,9 @@
|
|||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
|
||||||
use crate::maze::MazeConfig;
|
use crate::{
|
||||||
|
floor::components::CurrentFloor,
|
||||||
|
maze::{components::MazeConfig, GlobalMazeConfig},
|
||||||
|
};
|
||||||
|
|
||||||
use super::spawn::spawn_player;
|
use super::spawn::spawn_player;
|
||||||
|
|
||||||
@ -8,7 +11,15 @@ pub(crate) 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>,
|
maze_config_query: Query<&MazeConfig, With<CurrentFloor>>,
|
||||||
|
global_config: Res<GlobalMazeConfig>,
|
||||||
) {
|
) {
|
||||||
spawn_player(&mut commands, &mut meshes, &mut materials, &config);
|
let maze_config = maze_config_query.single();
|
||||||
|
spawn_player(
|
||||||
|
&mut commands,
|
||||||
|
&mut meshes,
|
||||||
|
&mut materials,
|
||||||
|
&maze_config,
|
||||||
|
&global_config,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
maze::MazeConfig,
|
maze::{components::MazeConfig, GlobalMazeConfig},
|
||||||
player::{
|
player::{
|
||||||
assets::{blue_material, generate_pill_mesh},
|
assets::{blue_material, generate_pill_mesh},
|
||||||
components::{CurrentPosition, Player},
|
components::{CurrentPosition, Player},
|
||||||
@ -11,19 +11,20 @@ pub(super) fn spawn_player(
|
|||||||
commands: &mut Commands,
|
commands: &mut Commands,
|
||||||
meshes: &mut ResMut<Assets<Mesh>>,
|
meshes: &mut ResMut<Assets<Mesh>>,
|
||||||
materials: &mut ResMut<Assets<StandardMaterial>>,
|
materials: &mut ResMut<Assets<StandardMaterial>>,
|
||||||
config: &MazeConfig,
|
maze_config: &MazeConfig,
|
||||||
|
global_config: &GlobalMazeConfig,
|
||||||
) {
|
) {
|
||||||
let player_radius = config.hex_size * 0.5;
|
let player_radius = global_config.hex_size * 0.5;
|
||||||
let player_height = player_radius * 3.5;
|
let player_height = player_radius * 3.5;
|
||||||
|
|
||||||
let y_offset = config.height / 2. + player_height / 1.3;
|
let y_offset = global_config.height / 2. + player_height / 1.3;
|
||||||
|
|
||||||
let start_pos = config.layout.hex_to_world_pos(config.start_pos);
|
let start_pos = maze_config.layout.hex_to_world_pos(maze_config.start_pos);
|
||||||
|
|
||||||
commands.spawn((
|
commands.spawn((
|
||||||
Name::new("Player"),
|
Name::new("Player"),
|
||||||
Player,
|
Player,
|
||||||
CurrentPosition(config.start_pos),
|
CurrentPosition(maze_config.start_pos),
|
||||||
Mesh3d(meshes.add(generate_pill_mesh(player_radius, player_height / 2.))),
|
Mesh3d(meshes.add(generate_pill_mesh(player_radius, player_height / 2.))),
|
||||||
MeshMaterial3d(materials.add(blue_material())),
|
MeshMaterial3d(materials.add(blue_material())),
|
||||||
Transform::from_xyz(start_pos.x, y_offset, start_pos.y),
|
Transform::from_xyz(start_pos.x, y_offset, start_pos.y),
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user