From f98ff3c0ea0981441ae577c50680de7701f64470 Mon Sep 17 00:00:00 2001 From: Lukas Orsvarn Date: Sun, 29 Nov 2020 01:05:23 +0100 Subject: [PATCH] Add removal_detection example --- 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 abacc1779410f..f4f0d672410fe 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] @@ -59,6 +65,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 ## Version 0.3.0 (2020-11-03) diff --git a/Cargo.toml b/Cargo.toml index bc85084c68c6f..7dd6e0ce9e61a 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 0000000000000..6709f940477e3 --- /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