do not repeat already guessed cons

This commit is contained in:
WanderingPenwing 2024-10-07 18:28:21 +02:00
parent 447dfc9f22
commit a00a66ab1e
3 changed files with 72 additions and 49 deletions

View file

@ -33,6 +33,8 @@ pub struct HintLabel;
pub fn setup( pub fn setup(
mut commands: Commands, mut commands: Commands,
mut game_data: ResMut<GameData>,
sky: Res<Sky>,
) { ) {
let container_node = NodeBundle { let container_node = NodeBundle {
style: Style { style: Style {
@ -162,6 +164,9 @@ pub fn setup(
let hint_label = commands.spawn((hint_label_node, MainGame, HintLabel)).id(); let hint_label = commands.spawn((hint_label_node, MainGame, HintLabel)).id();
commands.entity(centered_container).push_children(&[hint_label]); 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.health -= 1;
} }
game_data.content.retain(|x| x != &target_cons);
game_data.state = PlayerState::Answered; game_data.state = PlayerState::Answered;
for ( for (
_interaction, _interaction,
mut color, mut color,
mut border_color, mut border_color,
@ -369,37 +376,38 @@ fn choose_constellation(
if game_data.health == 0 { if game_data.health == 0 {
game_state.set(GameState::End); 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; if game_data.content.len() < 4 {
let target_constellation = &constellations[target_index]; 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())); let target_index = rng.next_u32().rem_euclid(4) as usize;
game_data.target_cons_name = Some(target_constellation.name.clone()); 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() { info!("Target constellation: {}", target_constellation.name);
text.sections[0].value = constellations[i].name.clone();
}
for (mut bg_color, mut border_color) in &mut button_query { for (i, mut text) in text_query.iter_mut().enumerate() {
*bg_color = NORMAL_BUTTON.into(); text.sections[0].value = selected_cons_names[i].clone();
*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 (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 { fn constellation_center(target_constellation: Constellation) -> Quat {

View file

@ -41,30 +41,28 @@ struct StarData {
name: Option<String>, name: Option<String>,
} }
#[derive(Resource, Default)] #[derive(Resource, Default, Clone)]
struct Sky { struct Sky {
content: Vec<Constellation>, content: Vec<Constellation>,
} }
#[derive(Resource)] impl Sky {
struct GameData { fn as_string(&self) -> Vec<String> {
seen: Vec<Constellation>, let mut cons_names : Vec<String> = vec![];
score: usize, for cons in self.content.clone() {
health: usize, cons_names.push(cons.name.clone());
state: PlayerState, }
target_cons_name: Option<String>, return cons_names;
} }
impl Default for GameData { fn get_constellation(&self, name: &str) -> Constellation {
fn default() -> Self { for cons in self.content.clone() {
GameData { if &cons.name == name {
seen: vec![], return cons;
score: 0, }
health: 3, }
state: PlayerState::Playing, return self.content[0].clone();
target_cons_name: None, }
}
}
} }
#[derive(Serialize, Deserialize, Debug, Clone)] #[derive(Serialize, Deserialize, Debug, Clone)]
@ -111,6 +109,27 @@ struct Player {
dragging_pos: Option<Vec2>, dragging_pos: Option<Vec2>,
} }
#[derive(Resource)]
struct GameData {
content: Vec<String>,
score: usize,
health: usize,
state: PlayerState,
target_cons_name: Option<String>,
}
impl Default for GameData {
fn default() -> Self {
GameData {
content: vec![],
score: 0,
health: 3,
state: PlayerState::Playing,
target_cons_name: None,
}
}
}
#[derive(Default, PartialEq)] #[derive(Default, PartialEq)]
enum PlayerState { enum PlayerState {
#[default] #[default]

View file

@ -3,7 +3,6 @@ use std::f64::consts::PI;
//use bevy::input::mouse::MouseMotion; //use bevy::input::mouse::MouseMotion;
use crate::GameState; use crate::GameState;
use crate::StartMenu; use crate::StartMenu;
use crate::GameData;
use crate::Player; use crate::Player;
#[derive(Component)] #[derive(Component)]
@ -19,7 +18,6 @@ pub fn audio_setup(asset_server: Res<AssetServer>, mut commands: Commands) {
pub fn setup( pub fn setup(
mut commands: Commands, mut commands: Commands,
mut game_data: ResMut<GameData>
) { ) {
let container_node = NodeBundle { let container_node = NodeBundle {
style: Style { style: Style {
@ -63,8 +61,6 @@ pub fn setup(
let bottom_text = commands.spawn((bottom_text_node, StartMenu)).id(); let bottom_text = commands.spawn((bottom_text_node, StartMenu)).id();
commands.entity(container).push_children(&[top_text, bottom_text]); commands.entity(container).push_children(&[top_text, bottom_text]);
*game_data = GameData::default();
} }
pub fn player_interact( pub fn player_interact(