summaryrefslogtreecommitdiff
path: root/ecs/examples/component_removed_event.rs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2025-09-13 19:24:29 +0200
committerHampusM <hampus@hampusmat.com>2025-09-13 19:24:29 +0200
commit6e7abf273d758bf15c1ba3e331e370b2bea3f8e2 (patch)
treed13ad55858583e0546632aeeb0b7336677378621 /ecs/examples/component_removed_event.rs
parent1bf48462393099d971b7a8613bd945d863d97273 (diff)
feat(ecs): re-implement component added & removed eventsHEADmaster
Diffstat (limited to 'ecs/examples/component_removed_event.rs')
-rw-r--r--ecs/examples/component_removed_event.rs39
1 files changed, 39 insertions, 0 deletions
diff --git a/ecs/examples/component_removed_event.rs b/ecs/examples/component_removed_event.rs
new file mode 100644
index 0000000..ffa37f4
--- /dev/null
+++ b/ecs/examples/component_removed_event.rs
@@ -0,0 +1,39 @@
+use ecs::actions::Actions;
+use ecs::component::Component;
+use ecs::event::component::Removed;
+use ecs::pair::Pair;
+use ecs::phase::UPDATE;
+use ecs::system::observer::Observe;
+use ecs::{Component, Query, World};
+
+#[derive(Debug, Component)]
+struct Cheese;
+
+fn eat_cheese(query: Query<(&Cheese,)>, mut actions: Actions)
+{
+ for (cheese_ent_id, (_,)) in query.iter_with_euids() {
+ println!("Eating cheese!");
+
+ actions.remove_components(cheese_ent_id, [Cheese::id()]);
+ }
+}
+
+fn on_cheese_removed(observe: Observe<Pair<Removed, Cheese>>)
+{
+ for cheese_ent in &observe {
+ println!("Cheese entity {} was eaten", cheese_ent.uid());
+ }
+}
+
+fn main()
+{
+ let mut world = World::new();
+
+ world.register_system(*UPDATE, eat_cheese);
+ world.register_observer(on_cheese_removed);
+
+ world.create_entity((Cheese,));
+
+ world.step();
+ world.step();
+}