summaryrefslogtreecommitdiff
path: root/ecs/examples
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2025-10-12 17:13:42 +0200
committerHampusM <hampus@hampusmat.com>2025-10-13 17:22:35 +0200
commit88a33aed4115984d496fcd12a965f9c4aaa0faf6 (patch)
tree56e73790c9d1b8662d7997d93756ce9b2fadb8af /ecs/examples
parentea1d70c8c28e3b96da6264021fa1c62e28fcd8e4 (diff)
feat(ecs): emit Removed events before component removal
Diffstat (limited to 'ecs/examples')
-rw-r--r--ecs/examples/component_events.rs68
-rw-r--r--ecs/examples/component_removed_event.rs15
2 files changed, 80 insertions, 3 deletions
diff --git a/ecs/examples/component_events.rs b/ecs/examples/component_events.rs
new file mode 100644
index 0000000..4e7335f
--- /dev/null
+++ b/ecs/examples/component_events.rs
@@ -0,0 +1,68 @@
+use ecs::actions::Actions;
+use ecs::component::Component;
+use ecs::event::component::{Changed, Removed};
+use ecs::pair::Pair;
+use ecs::phase::UPDATE;
+use ecs::system::observer::Observe;
+use ecs::{Component, Query, World};
+
+#[derive(Debug, Component)]
+struct CheeseCrumbs
+{
+ cnt: usize,
+}
+
+#[derive(Debug, Component)]
+struct Cheese
+{
+ name: &'static str,
+}
+
+fn eat_cheese(query: Query<(&Cheese, &mut CheeseCrumbs)>, mut actions: Actions)
+{
+ for (cheese_ent_id, (_, mut cheese_crumbs)) in query.iter_with_euids() {
+ println!("Eating cheese!");
+
+ cheese_crumbs.cnt += 40;
+ cheese_crumbs.set_changed();
+
+ actions.remove_components(cheese_ent_id, [Cheese::id()]);
+ }
+}
+
+fn on_cheese_removed(observe: Observe<Pair<Removed, Cheese>>)
+{
+ for evt_match in &observe {
+ let ent = evt_match.get_entity().unwrap();
+
+ let cheese = ent.get::<Cheese>().unwrap();
+
+ println!("{} cheese was eaten", cheese.name);
+ }
+}
+
+fn on_cheese_crumbs_changed(observe: Observe<Pair<Changed, CheeseCrumbs>>)
+{
+ for evt_match in &observe {
+ let ent = evt_match.get_entity().unwrap();
+
+ let cheese_crumbs = ent.get::<CheeseCrumbs>().unwrap();
+
+ println!("Cheese crumbs count changed to {}", cheese_crumbs.cnt);
+ }
+}
+
+fn main()
+{
+ let mut world = World::new();
+
+ world.register_system(*UPDATE, eat_cheese);
+ world.register_observer(on_cheese_removed);
+ world.register_observer(on_cheese_crumbs_changed);
+
+ world.create_entity((Cheese { name: "Brie" }, CheeseCrumbs { cnt: 0 }));
+ world.create_entity((Cheese { name: "Parmesan" }, CheeseCrumbs { cnt: 0 }));
+ world.create_entity((Cheese { name: "Gouda" }, CheeseCrumbs { cnt: 0 }));
+
+ world.step();
+}
diff --git a/ecs/examples/component_removed_event.rs b/ecs/examples/component_removed_event.rs
index e8d1a47..f984092 100644
--- a/ecs/examples/component_removed_event.rs
+++ b/ecs/examples/component_removed_event.rs
@@ -7,7 +7,10 @@ use ecs::system::observer::Observe;
use ecs::{Component, Query, World};
#[derive(Debug, Component)]
-struct Cheese;
+struct Cheese
+{
+ name: &'static str,
+}
fn eat_cheese(query: Query<(&Cheese,)>, mut actions: Actions)
{
@@ -21,7 +24,11 @@ fn eat_cheese(query: Query<(&Cheese,)>, mut actions: Actions)
fn on_cheese_removed(observe: Observe<Pair<Removed, Cheese>>)
{
for evt_match in &observe {
- println!("Cheese entity {} was eaten", evt_match.id());
+ let ent = evt_match.get_entity().unwrap();
+
+ let cheese = ent.get::<Cheese>().unwrap();
+
+ println!("{} cheese was eaten", cheese.name);
}
}
@@ -32,7 +39,9 @@ fn main()
world.register_system(*UPDATE, eat_cheese);
world.register_observer(on_cheese_removed);
- world.create_entity((Cheese,));
+ world.create_entity((Cheese { name: "Brie" },));
+ world.create_entity((Cheese { name: "Parmesan" },));
+ world.create_entity((Cheese { name: "Gouda" },));
world.step();
world.step();