diff options
Diffstat (limited to 'ecs/examples/component_relationship.rs')
| -rw-r--r-- | ecs/examples/component_relationship.rs | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/ecs/examples/component_relationship.rs b/ecs/examples/component_relationship.rs new file mode 100644 index 0000000..e07b214 --- /dev/null +++ b/ecs/examples/component_relationship.rs @@ -0,0 +1,65 @@ +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::builder() + .relation::<Likes>() + .target_as_data(Dogs { large: true }) + .build(), + )); + + world.create_entity(( + Person { name: "Mark".to_string() }, + Pair::builder() + .relation::<Likes>() + .target_as_data(Cats) + .build(), + )); + + world.create_entity(( + Person { name: "Helena".to_string() }, + Pair::builder() + .relation::<Likes>() + .target_as_data(Dogs { large: false }) + .build(), + )); + + world.step(); +} |
