linear state restart action
This commit is contained in:
parent
6506955115
commit
393e199a00
5 changed files with 108 additions and 9 deletions
|
|
@ -2,3 +2,6 @@ use bevy::prelude::*;
|
|||
|
||||
#[derive(Component)]
|
||||
pub struct LinearStateMarker;
|
||||
|
||||
#[derive(Component, Copy, Clone)]
|
||||
pub struct LinearRestartMarker;
|
||||
|
|
@ -19,4 +19,7 @@ pub use system_ball::*;
|
|||
mod constants;
|
||||
pub use constants::*;
|
||||
|
||||
mod ui;
|
||||
pub use ui::*;
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
)
|
||||
.add_systems(OnExit(LinearState), cleanup);
|
||||
.in_set(LinearUpdateSet::Track),
|
||||
)
|
||||
.add_systems(OnExit(LinearPlayState), cleanup);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -40,3 +52,7 @@ fn cleanup(mut commands: Commands, query: Query<Entity, With<LinearStateMarker>>
|
|||
commands.remove_resource::<Track>();
|
||||
commands.remove_resource::<PrecalculatedTrack>();
|
||||
}
|
||||
|
||||
fn linear_restart(mut next_state: ResMut<NextState<AppState>>) {
|
||||
next_state.set(LinearPlayState);
|
||||
}
|
||||
|
|
|
|||
77
src/states/linear/ui.rs
Normal file
77
src/states/linear/ui.rs
Normal file
|
|
@ -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::<ButtonClickMessage<LinearRestartMarker>>()
|
||||
.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::<LinearRestartMarker>,
|
||||
)
|
||||
.in_set(LinearUpdateSet::Track),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// хоть ui отделен, но по глобальному маркеру стейта, все сущности будут очищены в основном плагине
|
||||
fn setup_ui(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||
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<Interaction>, With<LinearRestartMarker>)>,
|
||||
mut next_state: ResMut<NextState<AppState>>,
|
||||
) {
|
||||
for interaction in &interaction_query {
|
||||
if matches!(interaction, Interaction::Pressed) {
|
||||
next_state.set(AppState::LinearGameRestart);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue