From 4d37a547ffdf074eba2379a62d5a29fb54c5ba44 Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Fri, 10 Jan 2025 19:19:13 +0200 Subject: [PATCH] fix(floor): issue #29 --- src/floor/systems/spawn.rs | 6 +---- src/maze/components.rs | 51 ++++++++++++++++++++++++++++++-------- src/maze/systems/spawn.rs | 1 + src/player/components.rs | 2 +- 4 files changed, 44 insertions(+), 16 deletions(-) diff --git a/src/floor/systems/spawn.rs b/src/floor/systems/spawn.rs index 308598b..cd6e88c 100644 --- a/src/floor/systems/spawn.rs +++ b/src/floor/systems/spawn.rs @@ -32,11 +32,7 @@ pub fn spawn_floor( commands.queue(SpawnMaze { floor: target_floor, - config: MazeConfig { - start_pos: config.end_pos, - radius: config.radius + 1, - ..default() - }, + config: MazeConfig::from_self(config), }); } } diff --git a/src/maze/components.rs b/src/maze/components.rs index 8ead6ce..48cd62b 100644 --- a/src/maze/components.rs +++ b/src/maze/components.rs @@ -52,20 +52,12 @@ impl MazeConfig { global_config: &GlobalMazeConfig, start_pos: Option, ) -> Self { - let seed = seed.unwrap_or_else(|| thread_rng().gen()); - let mut rng = StdRng::seed_from_u64(seed); + let (seed, mut rng) = setup_rng(seed); let start_pos = start_pos.unwrap_or_else(|| generate_pos(radius, &mut rng)); // Generate end position ensuring start and end are different - let mut end_pos; - loop { - end_pos = generate_pos(radius, &mut rng); - if start_pos != end_pos { - break; - } - } - dbg!(seed, start_pos, end_pos); + let end_pos = generate_end_pos(radius, start_pos, &mut rng); let layout = HexLayout { orientation, @@ -82,12 +74,35 @@ impl MazeConfig { } } + pub fn from_self(config: &Self) -> Self { + let start_pos = config.end_pos; + let (seed, mut rng) = setup_rng(None); + + let end_pos = generate_end_pos(config.radius, start_pos, &mut rng); + + Self { + radius: config.radius + 1, + start_pos, + end_pos, + seed, + layout: config.layout.clone(), + } + } + /// Updates the maze configuration with new global settings. pub fn update(&mut self, global_conig: &GlobalMazeConfig) { self.layout.hex_size = Vec2::splat(global_conig.hex_size); } } +// TO +// 3928551514041614914 +// (4, 0) + +// FROM +// 7365371276044996661 +// () + impl Default for MazeConfig { fn default() -> Self { Self::new( @@ -100,6 +115,22 @@ impl Default for MazeConfig { } } +fn setup_rng(seed: Option) -> (u64, StdRng) { + let seed = seed.unwrap_or_else(|| thread_rng().gen()); + let rng = StdRng::seed_from_u64(seed); + (seed, rng) +} + +fn generate_end_pos(radius: u16, start_pos: Hex, rng: &mut R) -> Hex { + let mut end_pos; + loop { + end_pos = generate_pos(radius, rng); + if start_pos != end_pos { + return end_pos; + } + } +} + /// Generates a random position within a hexagonal radius. /// /// # Returns diff --git a/src/maze/systems/spawn.rs b/src/maze/systems/spawn.rs index b33f932..98c889a 100644 --- a/src/maze/systems/spawn.rs +++ b/src/maze/systems/spawn.rs @@ -68,6 +68,7 @@ pub fn spawn_maze( .id(); let assets = MazeAssets::new(&mut meshes, &mut materials, &global_config); + spawn_maze_tiles( &mut commands, entity, diff --git a/src/player/components.rs b/src/player/components.rs index 26f8a8b..bf3b885 100644 --- a/src/player/components.rs +++ b/src/player/components.rs @@ -16,7 +16,7 @@ pub struct MovementSpeed(pub f32); impl Default for MovementSpeed { fn default() -> Self { - Self(100.) + Self(200.) } }