//! This example demonstrates how to use run conditions to control when systems run.12use bevy::prelude::*;34fn main() {5println!();6println!("For the first 2 seconds you will not be able to increment the counter");7println!("Once that time has passed you can press space, enter, left mouse, right mouse or touch the screen to increment the counter");8println!();910App::new()11.add_plugins(DefaultPlugins)12.init_resource::<InputCounter>()13.add_systems(14Update,15(16increment_input_counter17// The common_conditions module has a few useful run conditions18// for checking resources and states. These are included in the prelude.19.run_if(resource_exists::<InputCounter>)20// `.or()` is a run condition combinator that only evaluates the second condition21// if the first condition returns `false`. This behavior is known as "short-circuiting",22// and is how the `||` operator works in Rust (as well as most C-family languages).23// In this case, the `has_user_input` run condition will be evaluated since the `Unused` resource has not been initialized.24.run_if(resource_exists::<Unused>.or(25// This is a custom run condition, defined using a system that returns26// a `bool` and which has read-only `SystemParam`s.27// Only a single run condition must return `true` in order for the system to run.28has_user_input,29)),30print_input_counter31// `.and()` is a run condition combinator that only evaluates the second condition32// if the first condition returns `true`, analogous to the `&&` operator.33// In this case, the short-circuiting behavior prevents the second run condition from34// panicking if the `InputCounter` resource has not been initialized.35.run_if(resource_exists::<InputCounter>.and(36// This is a custom run condition in the form of a closure.37// This is useful for small, simple run conditions you don't need to reuse.38// All the normal rules still apply: all parameters must be read only except for local parameters.39|counter: Res<InputCounter>| counter.is_changed() && !counter.is_added(),40)),41print_time_message42// This function returns a custom run condition, much like the common conditions module.43// It will only return true once 2 seconds have passed.44.run_if(time_passed(2.0))45// You can use the `not` condition from the common_conditions module46// to inverse a run condition. In this case it will return true if47// less than 2.5 seconds have elapsed since the app started.48.run_if(not(time_passed(2.5))),49),50)51.run();52}5354#[derive(Resource, Default)]55struct InputCounter(usize);5657#[derive(Resource)]58struct Unused;5960/// Return true if any of the defined inputs were just pressed.61///62/// This is a custom run condition, it can take any normal system parameters as long as63/// they are read only (except for local parameters which can be mutable).64/// It returns a bool which determines if the system should run.65fn has_user_input(66keyboard_input: Res<ButtonInput<KeyCode>>,67mouse_button_input: Res<ButtonInput<MouseButton>>,68touch_input: Res<Touches>,69) -> bool {70keyboard_input.just_pressed(KeyCode::Space)71|| keyboard_input.just_pressed(KeyCode::Enter)72|| mouse_button_input.just_pressed(MouseButton::Left)73|| mouse_button_input.just_pressed(MouseButton::Right)74|| touch_input.any_just_pressed()75}7677/// This is a function that returns a closure which can be used as a run condition.78///79/// This is useful because you can reuse the same run condition but with different variables.80/// This is how the common conditions module works.81fn time_passed(t: f32) -> impl FnMut(Local<f32>, Res<Time>) -> bool {82move |mut timer: Local<f32>, time: Res<Time>| {83// Tick the timer84*timer += time.delta_secs();85// Return true if the timer has passed the time86*timer >= t87}88}8990/// SYSTEM: Increment the input counter91/// Notice how we can take just the `ResMut` and not have to wrap92/// it in an option in case it hasn't been initialized, this is because93/// it has a run condition that checks if the `InputCounter` resource exists94fn increment_input_counter(mut counter: ResMut<InputCounter>) {95counter.0 += 1;96}9798/// SYSTEM: Print the input counter99fn print_input_counter(counter: Res<InputCounter>) {100println!("Input counter: {}", counter.0);101}102103/// SYSTEM: Adds the input counter resource104fn print_time_message() {105println!("It has been more than 2 seconds since the program started and less than 2.5 seconds");106}107108109