fix(maze): rendering

This commit is contained in:
Kristofers Solo 2024-12-14 20:04:55 +02:00
parent 3751ef3ee3
commit 5c14631e53
7 changed files with 62 additions and 44 deletions

View File

@ -34,11 +34,10 @@ pub(crate) fn maze_controls_ui(world: &mut World) {
let mut maze_config = maze_config.clone(); let mut maze_config = maze_config.clone();
let floor_value = floor.0; let floor_value = floor.0;
let global_config = world.get_resource::<GlobalMazeConfig>().cloned();
let mut changed = false; let mut changed = false;
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 global_config) = global_config.clone() { if let Some(mut global_config) = world.get_resource_mut::<GlobalMazeConfig>() {
ui.heading("Maze Configuration"); ui.heading("Maze Configuration");
changed |= add_seed_control(ui, &mut maze_config.seed); changed |= add_seed_control(ui, &mut maze_config.seed);

View File

@ -0,0 +1,14 @@
use crate::maze::{
components::MazeConfig,
errors::{MazeError, MazeResult},
};
use hexlab::{GeneratorType, HexMaze, MazeBuilder};
pub(super) fn generate_maze(config: &MazeConfig) -> MazeResult<HexMaze> {
Ok(MazeBuilder::new()
.with_radius(config.radius)
.with_seed(config.seed)
.with_generator(GeneratorType::RecursiveBacktracking)
.build()
.map_err(|_| MazeError::generation_failed(config.radius, config.seed))?)
}

View File

@ -10,13 +10,13 @@ pub(crate) fn handle_maze_events(
mut meshes: ResMut<Assets<Mesh>>, mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>, mut materials: ResMut<Assets<StandardMaterial>>,
mut event_reader: EventReader<MazeEvent>, mut event_reader: EventReader<MazeEvent>,
mut maze_query: Query<(Entity, &Floor, &Children, &mut Maze)>, mut maze_query: Query<(Entity, &Floor, &mut Maze)>,
global_config: Res<GlobalMazeConfig>, global_config: Res<GlobalMazeConfig>,
) { ) {
for event in event_reader.read() { for event in event_reader.read() {
match event { match event {
MazeEvent::Create { floor, config } => { MazeEvent::Create { floor, config } => {
if maze_query.iter().any(|(_, f, _, _)| f.0 == *floor) { if maze_query.iter().any(|(_, f, _)| f.0 == *floor) {
warn!("Floor {} already exists, skipping creation", floor); warn!("Floor {} already exists, skipping creation", floor);
continue; continue;
} }
@ -44,8 +44,8 @@ pub(crate) fn handle_maze_events(
} }
} }
MazeEvent::Remove { floor } => { MazeEvent::Remove { floor } => {
match maze_query.iter().find(|(_, f, _, _)| f.0 == *floor) { match maze_query.iter().find(|(_, f, _)| f.0 == *floor) {
Some((entity, _, _, _)) => commands.entity(entity).despawn_recursive(), Some((entity, _, _)) => commands.entity(entity).despawn_recursive(),
_ => warn!("Floor {} not found for removal", floor), _ => warn!("Floor {} not found for removal", floor),
} }
} }

View File

@ -1,3 +1,4 @@
pub mod common;
pub mod event_handler; pub mod event_handler;
pub mod setup; pub mod setup;
pub mod spawn; pub mod spawn;

View File

@ -11,6 +11,8 @@ use hexlab::prelude::*;
use hexx::HexOrientation; use hexx::HexOrientation;
use std::f32::consts::{FRAC_PI_2, FRAC_PI_3, FRAC_PI_6}; use std::f32::consts::{FRAC_PI_2, FRAC_PI_3, FRAC_PI_6};
use super::common::generate_maze;
pub(super) fn spawn_floor( pub(super) fn spawn_floor(
commands: &mut Commands, commands: &mut Commands,
meshes: &mut ResMut<Assets<Mesh>>, meshes: &mut ResMut<Assets<Mesh>>,
@ -19,15 +21,9 @@ pub(super) fn spawn_floor(
maze_config: &MazeConfig, maze_config: &MazeConfig,
global_config: &GlobalMazeConfig, global_config: &GlobalMazeConfig,
) { ) {
let maze = MazeBuilder::new() let maze = generate_maze(maze_config).expect("Failed to generate maze during spawn");
.with_radius(maze_config.radius)
.with_seed(maze_config.seed)
.with_generator(GeneratorType::RecursiveBacktracking)
.build()
.expect("Something went wrong while creating maze");
let assets = MazeAssets::new(meshes, materials, &global_config); let entity = commands
commands
.spawn(( .spawn((
Name::new(format!("Floor {}", floor)), Name::new(format!("Floor {}", floor)),
Maze(maze.clone()), Maze(maze.clone()),
@ -37,9 +33,23 @@ pub(super) fn spawn_floor(
Transform::from_translation(Vec3::ZERO), Transform::from_translation(Vec3::ZERO),
Visibility::Visible, Visibility::Visible,
)) ))
.with_children(|parent| { .id();
let assets = MazeAssets::new(meshes, materials, global_config);
spawn_maze_tiles(commands, entity, &maze, &assets, maze_config, global_config);
}
pub(super) fn spawn_maze_tiles(
commands: &mut Commands,
parent_entity: Entity,
maze: &HexMaze,
assets: &MazeAssets,
maze_config: &MazeConfig,
global_config: &GlobalMazeConfig,
) {
commands.entity(parent_entity).with_children(|parent| {
for tile in maze.values() { for tile in maze.values() {
spawn_single_hex_tile(parent, &assets, tile, &maze_config, &global_config) spawn_single_hex_tile(parent, &assets, tile, maze_config, global_config);
} }
}); });
} }

View File

@ -1,3 +1,7 @@
use super::{
common::generate_maze,
spawn::{spawn_maze_tiles, spawn_single_hex_tile},
};
use crate::{ use crate::{
floor::components::Floor, floor::components::Floor,
maze::{ maze::{
@ -8,45 +12,35 @@ use crate::{
}, },
}; };
use bevy::prelude::*; use bevy::prelude::*;
use hexlab::{GeneratorType, MazeBuilder};
use super::spawn::spawn_single_hex_tile;
pub(super) fn update_floor( pub(super) fn update_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>>,
maze_query: &mut Query<(Entity, &Floor, &Children, &mut Maze)>, maze_query: &mut Query<(Entity, &Floor, &mut Maze)>,
floor: u8, floor: u8,
maze_config: &MazeConfig, maze_config: &MazeConfig,
global_config: &GlobalMazeConfig, global_config: &GlobalMazeConfig,
) -> MazeResult<()> { ) -> MazeResult<()> {
let (entity, _, children, mut maze) = maze_query let (entity, _, mut maze) = maze_query
.iter_mut() .iter_mut()
.find(|(_, f, _, _)| f.0 == floor) .find(|(_, f, _)| f.0 == floor)
.ok_or(MazeError::FloorNotFound(floor))?; .ok_or(MazeError::FloorNotFound(floor))?;
let new_maze = MazeBuilder::new() maze.0 = generate_maze(maze_config)?;
.with_radius(maze_config.radius)
.with_seed(maze_config.seed)
.with_generator(GeneratorType::RecursiveBacktracking)
.build()
.map_err(|_| MazeError::generation_failed(maze_config.radius, maze_config.seed))?;
let new_maze = Maze(new_maze); commands.entity(entity).despawn_descendants();
commands.entity(entity).clear_children();
for &child in children.iter() {
commands.entity(child).despawn_recursive();
}
let assets = MazeAssets::new(meshes, materials, global_config); let assets = MazeAssets::new(meshes, materials, global_config);
commands.entity(entity).with_children(|parent| { spawn_maze_tiles(
for tile in new_maze.0.values() { commands,
spawn_single_hex_tile(parent, &assets, tile, maze_config, global_config); entity,
} &maze.0,
}); &assets,
maze_config,
global_config,
);
*maze = new_maze;
commands.entity(entity).insert(maze_config.clone()); commands.entity(entity).insert(maze_config.clone());
Ok(()) Ok(())
} }

View File

@ -1,5 +1,5 @@
use crate::{ use crate::{
floor::components::{CurrentFloor, Floor}, floor::components::CurrentFloor,
maze::components::{Maze, MazeConfig}, maze::components::{Maze, MazeConfig},
player::components::{CurrentPosition, MovementTarget, Player}, player::components::{CurrentPosition, MovementTarget, Player},
}; };