mirror of
https://github.com/kristoferssolo/maze-ascension.git
synced 2025-10-21 19:20:34 +00:00
feat(floor): synchronize floor start/end positions #13
This commit is contained in:
parent
e15c055f06
commit
a4e819b4b6
@ -4,17 +4,17 @@ use crate::{
|
|||||||
events::TransitionFloor,
|
events::TransitionFloor,
|
||||||
resources::HighestFloor,
|
resources::HighestFloor,
|
||||||
},
|
},
|
||||||
maze::events::SpawnMaze,
|
maze::{components::MazeConfig, events::SpawnMaze},
|
||||||
};
|
};
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
|
||||||
pub(super) fn spawn_floor(
|
pub(super) fn spawn_floor(
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
query: Query<&mut Floor, (With<CurrentFloor>, Without<FloorYTarget>)>,
|
query: Query<(&mut Floor, &MazeConfig), (With<CurrentFloor>, Without<FloorYTarget>)>,
|
||||||
mut event_reader: EventReader<TransitionFloor>,
|
mut event_reader: EventReader<TransitionFloor>,
|
||||||
mut highest_floor: ResMut<HighestFloor>,
|
mut highest_floor: ResMut<HighestFloor>,
|
||||||
) {
|
) {
|
||||||
let Ok(current_floor) = query.get_single() else {
|
let Ok((current_floor, config)) = query.get_single() else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -24,14 +24,17 @@ pub(super) fn spawn_floor(
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let next_floor = event.next_floor_num(current_floor);
|
let target_floor = event.next_floor_num(current_floor);
|
||||||
highest_floor.0 = highest_floor.0.max(next_floor);
|
highest_floor.0 = highest_floor.0.max(target_floor);
|
||||||
|
|
||||||
info!("Creating level for floor {}", next_floor);
|
info!("Creating level for floor {}", target_floor);
|
||||||
|
|
||||||
commands.trigger(SpawnMaze {
|
commands.trigger(SpawnMaze {
|
||||||
floor: next_floor,
|
floor: target_floor,
|
||||||
|
config: MazeConfig {
|
||||||
|
start_pos: config.end_pos,
|
||||||
..default()
|
..default()
|
||||||
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -35,18 +35,17 @@ impl MazeConfig {
|
|||||||
orientation: HexOrientation,
|
orientation: HexOrientation,
|
||||||
seed: Option<u64>,
|
seed: Option<u64>,
|
||||||
global_conig: &GlobalMazeConfig,
|
global_conig: &GlobalMazeConfig,
|
||||||
|
start_pos: Option<Hex>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let seed = seed.unwrap_or_else(|| thread_rng().gen());
|
let seed = seed.unwrap_or_else(|| thread_rng().gen());
|
||||||
let mut rng = StdRng::seed_from_u64(seed);
|
let mut rng = StdRng::seed_from_u64(seed);
|
||||||
|
|
||||||
// Generate start and end positions ensuring they're different
|
let start_pos = start_pos.unwrap_or_else(|| generate_pos(radius, &mut rng));
|
||||||
let mut start_pos;
|
|
||||||
|
// Generate end position ensuring start and end are different
|
||||||
let mut end_pos;
|
let mut end_pos;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
start_pos = generate_pos(radius, &mut rng);
|
|
||||||
end_pos = generate_pos(radius, &mut rng);
|
end_pos = generate_pos(radius, &mut rng);
|
||||||
|
|
||||||
if start_pos != end_pos {
|
if start_pos != end_pos {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -79,7 +78,13 @@ impl MazeConfig {
|
|||||||
|
|
||||||
impl Default for MazeConfig {
|
impl Default for MazeConfig {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self::new(8, HexOrientation::Flat, None, &GlobalMazeConfig::default())
|
Self::new(
|
||||||
|
8,
|
||||||
|
HexOrientation::Flat,
|
||||||
|
None,
|
||||||
|
&GlobalMazeConfig::default(),
|
||||||
|
None,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -126,7 +131,7 @@ mod tests {
|
|||||||
let seed = Some(12345);
|
let seed = Some(12345);
|
||||||
let global_config = GlobalMazeConfig::default();
|
let global_config = GlobalMazeConfig::default();
|
||||||
|
|
||||||
let config = MazeConfig::new(radius, orientation, seed, &global_config);
|
let config = MazeConfig::new(radius, orientation, seed, &global_config, None);
|
||||||
|
|
||||||
assert_eq!(config.radius, radius);
|
assert_eq!(config.radius, radius);
|
||||||
assert_eq!(config.seed, 12345);
|
assert_eq!(config.seed, 12345);
|
||||||
@ -173,8 +178,13 @@ mod tests {
|
|||||||
];
|
];
|
||||||
|
|
||||||
for seed in test_seeds {
|
for seed in test_seeds {
|
||||||
let config =
|
let config = MazeConfig::new(
|
||||||
MazeConfig::new(8, HexOrientation::Flat, seed, &GlobalMazeConfig::default());
|
8,
|
||||||
|
HexOrientation::Flat,
|
||||||
|
seed,
|
||||||
|
&GlobalMazeConfig::default(),
|
||||||
|
None,
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(config.radius, 8);
|
assert_eq!(config.radius, 8);
|
||||||
assert_eq!(config.layout.orientation, HexOrientation::Flat);
|
assert_eq!(config.layout.orientation, HexOrientation::Flat);
|
||||||
@ -226,12 +236,14 @@ mod tests {
|
|||||||
HexOrientation::Flat,
|
HexOrientation::Flat,
|
||||||
Some(1),
|
Some(1),
|
||||||
&GlobalMazeConfig::default(),
|
&GlobalMazeConfig::default(),
|
||||||
|
None,
|
||||||
);
|
);
|
||||||
let config2 = MazeConfig::new(
|
let config2 = MazeConfig::new(
|
||||||
8,
|
8,
|
||||||
HexOrientation::Flat,
|
HexOrientation::Flat,
|
||||||
Some(2),
|
Some(2),
|
||||||
&GlobalMazeConfig::default(),
|
&GlobalMazeConfig::default(),
|
||||||
|
None,
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_ne!(config1.start_pos, config2.start_pos);
|
assert_ne!(config1.start_pos, config2.start_pos);
|
||||||
@ -241,8 +253,20 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn same_seed_same_positions() {
|
fn same_seed_same_positions() {
|
||||||
let seed = Some(12345);
|
let seed = Some(12345);
|
||||||
let config1 = MazeConfig::new(8, HexOrientation::Flat, seed, &GlobalMazeConfig::default());
|
let config1 = MazeConfig::new(
|
||||||
let config2 = MazeConfig::new(8, HexOrientation::Flat, seed, &GlobalMazeConfig::default());
|
8,
|
||||||
|
HexOrientation::Flat,
|
||||||
|
seed,
|
||||||
|
&GlobalMazeConfig::default(),
|
||||||
|
None,
|
||||||
|
);
|
||||||
|
let config2 = MazeConfig::new(
|
||||||
|
8,
|
||||||
|
HexOrientation::Flat,
|
||||||
|
seed,
|
||||||
|
&GlobalMazeConfig::default(),
|
||||||
|
None,
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(config1.start_pos, config2.start_pos);
|
assert_eq!(config1.start_pos, config2.start_pos);
|
||||||
assert_eq!(config1.end_pos, config2.end_pos);
|
assert_eq!(config1.end_pos, config2.end_pos);
|
||||||
@ -255,6 +279,7 @@ mod tests {
|
|||||||
HexOrientation::Pointy,
|
HexOrientation::Pointy,
|
||||||
None,
|
None,
|
||||||
&GlobalMazeConfig::default(),
|
&GlobalMazeConfig::default(),
|
||||||
|
None,
|
||||||
);
|
);
|
||||||
assert_eq!(config.layout.orientation, HexOrientation::Pointy);
|
assert_eq!(config.layout.orientation, HexOrientation::Pointy);
|
||||||
}
|
}
|
||||||
@ -269,6 +294,7 @@ mod tests {
|
|||||||
hex_size: 0.0,
|
hex_size: 0.0,
|
||||||
..default()
|
..default()
|
||||||
},
|
},
|
||||||
|
None,
|
||||||
);
|
);
|
||||||
assert_eq!(config.layout.hex_size.x, 0.0);
|
assert_eq!(config.layout.hex_size.x, 0.0);
|
||||||
assert_eq!(config.layout.hex_size.y, 0.0);
|
assert_eq!(config.layout.hex_size.y, 0.0);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user