From 9b8c2b37efce8da99662d2289f4b8f8076ea6afc Mon Sep 17 00:00:00 2001 From: AmrDhaliwal Date: Sun, 16 May 2021 21:01:08 -0700 Subject: [PATCH 1/8] Added a generic systems example to the examples folder in bevy. --- examples/generic/generic_systems.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 examples/generic/generic_systems.rs diff --git a/examples/generic/generic_systems.rs b/examples/generic/generic_systems.rs new file mode 100644 index 0000000000000..9a8d0acd60b56 --- /dev/null +++ b/examples/generic/generic_systems.rs @@ -0,0 +1,11 @@ +use bevy::ecs::component::Component; + +// remove all entites that aren't the player +fn remove_entities(q: Query>) { + for entity in q.iter() { + let id = Entity; + if entity != id.0 { + command.entity(entity).despawn(); + } + } +} \ No newline at end of file From 0921bb4fad6f05793aff246a0720718a9b97dd5b Mon Sep 17 00:00:00 2001 From: AmrDhaliwal Date: Tue, 18 May 2021 00:41:30 -0700 Subject: [PATCH 2/8] Added generic systems as an example to Cargo.toml and examples/README.md. --- Cargo.toml | 4 ++++ examples/ecs/generic_systems.rs | 21 +++++++++++++++++++++ examples/generic/generic_systems.rs | 11 ----------- 3 files changed, 25 insertions(+), 11 deletions(-) create mode 100644 examples/ecs/generic_systems.rs delete mode 100644 examples/generic/generic_systems.rs diff --git a/Cargo.toml b/Cargo.toml index 95141021a8077..b7b4bc614fc7d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -303,6 +303,10 @@ path = "examples/ecs/timers.rs" name = "query_bundle" path = "examples/ecs/query_bundle.rs" +[[example]] +name = "generic_systems" +path = "examples/ecs/generic_systems.rs" + # Games [[example]] name = "alien_cake_addict" diff --git a/examples/ecs/generic_systems.rs b/examples/ecs/generic_systems.rs new file mode 100644 index 0000000000000..44ef53d7e0703 --- /dev/null +++ b/examples/ecs/generic_systems.rs @@ -0,0 +1,21 @@ +use bevy::prelude::*; +use bevy::ecs::component::Component; + +// When entities spawn in call an event + +struct A; +struct B; + +fn spawn_entities(mut commands: Commands, q: Query>) { + q.for_each(|entity| { commands.entity(entity).spawn();}); + info!("Entity {:?} spawned in!") +} + + + +fn main() { + App::build() + .add_system(spawn_entities::.system()) + .add_system(spawn_entities::.system()) + .run(); +} \ No newline at end of file diff --git a/examples/generic/generic_systems.rs b/examples/generic/generic_systems.rs deleted file mode 100644 index 9a8d0acd60b56..0000000000000 --- a/examples/generic/generic_systems.rs +++ /dev/null @@ -1,11 +0,0 @@ -use bevy::ecs::component::Component; - -// remove all entites that aren't the player -fn remove_entities(q: Query>) { - for entity in q.iter() { - let id = Entity; - if entity != id.0 { - command.entity(entity).despawn(); - } - } -} \ No newline at end of file From 76ce2beaf4ecc8eeabb0ff4bc5f3117fa8f10028 Mon Sep 17 00:00:00 2001 From: AmrDhaliwal Date: Wed, 19 May 2021 13:30:13 -0700 Subject: [PATCH 3/8] Added mouse inputs to the generic_systems example. --- examples/ecs/generic_systems.rs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/examples/ecs/generic_systems.rs b/examples/ecs/generic_systems.rs index 44ef53d7e0703..9467074816d2e 100644 --- a/examples/ecs/generic_systems.rs +++ b/examples/ecs/generic_systems.rs @@ -1,20 +1,26 @@ use bevy::prelude::*; use bevy::ecs::component::Component; -// When entities spawn in call an event - struct A; struct B; -fn spawn_entities(mut commands: Commands, q: Query>) { - q.for_each(|entity| { commands.entity(entity).spawn();}); - info!("Entity {:?} spawned in!") +pub fn spawn_entities(mut commands: Commands, + mouse_button_inputs: Res>,) { + for _ in 0..10 { + let id = commands.spawn().insert(T).id(); + if !mouse_button_inputs.just_pressed(MouseButton::Left) { + return info!("Spawned entity {:?} with component {}", id, std::any::type_name::()); + } + if !mouse_button_inputs.just_pressed(MouseButton::Right) { + return info!("Spawned entity {:?} with component {}", id, std::any::type_name::()); + } + } } - fn main() { App::build() + .add_plugins(DefaultPlugins) .add_system(spawn_entities::.system()) .add_system(spawn_entities::.system()) .run(); From 0c78aac03215852c9c9c49bdd758d1129c2764ad Mon Sep 17 00:00:00 2001 From: AmrDhaliwal Date: Wed, 19 May 2021 14:09:10 -0700 Subject: [PATCH 4/8] Changed inputs from being negated to accepted. --- examples/ecs/generic_systems.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/ecs/generic_systems.rs b/examples/ecs/generic_systems.rs index 9467074816d2e..d778765932db4 100644 --- a/examples/ecs/generic_systems.rs +++ b/examples/ecs/generic_systems.rs @@ -8,10 +8,10 @@ pub fn spawn_entities(mut commands: Commands, mouse_button_inputs: Res>,) { for _ in 0..10 { let id = commands.spawn().insert(T).id(); - if !mouse_button_inputs.just_pressed(MouseButton::Left) { + if mouse_button_inputs.just_pressed(MouseButton::Left) { return info!("Spawned entity {:?} with component {}", id, std::any::type_name::()); } - if !mouse_button_inputs.just_pressed(MouseButton::Right) { + if mouse_button_inputs.just_pressed(MouseButton::Right) { return info!("Spawned entity {:?} with component {}", id, std::any::type_name::()); } } From 4738f761bd265f4494c35e55830009497f5a8ba9 Mon Sep 17 00:00:00 2001 From: AmrDhaliwal Date: Thu, 20 May 2021 13:30:06 -0700 Subject: [PATCH 5/8] Restructured for loop to have results of just pressed outside the loop. --- examples/ecs/generic_systems.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/examples/ecs/generic_systems.rs b/examples/ecs/generic_systems.rs index d778765932db4..988817cb98571 100644 --- a/examples/ecs/generic_systems.rs +++ b/examples/ecs/generic_systems.rs @@ -8,13 +8,10 @@ pub fn spawn_entities(mut commands: Commands, mouse_button_inputs: Res>,) { for _ in 0..10 { let id = commands.spawn().insert(T).id(); - if mouse_button_inputs.just_pressed(MouseButton::Left) { - return info!("Spawned entity {:?} with component {}", id, std::any::type_name::()); - } - if mouse_button_inputs.just_pressed(MouseButton::Right) { - return info!("Spawned entity {:?} with component {}", id, std::any::type_name::()); - } + info!("Spawned entity {:?} with component {}", id, std::any::type_name::()); } + mouse_button_inputs.just_pressed(MouseButton::Left); + mouse_button_inputs.just_pressed(MouseButton::Right); } From 993cb239ddc46f3cc340ee653660cae3a671ae2e Mon Sep 17 00:00:00 2001 From: AmrDhaliwal Date: Thu, 27 May 2021 11:40:45 -0700 Subject: [PATCH 6/8] Updated the generic system example to be able to us const generics. --- examples/ecs/generic_systems.rs | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/examples/ecs/generic_systems.rs b/examples/ecs/generic_systems.rs index 988817cb98571..7e7e6771c0d39 100644 --- a/examples/ecs/generic_systems.rs +++ b/examples/ecs/generic_systems.rs @@ -4,21 +4,23 @@ use bevy::ecs::component::Component; struct A; struct B; -pub fn spawn_entities(mut commands: Commands, - mouse_button_inputs: Res>,) { - for _ in 0..10 { - let id = commands.spawn().insert(T).id(); - info!("Spawned entity {:?} with component {}", id, std::any::type_name::()); +fn spawn_entities_on_click( + mut cmds: Commands, + mouse_button_inputs: Res>, +) { + if mouse_button_inputs.just_pressed(MouseButton::Left) { + for _ in 0..N { + cmds.spawn().insert(T).id(); + } + info!("spawned {} entities with component {}", N, std::any::type_name::()); } - mouse_button_inputs.just_pressed(MouseButton::Left); - mouse_button_inputs.just_pressed(MouseButton::Right); } fn main() { App::build() .add_plugins(DefaultPlugins) - .add_system(spawn_entities::.system()) - .add_system(spawn_entities::.system()) + .add_system(spawn_entities_on_click::.system()) + .add_system(spawn_entities_on_click::.system()) .run(); } \ No newline at end of file From 911b603b1d173eb92a69a5d32f5c032e98e85e53 Mon Sep 17 00:00:00 2001 From: AmrDhaliwal Date: Fri, 28 May 2021 11:10:26 -0700 Subject: [PATCH 7/8] Moved the fn main() to the top of the file and constrained T on default. --- examples/ecs/generic_systems.rs | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/examples/ecs/generic_systems.rs b/examples/ecs/generic_systems.rs index 7e7e6771c0d39..e2cc7f2164580 100644 --- a/examples/ecs/generic_systems.rs +++ b/examples/ecs/generic_systems.rs @@ -1,5 +1,12 @@ -use bevy::prelude::*; -use bevy::ecs::component::Component; +use bevy::{prelude::*, ecs::component::Component}; + +fn main() { + App::build() + .add_plugins(DefaultPlugins) + .add_system(spawn_entities_on_click::.system()) + .add_system(spawn_entities_on_click::.system()) + .run(); +} struct A; struct B; @@ -7,20 +14,15 @@ struct B; fn spawn_entities_on_click( mut cmds: Commands, mouse_button_inputs: Res>, -) { +) +where + T: Component + Default + { if mouse_button_inputs.just_pressed(MouseButton::Left) { for _ in 0..N { - cmds.spawn().insert(T).id(); + cmds.spawn().insert(T::default()).id(); } info!("spawned {} entities with component {}", N, std::any::type_name::()); } } - -fn main() { - App::build() - .add_plugins(DefaultPlugins) - .add_system(spawn_entities_on_click::.system()) - .add_system(spawn_entities_on_click::.system()) - .run(); -} \ No newline at end of file From 967440f349f1358350d62c0244ff4bd3929af45b Mon Sep 17 00:00:00 2001 From: AmrDhaliwal Date: Wed, 2 Jun 2021 13:34:52 -0700 Subject: [PATCH 8/8] Added the second type parameter to the fn main() function. --- examples/ecs/generic_systems.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/ecs/generic_systems.rs b/examples/ecs/generic_systems.rs index e2cc7f2164580..170797eb3debd 100644 --- a/examples/ecs/generic_systems.rs +++ b/examples/ecs/generic_systems.rs @@ -3,8 +3,8 @@ use bevy::{prelude::*, ecs::component::Component}; fn main() { App::build() .add_plugins(DefaultPlugins) - .add_system(spawn_entities_on_click::.system()) - .add_system(spawn_entities_on_click::.system()) + .add_system(spawn_entities_on_click::::.system()) + .add_system(spawn_entities_on_click::::.system()) .run(); }