diff --git a/src/game_state.rs b/src/game_state.rs index 647fb7d..419caaf 100644 --- a/src/game_state.rs +++ b/src/game_state.rs @@ -33,6 +33,8 @@ pub struct HintLabel; pub fn setup( mut commands: Commands, + mut game_data: ResMut, + sky: Res, ) { let container_node = NodeBundle { style: Style { @@ -162,6 +164,9 @@ pub fn setup( let hint_label = commands.spawn((hint_label_node, MainGame, HintLabel)).id(); commands.entity(centered_container).push_children(&[hint_label]); + + *game_data = GameData::default(); + game_data.content = sky.as_string(); } @@ -330,9 +335,11 @@ pub fn ui_buttons( game_data.health -= 1; } + game_data.content.retain(|x| x != &target_cons); + game_data.state = PlayerState::Answered; - for ( + for ( _interaction, mut color, mut border_color, @@ -369,37 +376,38 @@ fn choose_constellation( if game_data.health == 0 { game_state.set(GameState::End); } - if sky.content.len() >= 4 { - let mut rng = rand::thread_rng(); - let mut selected_constellations = sky.content.clone(); - selected_constellations.shuffle(&mut rng); - let constellations = &selected_constellations[0..4]; - let target_index = rng.next_u32().rem_euclid(4) as usize; - let target_constellation = &constellations[target_index]; + if game_data.content.len() < 4 { + game_state.set(GameState::End); + } + + let mut rng = rand::thread_rng(); + let mut cons_names = game_data.content.clone(); + cons_names.shuffle(&mut rng); + let selected_cons_names = &cons_names[0..4]; - player.target_rotation = Some(constellation_center(target_constellation.clone())); - game_data.target_cons_name = Some(target_constellation.name.clone()); + let target_index = rng.next_u32().rem_euclid(4) as usize; + let target_constellation = sky.get_constellation(&selected_cons_names[target_index]); - info!("Target constellation: {}", target_constellation.name); + player.target_rotation = Some(constellation_center(target_constellation.clone())); + game_data.target_cons_name = Some(target_constellation.name.clone()); - for (i, mut text) in text_query.iter_mut().enumerate() { - text.sections[0].value = constellations[i].name.clone(); - } + info!("Target constellation: {}", target_constellation.name); - for (mut bg_color, mut border_color) in &mut button_query { - *bg_color = NORMAL_BUTTON.into(); - *border_color = Color::BLACK.into(); - } - - for (entity, _line) in constellation_line_query.iter() { - commands.entity(entity).despawn(); - } - - game_data.state = PlayerState::Playing; - } else { - info!("Not enough constellations in the sky (need 4)"); + for (i, mut text) in text_query.iter_mut().enumerate() { + text.sections[0].value = selected_cons_names[i].clone(); } + + for (mut bg_color, mut border_color) in &mut button_query { + *bg_color = NORMAL_BUTTON.into(); + *border_color = Color::BLACK.into(); + } + + for (entity, _line) in constellation_line_query.iter() { + commands.entity(entity).despawn(); + } + + game_data.state = PlayerState::Playing; } fn constellation_center(target_constellation: Constellation) -> Quat { diff --git a/src/main.rs b/src/main.rs index fcfe774..b9a05e7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -41,30 +41,28 @@ struct StarData { name: Option, } -#[derive(Resource, Default)] +#[derive(Resource, Default, Clone)] struct Sky { content: Vec, } -#[derive(Resource)] -struct GameData { - seen: Vec, - score: usize, - health: usize, - state: PlayerState, - target_cons_name: Option, -} +impl Sky { + fn as_string(&self) -> Vec { + let mut cons_names : Vec = vec![]; + for cons in self.content.clone() { + cons_names.push(cons.name.clone()); + } + return cons_names; + } -impl Default for GameData { - fn default() -> Self { - GameData { - seen: vec![], - score: 0, - health: 3, - state: PlayerState::Playing, - target_cons_name: None, - } - } + fn get_constellation(&self, name: &str) -> Constellation { + for cons in self.content.clone() { + if &cons.name == name { + return cons; + } + } + return self.content[0].clone(); + } } #[derive(Serialize, Deserialize, Debug, Clone)] @@ -111,6 +109,27 @@ struct Player { dragging_pos: Option, } +#[derive(Resource)] +struct GameData { + content: Vec, + score: usize, + health: usize, + state: PlayerState, + target_cons_name: Option, +} + +impl Default for GameData { + fn default() -> Self { + GameData { + content: vec![], + score: 0, + health: 3, + state: PlayerState::Playing, + target_cons_name: None, + } + } +} + #[derive(Default, PartialEq)] enum PlayerState { #[default] diff --git a/src/start_state.rs b/src/start_state.rs index 56c7643..9ae9ba3 100644 --- a/src/start_state.rs +++ b/src/start_state.rs @@ -3,7 +3,6 @@ use std::f64::consts::PI; //use bevy::input::mouse::MouseMotion; use crate::GameState; use crate::StartMenu; -use crate::GameData; use crate::Player; #[derive(Component)] @@ -19,7 +18,6 @@ pub fn audio_setup(asset_server: Res, mut commands: Commands) { pub fn setup( mut commands: Commands, - mut game_data: ResMut ) { let container_node = NodeBundle { style: Style { @@ -63,8 +61,6 @@ pub fn setup( let bottom_text = commands.spawn((bottom_text_node, StartMenu)).id(); commands.entity(container).push_children(&[top_text, bottom_text]); - - *game_data = GameData::default(); } pub fn player_interact(