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,6 +335,8 @@ 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 (
@ -369,14 +376,18 @@ 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 {
if game_data.content.len() < 4 {
game_state.set(GameState::End);
}
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
let mut selected_constellations = sky.content.clone(); let mut cons_names = game_data.content.clone();
selected_constellations.shuffle(&mut rng); cons_names.shuffle(&mut rng);
let constellations = &selected_constellations[0..4]; let selected_cons_names = &cons_names[0..4];
let target_index = rng.next_u32().rem_euclid(4) as usize; let target_index = rng.next_u32().rem_euclid(4) as usize;
let target_constellation = &constellations[target_index]; let target_constellation = sky.get_constellation(&selected_cons_names[target_index]);
player.target_rotation = Some(constellation_center(target_constellation.clone())); player.target_rotation = Some(constellation_center(target_constellation.clone()));
game_data.target_cons_name = Some(target_constellation.name.clone()); game_data.target_cons_name = Some(target_constellation.name.clone());
@ -384,7 +395,7 @@ fn choose_constellation(
info!("Target constellation: {}", target_constellation.name); info!("Target constellation: {}", target_constellation.name);
for (i, mut text) in text_query.iter_mut().enumerate() { for (i, mut text) in text_query.iter_mut().enumerate() {
text.sections[0].value = constellations[i].name.clone(); text.sections[0].value = selected_cons_names[i].clone();
} }
for (mut bg_color, mut border_color) in &mut button_query { for (mut bg_color, mut border_color) in &mut button_query {
@ -397,9 +408,6 @@ fn choose_constellation(
} }
game_data.state = PlayerState::Playing; game_data.state = PlayerState::Playing;
} else {
info!("Not enough constellations in the sky (need 4)");
}
} }
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,
target_cons_name: None,
} }
} }
return self.content[0].clone();
}
} }
#[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(