random button questions
This commit is contained in:
parent
a7f66ba049
commit
85b4949cb4
33
src/main.rs
33
src/main.rs
|
@ -8,6 +8,7 @@ use std::io::Read;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::f64::consts::PI;
|
use std::f64::consts::PI;
|
||||||
use rand::seq::SliceRandom;
|
use rand::seq::SliceRandom;
|
||||||
|
use rand::RngCore;
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
struct StarData {
|
struct StarData {
|
||||||
|
@ -210,20 +211,36 @@ fn player_rotate(
|
||||||
info!("space pressed");
|
info!("space pressed");
|
||||||
|
|
||||||
// Select a random constellation from the Sky's content
|
// Select a random constellation from the Sky's content
|
||||||
if let Some(constellation) = sky.content.choose(&mut rand::thread_rng()) {
|
if sky.content.len() >= 4 {
|
||||||
// Create a target rotation quaternion from the constellation's direction
|
// Select 4 random constellations without repetition
|
||||||
let target_rotation = Quat::from_rotation_arc(Vec3::Z, celestial_to_cartesian(constellation.rah, constellation.dec));
|
let mut rng = rand::thread_rng();
|
||||||
|
let mut selected_constellations = sky.content.clone();
|
||||||
|
selected_constellations.shuffle(&mut rng);
|
||||||
|
let constellations = &selected_constellations[0..4]; // Select 4 constellations
|
||||||
|
|
||||||
|
// Assign the first one as the target
|
||||||
|
let target_index = rng.next_u32().rem_euclid(4) as usize;
|
||||||
|
info!("target const index : {}", target_index);
|
||||||
|
let target_constellation = &constellations[target_index];
|
||||||
|
let target_rotation = Quat::from_rotation_arc(
|
||||||
|
Vec3::Z,
|
||||||
|
celestial_to_cartesian(target_constellation.rah, target_constellation.dec),
|
||||||
|
);
|
||||||
|
|
||||||
// Store the target rotation in the player component
|
// Store the target rotation in the player component
|
||||||
player.target_rotation = Some(target_rotation);
|
player.target_rotation = Some(target_rotation);
|
||||||
player.target_cons_name = Some(constellation.name.clone());
|
player.target_cons_name = Some(target_constellation.name.clone());
|
||||||
|
|
||||||
|
info!("Target constellation: {}", target_constellation.name);
|
||||||
|
|
||||||
info!("constellation : {}", constellation.name);
|
// Iterate over the button text query and assign each a constellation name
|
||||||
|
for (i, mut text) in text_query.iter_mut().enumerate() {
|
||||||
for mut text in &mut text_query {
|
// Assign the name of one of the 4 selected constellations to each button
|
||||||
text.sections[0].value = constellation.name.clone();
|
text.sections[0].value = constellations[i].name.clone();
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// Handle case where there are not enough constellations
|
||||||
|
info!("Not enough constellations in the sky (need 4)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue