From d9c428e32c870abe62968d4cfe3a26fc55a5503a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Orsv=C3=A4rn?= Date: Mon, 30 Nov 2020 22:25:49 +0100 Subject: [PATCH] Add removal_detection example (#945) Co-authored-by: Carter Anderson --- CHANGELOG.md | 7 +++ Cargo.toml | 4 ++ examples/ecs/removal_detection.rs | 74 +++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 examples/ecs/removal_detection.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 640464564c..a051f8e68a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ current changes on git with [previous release tags][git_tag_comparison]. ## Unreleased ### Added + - [`bevy_log`][836] - Adds logging functionality as a Plugin. - Changes internal logging to work with the new implementation. @@ -21,7 +22,10 @@ current changes on git with [previous release tags][git_tag_comparison]. - [Added `set_cursor_position` to `Window`][917] - [Added new Bevy reflection system][926] - Replaces the properties system +- [Add removal_detection example][945] + ### Changed + - [FileAssetIo includes full path on error][821] - [Removed ECS query APIs that could easily violate safety from the public interface][829] - [Changed Query filter API to be easier to understand][834] @@ -33,7 +37,9 @@ current changes on git with [previous release tags][git_tag_comparison]. - Created getters to get `Time` state and made members private. - Modifying `Time`'s values directly is no longer possible outside of bevy. - [Use `mailbox` instead of `fifo` for vsync on supported systems][920] + ### Fixed + - [Fixed typos in KeyCode identifiers][857] - [Don't draw text that isn't visible][893] - [Use `instant::Instant` for WASM compatibility][895] @@ -60,6 +66,7 @@ current changes on git with [previous release tags][git_tag_comparison]. [926]: https://github.com/bevyengine/bevy/pull/926 [931]: https://github.com/bevyengine/bevy/pull/931 [934]: https://github.com/bevyengine/bevy/pull/934 +[945]: https://github.com/bevyengine/bevy/pull/945 [955]: https://github.com/bevyengine/bevy/pull/955 ## Version 0.3.0 (2020-11-03) diff --git a/Cargo.toml b/Cargo.toml index bc85084c68..7dd6e0ce9e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -326,6 +326,10 @@ path = "examples/window/multiple_windows.rs" name = "window_settings" path = "examples/window/window_settings.rs" +[[example]] +name = "removal_detection" +path = "examples/ecs/removal_detection.rs" + [[example]] name = "hello_wasm" path = "examples/wasm/hello_wasm.rs" diff --git a/examples/ecs/removal_detection.rs b/examples/ecs/removal_detection.rs new file mode 100644 index 0000000000..6709f94047 --- /dev/null +++ b/examples/ecs/removal_detection.rs @@ -0,0 +1,74 @@ +// This example shows how you can know when a `Component` has been removed, so you can react to it. + +use bevy::prelude::*; + +fn main() { + // Information regarding removed `Component`s is discarded at the end of each frame, so you need + // to react to the removal before the frame is over. + // + // Also, `Components` are removed via a `Command`. `Command`s are applied after a stage has + // finished executing. So you need to react to the removal at some stage after the + // `Component` is removed. + // + // With these constraints in mind we make sure to place the system that removes a `Component` on + // the `stage::UPDATE` stage, and the system that reacts on the removal on the + // `stage::POST_UPDATE` stage. + App::build() + .add_plugins(DefaultPlugins) + .add_startup_system(setup.system()) + .add_system_to_stage(stage::UPDATE, remove_component.system()) + .add_system_to_stage(stage::POST_UPDATE, react_on_removal.system()) + .run(); +} + +// This `Struct` is just used for convenience in this example. This is the `Component` we'll be +// giving to the `Entity` so we have a `Component` to remove in `remove_component()`. +struct MyComponent; + +fn setup( + commands: &mut Commands, + asset_server: Res, + mut materials: ResMut>, +) { + let texture = asset_server.load("branding/icon.png"); + + commands + .spawn(Camera2dBundle::default()) + .spawn(SpriteBundle { + material: materials.add(texture.into()), + ..Default::default() + }) + .with(MyComponent); // Add the `Component`. +} + +fn remove_component( + time: Res