summaryrefslogtreecommitdiff
path: root/ecs/examples/component_relationship.rs
diff options
context:
space:
mode:
Diffstat (limited to 'ecs/examples/component_relationship.rs')
-rw-r--r--ecs/examples/component_relationship.rs56
1 files changed, 56 insertions, 0 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();
+}