From 440ec86cc78b572edb2f068d1be59c3beeef6388 Mon Sep 17 00:00:00 2001 From: WanderingPenwing Date: Sun, 6 Oct 2024 15:36:10 +0200 Subject: [PATCH] broken --- src/end_state.rs | 70 +++++++++++++++++++++++++++++++++++++++++++++ src/game_state.rs | 0 src/main.rs | 71 +++------------------------------------------- src/start_state.rs | 0 4 files changed, 74 insertions(+), 67 deletions(-) create mode 100644 src/end_state.rs create mode 100644 src/game_state.rs create mode 100644 src/start_state.rs diff --git a/src/end_state.rs b/src/end_state.rs new file mode 100644 index 0000000..09b6771 --- /dev/null +++ b/src/end_state.rs @@ -0,0 +1,70 @@ +use bevy::prelude::*; +use core::Player; +use core::GameState; + +fn setup( + mut commands: Commands, + _asset_server: Res, + mut player_query: Query<&mut Player> +) { + if let Ok(player) = player_query.get_single_mut() { + // Create a container node that places its children (text areas) in a vertical column and centers them + let container_node = NodeBundle { + style: Style { + width: Val::Percent(100.0), // Full width of the screen + height: Val::Percent(100.0), // Full height of the screen + flex_direction: FlexDirection::Column, // Arrange children in a column (vertical) + justify_content: JustifyContent::Center, // Center vertically + align_items: AlignItems::Center, // Center horizontally + ..default() + }, + ..default() + }; + + let container = commands.spawn(container_node).id(); + + // TextStyle for the top text (larger font) + let top_text_style = TextStyle { + font_size: 50.0, // Larger font size + color: Color::WHITE, + // font: asset_server.load("fonts/FiraSans-Bold.ttf"), // Load font if needed + ..default() + }; + + // TextStyle for the bottom text (smaller font) + let bottom_text_style = TextStyle { + font_size: 30.0, // Smaller font size + color: Color::WHITE, + // font: asset_server.load("fonts/FiraSans-Regular.ttf"), // Load font if needed + ..default() + }; + + // TextBundle for the top text + let top_text_node = TextBundle::from_section( + "Game Over", // Text for the top section + top_text_style, + ); + + // TextBundle for the bottom text + let bottom_text_node = TextBundle::from_section( + format!("final score : {}", player.score), // Text for the bottom section + bottom_text_style, + ); + + // Spawn the text nodes and add them as children to the container + let top_text = commands.spawn((top_text_node, GameOver)).id(); + let bottom_text = commands.spawn((bottom_text_node, GameOver)).id(); + + commands.entity(container).push_children(&[top_text, bottom_text]); + } +} + +fn buttons( + keys: Res>, + mut game_state: ResMut> +) { + if keys.just_pressed(KeyCode::Space) { + info!("start space"); + game_state.set(GameState::Start); + } +} diff --git a/src/game_state.rs b/src/game_state.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/main.rs b/src/main.rs index 3daa7c8..1f31c66 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,6 +9,8 @@ use std::f64::consts::PI; use rand::seq::SliceRandom; use rand::RngCore; +mod end_state; + const NORMAL_BUTTON: Color = Color::srgb(0.15, 0.15, 0.15); const WRONG_BUTTON: Color = Color::srgb(0.50, 0.15, 0.15); const RIGHT_BUTTON: Color = Color::srgb(0.15, 0.50, 0.15); @@ -124,78 +126,13 @@ fn main() { .add_systems(Update, game_buttons.run_if(in_state(GameState::Game))) .add_systems(Update, label_update.run_if(in_state(GameState::Game))) .add_systems(OnExit(GameState::Game), despawn_screen::) - .add_systems(OnEnter(GameState::End), end_setup) - .add_systems(Update, end_buttons.run_if(in_state(GameState::End))) + .add_systems(OnEnter(GameState::End), end_state::setup) + .add_systems(Update, end_state::buttons.run_if(in_state(GameState::End))) .add_systems(OnExit(GameState::End), despawn_screen::) .run(); } -fn end_setup( - mut commands: Commands, - _asset_server: Res, - mut player_query: Query<&mut Player> -) { - if let Ok(player) = player_query.get_single_mut() { - // Create a container node that places its children (text areas) in a vertical column and centers them - let container_node = NodeBundle { - style: Style { - width: Val::Percent(100.0), // Full width of the screen - height: Val::Percent(100.0), // Full height of the screen - flex_direction: FlexDirection::Column, // Arrange children in a column (vertical) - justify_content: JustifyContent::Center, // Center vertically - align_items: AlignItems::Center, // Center horizontally - ..default() - }, - ..default() - }; - let container = commands.spawn(container_node).id(); - - // TextStyle for the top text (larger font) - let top_text_style = TextStyle { - font_size: 50.0, // Larger font size - color: Color::WHITE, - // font: asset_server.load("fonts/FiraSans-Bold.ttf"), // Load font if needed - ..default() - }; - - // TextStyle for the bottom text (smaller font) - let bottom_text_style = TextStyle { - font_size: 30.0, // Smaller font size - color: Color::WHITE, - // font: asset_server.load("fonts/FiraSans-Regular.ttf"), // Load font if needed - ..default() - }; - - // TextBundle for the top text - let top_text_node = TextBundle::from_section( - "Game Over", // Text for the top section - top_text_style, - ); - - // TextBundle for the bottom text - let bottom_text_node = TextBundle::from_section( - format!("final score : {}", player.score), // Text for the bottom section - bottom_text_style, - ); - - // Spawn the text nodes and add them as children to the container - let top_text = commands.spawn((top_text_node, GameOver)).id(); - let bottom_text = commands.spawn((bottom_text_node, GameOver)).id(); - - commands.entity(container).push_children(&[top_text, bottom_text]); - } -} - -fn end_buttons( - keys: Res>, - mut game_state: ResMut> -) { - if keys.just_pressed(KeyCode::Space) { - info!("start space"); - game_state.set(GameState::Start); - } -} fn start_ui_setup(mut commands: Commands, _asset_server: Res) { // Create a container node that places its children (text areas) in a vertical column and centers them diff --git a/src/start_state.rs b/src/start_state.rs new file mode 100644 index 0000000..e69de29