mirror of
https://github.com/bevyengine/bevy
synced 2024-11-10 07:04:33 +00:00
b8263b55fb
# Objective Support the following syntax for adding systems: ```rust App::new() .add_system(setup.on_startup()) .add_systems(( show_menu.in_schedule(OnEnter(GameState::Paused)), menu_ssytem.in_set(OnUpdate(GameState::Paused)), hide_menu.in_schedule(OnExit(GameState::Paused)), )) ``` ## Solution Add the traits `IntoSystemAppConfig{s}`, which provide the extension methods necessary for configuring which schedule a system belongs to. These extension methods return `IntoSystemAppConfig{s}`, which `App::add_system{s}` uses to choose which schedule to add systems to. --- ## Changelog + Added the extension methods `in_schedule(label)` and `on_startup()` for configuring the schedule a system belongs to. ## Future Work * Replace all uses of `add_startup_system` in the engine. * Deprecate this method
94 lines
3.1 KiB
Rust
94 lines
3.1 KiB
Rust
//! Generic types allow us to reuse logic across many related systems,
|
|
//! allowing us to specialize our function's behavior based on which type (or types) are passed in.
|
|
//!
|
|
//! This is commonly useful for working on related components or resources,
|
|
//! where we want to have unique types for querying purposes but want them all to work the same way.
|
|
//! This is particularly powerful when combined with user-defined traits to add more functionality to these related types.
|
|
//! Remember to insert a specialized copy of the system into the schedule for each type that you want to operate on!
|
|
//!
|
|
//! For more advice on working with generic types in Rust, check out <https://doc.rust-lang.org/book/ch10-01-syntax.html>
|
|
//! or <https://doc.rust-lang.org/rust-by-example/generics.html>
|
|
|
|
use bevy::prelude::*;
|
|
|
|
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Hash)]
|
|
enum AppState {
|
|
#[default]
|
|
MainMenu,
|
|
InGame,
|
|
}
|
|
|
|
impl States for AppState {
|
|
type Iter = std::array::IntoIter<AppState, 2>;
|
|
|
|
fn variants() -> Self::Iter {
|
|
[AppState::MainMenu, AppState::InGame].into_iter()
|
|
}
|
|
}
|
|
|
|
#[derive(Component)]
|
|
struct TextToPrint(String);
|
|
|
|
#[derive(Component, Deref, DerefMut)]
|
|
struct PrinterTick(Timer);
|
|
|
|
#[derive(Component)]
|
|
struct MenuClose;
|
|
|
|
#[derive(Component)]
|
|
struct LevelUnload;
|
|
|
|
fn main() {
|
|
App::new()
|
|
.add_plugins(DefaultPlugins)
|
|
.add_state::<AppState>()
|
|
.add_system(setup_system.on_startup())
|
|
.add_system(print_text_system)
|
|
.add_system(transition_to_in_game_system.in_set(OnUpdate(AppState::MainMenu)))
|
|
// add the cleanup systems
|
|
.add_systems((
|
|
// Pass in the types your system should operate on using the ::<T> (turbofish) syntax
|
|
cleanup_system::<MenuClose>.in_schedule(OnExit(AppState::MainMenu)),
|
|
cleanup_system::<LevelUnload>.in_schedule(OnExit(AppState::InGame)),
|
|
))
|
|
.run();
|
|
}
|
|
|
|
fn setup_system(mut commands: Commands) {
|
|
commands.spawn((
|
|
PrinterTick(Timer::from_seconds(1.0, TimerMode::Repeating)),
|
|
TextToPrint("I will print until you press space.".to_string()),
|
|
MenuClose,
|
|
));
|
|
|
|
commands.spawn((
|
|
PrinterTick(Timer::from_seconds(1.0, TimerMode::Repeating)),
|
|
TextToPrint("I will always print".to_string()),
|
|
LevelUnload,
|
|
));
|
|
}
|
|
|
|
fn print_text_system(time: Res<Time>, mut query: Query<(&mut PrinterTick, &TextToPrint)>) {
|
|
for (mut timer, text) in &mut query {
|
|
if timer.tick(time.delta()).just_finished() {
|
|
info!("{}", text.0);
|
|
}
|
|
}
|
|
}
|
|
|
|
fn transition_to_in_game_system(
|
|
mut next_state: ResMut<NextState<AppState>>,
|
|
keyboard_input: Res<Input<KeyCode>>,
|
|
) {
|
|
if keyboard_input.pressed(KeyCode::Space) {
|
|
next_state.set(AppState::InGame);
|
|
}
|
|
}
|
|
|
|
// Type arguments on functions come after the function name, but before ordinary arguments.
|
|
// Here, the `Component` trait is a trait bound on T, our generic type
|
|
fn cleanup_system<T: Component>(mut commands: Commands, query: Query<Entity, With<T>>) {
|
|
for e in &query {
|
|
commands.entity(e).despawn_recursive();
|
|
}
|
|
}
|