summaryrefslogtreecommitdiff
path: root/ecs/examples
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2025-08-20 14:17:14 +0200
committerHampusM <hampus@hampusmat.com>2025-08-20 14:17:14 +0200
commit5c9113431ea22c53cc59324c93ec3dc6efdfe926 (patch)
tree3218de3310339878a25b26ac957e0b2d54e7be49 /ecs/examples
parentfb9263ff3fd6211d5ca19e182094e25835886503 (diff)
feat(ecs): add support for pairs with target component as data
Diffstat (limited to 'ecs/examples')
-rw-r--r--ecs/examples/component_relationship.rs56
-rw-r--r--ecs/examples/relationship.rs3
2 files changed, 57 insertions, 2 deletions
diff --git a/ecs/examples/component_relationship.rs b/ecs/examples/component_relationship.rs
new file mode 100644
index 0000000..4453e3a
--- /dev/null
+++ b/ecs/examples/component_relationship.rs
@@ -0,0 +1,56 @@
+use ecs::pair::Pair;
+use ecs::phase::START as START_PHASE;
+use ecs::{Component, Query, World};
+
+#[derive(Component)]
+struct Person
+{
+ name: String,
+}
+
+fn print_dog_likers(query: Query<(&Person, Pair<Likes, &Dogs>)>)
+{
+ for (person, liked_dogs) in &query {
+ println!(
+ "{} likes {} dogs!",
+ person.name,
+ if liked_dogs.large { "large" } else { "small" },
+ );
+ }
+}
+
+#[derive(Component)]
+struct Likes;
+
+#[derive(Component)]
+struct Cats;
+
+#[derive(Component)]
+struct Dogs
+{
+ large: bool,
+}
+
+fn main()
+{
+ let mut world = World::new();
+
+ world.register_system(*START_PHASE, print_dog_likers);
+
+ world.create_entity((
+ Person { name: "Irving".to_string() },
+ Pair::new_with_comp_target::<Likes>(Dogs { large: true }),
+ ));
+
+ world.create_entity((
+ Person { name: "Mark".to_string() },
+ Pair::new_with_comp_target::<Likes>(Cats),
+ ));
+
+ world.create_entity((
+ Person { name: "Helena".to_string() },
+ Pair::new_with_comp_target::<Likes>(Dogs { large: false }),
+ ));
+
+ world.step();
+}
diff --git a/ecs/examples/relationship.rs b/ecs/examples/relationship.rs
index b607398..f99d090 100644
--- a/ecs/examples/relationship.rs
+++ b/ecs/examples/relationship.rs
@@ -1,6 +1,5 @@
-use ecs::pair::Pair;
+use ecs::pair::{Pair, Wildcard};
use ecs::phase::START as START_PHASE;
-use ecs::uid::Wildcard;
use ecs::{Component, Query, World};
#[derive(Component)]