//! This example demonstrates how fallible parameters can prevent their systems //! from running if their acquiry conditions aren't met. //! //! Fallible parameters include: //! - [`Res`], [`ResMut`] - Resource has to exist. //! - [`Single`] - There must be exactly one matching entity. //! - [`Option>`] - There must be zero or one matching entity. //! - [`Populated`] - There must be at least one matching entity. use bevy::prelude::*; use rand::Rng; fn main() { println!(); println!("Press 'A' to add enemy ships and 'R' to remove them."); println!("Player ship will wait for enemy ships and track one if it exists,"); println!("but will stop tracking if there are more than one."); println!(); App::new() .add_plugins(DefaultPlugins) .add_systems(Startup, setup) // Systems that fail parameter validation will emit warnings. // The default policy is to emit a warning once per system. // This is good for catching unexpected behavior, but can // lead to spam. You can disable invalid param warnings // per system using the `.never_param_warn()` method. .add_systems( Update, ( user_input, move_targets.never_param_warn(), move_pointer.never_param_warn(), ) .chain(), ) // We will leave this systems with default warning policy. .add_systems(Update, do_nothing_fail_validation) .run(); } /// Enemy component stores data for movement in a circle. #[derive(Component, Default)] struct Enemy { origin: Vec2, radius: f32, rotation: f32, rotation_speed: f32, } /// Player component stores data for going after enemies. #[derive(Component, Default)] struct Player { speed: f32, rotation_speed: f32, min_follow_radius: f32, } fn setup(mut commands: Commands, asset_server: Res) { // Spawn 2D camera. commands.spawn(Camera2d); // Spawn player. let texture = asset_server.load("textures/simplespace/ship_C.png"); commands.spawn(( Player { speed: 100.0, rotation_speed: 2.0, min_follow_radius: 50.0, }, Sprite { image: texture, color: bevy::color::palettes::tailwind::BLUE_800.into(), ..Default::default() }, Transform::from_translation(Vec3::ZERO), )); } /// System that reads user input. /// If user presses 'A' we spawn a new random enemy. /// If user presses 'R' we remove a random enemy (if any exist). fn user_input( mut commands: Commands, enemies: Query>, keyboard_input: Res>, asset_server: Res, ) { let mut rng = rand::thread_rng(); if keyboard_input.just_pressed(KeyCode::KeyA) { let texture = asset_server.load("textures/simplespace/enemy_A.png"); commands.spawn(( Enemy { origin: Vec2::new(rng.gen_range(-200.0..200.0), rng.gen_range(-200.0..200.0)), radius: rng.gen_range(50.0..150.0), rotation: rng.gen_range(0.0..std::f32::consts::TAU), rotation_speed: rng.gen_range(0.5..1.5), }, Sprite { image: texture, color: bevy::color::palettes::tailwind::RED_800.into(), ..default() }, Transform::from_translation(Vec3::ZERO), )); } if keyboard_input.just_pressed(KeyCode::KeyR) { if let Some(entity) = enemies.iter().next() { commands.entity(entity).despawn(); } } } // System that moves the enemies in a circle. // Only runs if there are enemies. fn move_targets(mut enemies: Populated<(&mut Transform, &mut Enemy)>, time: Res