diff --git a/src/states/linear/components.rs b/src/states/linear/components.rs index 8fb44ec..6c0a133 100644 --- a/src/states/linear/components.rs +++ b/src/states/linear/components.rs @@ -1,4 +1,7 @@ use bevy::prelude::*; #[derive(Component)] -pub struct LinearStateMarker; \ No newline at end of file +pub struct LinearStateMarker; + +#[derive(Component, Copy, Clone)] +pub struct LinearRestartMarker; \ No newline at end of file diff --git a/src/states/linear/mod.rs b/src/states/linear/mod.rs index 5a29cd3..c2c08ac 100644 --- a/src/states/linear/mod.rs +++ b/src/states/linear/mod.rs @@ -19,4 +19,7 @@ pub use system_ball::*; mod constants; pub use constants::*; +mod ui; +pub use ui::*; + diff --git a/src/states/linear/plugin.rs b/src/states/linear/plugin.rs index 190b88d..b95b59e 100644 --- a/src/states/linear/plugin.rs +++ b/src/states/linear/plugin.rs @@ -1,8 +1,9 @@ -use crate::states::AppState::LinearState; +use crate::states::AppState; +use crate::states::AppState::{LinearGameRestart, LinearPlayState}; use crate::states::linear::*; use bevy::prelude::*; -pub struct LinearPlugin; +pub struct LinearPlayPlugin; // создаем енум, чтоб не указывать run_if(in_state) для каждой системы #[derive(SystemSet, Debug, Hash, PartialEq, Eq, Clone)] @@ -10,15 +11,26 @@ pub enum LinearUpdateSet { Track, } -impl Plugin for LinearPlugin { +impl Plugin for LinearPlayPlugin { fn build(&self, app: &mut App) { - app.add_systems(OnEnter(LinearState), setup) - .configure_sets(Update, LinearUpdateSet::Track.run_if(in_state(LinearState))) + app + .add_systems(OnEnter(LinearPlayState), setup) + .add_systems(OnEnter(LinearGameRestart), linear_restart) + .add_plugins(LinearUIPlugin) + .configure_sets( + Update, + LinearUpdateSet::Track.run_if(in_state(LinearPlayState)), + ) .add_systems( Update, - (draw_track_gizmos, spawn_round_ball).in_set(LinearUpdateSet::Track), + ( + draw_track_gizmos, + spawn_round_ball, + move_round_balls, + ) + .in_set(LinearUpdateSet::Track), ) - .add_systems(OnExit(LinearState), cleanup); + .add_systems(OnExit(LinearPlayState), cleanup); } } @@ -40,3 +52,7 @@ fn cleanup(mut commands: Commands, query: Query> commands.remove_resource::(); commands.remove_resource::(); } + +fn linear_restart(mut next_state: ResMut>) { + next_state.set(LinearPlayState); +} diff --git a/src/states/linear/ui.rs b/src/states/linear/ui.rs new file mode 100644 index 0000000..18e4c93 --- /dev/null +++ b/src/states/linear/ui.rs @@ -0,0 +1,77 @@ +use crate::states::AppState; +use crate::states::AppState::LinearPlayState; +use crate::states::linear::plugin::LinearUpdateSet; +use crate::states::linear::{LinearRestartMarker, LinearStateMarker}; +use crate::ui::button_click::ButtonClickMessage; +use crate::ui::click::handle_click_system; +use crate::ui::{ButtonStyle, spawn_button}; +use bevy::prelude::*; + +pub struct LinearUIPlugin; + +impl Plugin for LinearUIPlugin { + fn build(&self, app: &mut App) { + app.add_message::>() + .add_systems(OnEnter(LinearPlayState), setup_ui) + .configure_sets( + Update, + LinearUpdateSet::Track.run_if(in_state(LinearPlayState)), + ) + .add_systems( + Update, + ( + restart_game_button_system, + handle_click_system::, + ) + .in_set(LinearUpdateSet::Track), + ); + } +} + +// хоть ui отделен, но по глобальному маркеру стейта, все сущности будут очищены в основном плагине +fn setup_ui(mut commands: Commands, asset_server: Res) { + let root = commands + .spawn(( + Node { + display: Display::Flex, + width: Val::Percent(100.0), + height: Val::Percent(100.0), + ..default() + }, + BackgroundColor(Color::NONE), + Name::new("Game state background node"), + LinearStateMarker, + )) + .id(); + + let restart_button_style = ButtonStyle { + font: asset_server.load("fonts/QR Ames Beta.otf"), + font_size: 24.0, + text_color: Color::WHITE, + normal_bg: Color::linear_rgba(0.15, 0.15, 0.15, 1.0), + hovered_bg: Color::linear_rgb(0.25, 0.25, 0.25), + pressed_bg: Color::linear_rgb(0.3, 0.3, 0.3), + width: Val::Px(200.0), + height: Val::Px(50.0), + margin: UiRect::all(Val::Px(10.0)), + }; + + spawn_button( + &mut commands, + root, + "Restart", + &restart_button_style, + LinearRestartMarker, + ); +} + +pub fn restart_game_button_system( + interaction_query: Query<&Interaction, (Changed, With)>, + mut next_state: ResMut>, +) { + for interaction in &interaction_query { + if matches!(interaction, Interaction::Pressed) { + next_state.set(AppState::LinearGameRestart); + } + } +} diff --git a/src/states/main_menu/systems.rs b/src/states/main_menu/systems.rs index bdc9082..b6fbd92 100644 --- a/src/states/main_menu/systems.rs +++ b/src/states/main_menu/systems.rs @@ -31,7 +31,7 @@ pub fn linear_button_system( for interaction in &interaction_query { if matches!(interaction, Interaction::Pressed) { println!("Линейное нажата"); - next_state.set(AppState::LinearState); + next_state.set(AppState::LinearPlayState); } } } \ No newline at end of file