From 8022e8998290b067b8aa0cb9cba8ba410826bdab Mon Sep 17 00:00:00 2001 From: HampusM Date: Thu, 21 May 2026 17:55:20 +0200 Subject: chore: rename ecs* crates to engine-ecs* --- engine-ecs/examples/component_events.rs | 64 +++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 engine-ecs/examples/component_events.rs (limited to 'engine-ecs/examples/component_events.rs') diff --git a/engine-ecs/examples/component_events.rs b/engine-ecs/examples/component_events.rs new file mode 100644 index 0000000..7c65630 --- /dev/null +++ b/engine-ecs/examples/component_events.rs @@ -0,0 +1,64 @@ +use engine_ecs::actions::Actions; +use engine_ecs::component::Component; +use engine_ecs::event::component::{Changed, EventMatchExt, Removed}; +use engine_ecs::pair::Pair; +use engine_ecs::phase::UPDATE; +use engine_ecs::system::observer::Observe; +use engine_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>) +{ + for evt_match in &observe { + let cheese = evt_match.get_ent_target_comp(); + + println!("{} cheese was eaten", cheese.name); + } +} + +fn on_cheese_crumbs_changed(observe: Observe>) +{ + for evt_match in &observe { + let cheese_crumbs = evt_match.get_ent_target_comp(); + + 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(); +} -- cgit v1.2.3-18-g5258