constellation name to button

This commit is contained in:
WanderingPenwing 2024-10-06 09:28:13 +02:00
parent 245cf2b5a2
commit a7f66ba049

View file

@ -75,9 +75,13 @@ fn main() {
#[derive(Component)] #[derive(Component)]
struct Star; struct Star;
#[derive(Component)]
struct AnswerButton;
#[derive(Component)] #[derive(Component)]
struct Player { struct Player {
target_rotation: Option<Quat>, target_rotation: Option<Quat>,
target_cons_name: Option<String>,
} }
fn star_setup( fn star_setup(
@ -130,14 +134,13 @@ fn star_setup(
..default() ..default()
}, },
Player { Player {
target_rotation: Some(Quat::from_rotation_y(-1.5)) target_rotation: None,
target_cons_name: None,
}, },
)); ));
} }
fn cons_setup(mut sky: ResMut<Sky>) { fn cons_setup(mut sky: ResMut<Sky>) {
info!("setup");
sky.content = get_cons().unwrap(); sky.content = get_cons().unwrap();
} }
@ -146,8 +149,6 @@ fn get_stars() -> std::io::Result<Vec<StarData>> {
let mut data = String::new(); let mut data = String::new();
file.read_to_string(&mut data)?; file.read_to_string(&mut data)?;
info!("###");
let stars: Vec<StarData> = serde_json::from_str(&data).unwrap(); let stars: Vec<StarData> = serde_json::from_str(&data).unwrap();
Ok(stars) Ok(stars)
@ -158,8 +159,6 @@ fn get_cons() -> std::io::Result<Vec<Constellation>> {
let mut data = String::new(); let mut data = String::new();
file.read_to_string(&mut data)?; file.read_to_string(&mut data)?;
info!("###");
let sky_data: Vec<Constellation> = serde_json::from_str(&data).unwrap(); let sky_data: Vec<Constellation> = serde_json::from_str(&data).unwrap();
Ok(sky_data) Ok(sky_data)
@ -203,7 +202,7 @@ fn player_rotate(
keys: Res<ButtonInput<KeyCode>>, keys: Res<ButtonInput<KeyCode>>,
mut query: Query<(&mut Player, &mut Transform)>, // Query to get Player and Transform mut query: Query<(&mut Player, &mut Transform)>, // Query to get Player and Transform
sky: Res<Sky>, // Res to access the Sky resource sky: Res<Sky>, // Res to access the Sky resource
//mut commands: Commands, mut text_query: Query<&mut Text, With<AnswerButton>>,
) { ) {
for (mut player, mut transform) in query.iter_mut() { for (mut player, mut transform) in query.iter_mut() {
// If the space key was just pressed // If the space key was just pressed
@ -217,6 +216,14 @@ fn player_rotate(
// 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());
info!("constellation : {}", constellation.name);
for mut text in &mut text_query {
text.sections[0].value = constellation.name.clone();
}
} }
} }
@ -289,7 +296,7 @@ fn ui_setup(mut commands: Commands, asset_server: Res<AssetServer>) {
// Spawn the button and its text as children of the container // Spawn the button and its text as children of the container
let button = commands.spawn(button_node).id(); let button = commands.spawn(button_node).id();
let button_text = commands.spawn(button_text_node).id(); let button_text = commands.spawn((button_text_node, AnswerButton)).id();
commands.entity(button).push_children(&[button_text]); commands.entity(button).push_children(&[button_text]);
commands.entity(container).push_children(&[button]); commands.entity(container).push_children(&[button]);