From 463b7f3afd567ee8f0cddce595ba7609955d9012 Mon Sep 17 00:00:00 2001 From: Brandon Janson Date: Wed, 11 Aug 2021 21:20:19 +1000 Subject: [PATCH 1/9] Add generic systems example --- Cargo.toml | 4 ++ examples/ecs/generic_system.rs | 78 ++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 examples/ecs/generic_system.rs diff --git a/Cargo.toml b/Cargo.toml index 0fafd91be7970..052170c8cf083 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -274,6 +274,10 @@ path = "examples/ecs/event.rs" name = "fixed_timestep" path = "examples/ecs/fixed_timestep.rs" +[[example]] +name = "generic_system" +path = "examples/ecs/generic_system.rs" + [[example]] name = "hierarchy" path = "examples/ecs/hierarchy.rs" diff --git a/examples/ecs/generic_system.rs b/examples/ecs/generic_system.rs new file mode 100644 index 0000000000000..ab62b763657e6 --- /dev/null +++ b/examples/ecs/generic_system.rs @@ -0,0 +1,78 @@ +use bevy::{ecs::component::Component, prelude::*}; + +#[derive(Debug, Clone, Eq, PartialEq, Hash)] +enum AppState { + MainMenu, + InGame, +} + +struct TextToPrint(String); + +struct MenuClose; +struct LevelUnload; + +fn main() { + App::new() + .add_plugins(DefaultPlugins) + .add_state(AppState::MainMenu) + .add_startup_system(setup) + .add_system(print_text) + // .add_system(transition_to_in_game) + .add_system_set( + SystemSet::on_update(AppState::MainMenu) + .with_system(transition_to_in_game) + ) + // add the cleanup systems + .add_system_set( + SystemSet::on_exit(AppState::MainMenu) + .with_system(cleanup_system::) + ) + .add_system_set( + SystemSet::on_exit(AppState::InGame) + .with_system(cleanup_system::) + ) + .run(); +} + +fn setup( + mut commands: Commands +) { + commands.spawn() + .insert(Timer::from_seconds(1.0, true)) + .insert(TextToPrint("I will print until you press space.".to_string())) + .insert(MenuClose); + + commands.spawn() + .insert(Timer::from_seconds(1.0, true)) + .insert(TextToPrint("I will always print".to_string())) + .insert(LevelUnload); +} + +fn print_text( + time: Res