summaryrefslogtreecommitdiff
path: root/ecs/examples/relationship.rs
diff options
context:
space:
mode:
Diffstat (limited to 'ecs/examples/relationship.rs')
-rw-r--r--ecs/examples/relationship.rs55
1 files changed, 55 insertions, 0 deletions
diff --git a/ecs/examples/relationship.rs b/ecs/examples/relationship.rs
new file mode 100644
index 0000000..8610b47
--- /dev/null
+++ b/ecs/examples/relationship.rs
@@ -0,0 +1,55 @@
+use ecs::event::Event;
+use ecs::relationship::Relationship;
+use ecs::{Component, Query, World};
+
+#[derive(Component)]
+struct Sword
+{
+ attack_strength: u32,
+}
+
+#[derive(Component)]
+struct Player;
+
+#[derive(Component)]
+struct Health
+{
+ health: u32,
+}
+
+struct Holding;
+
+fn print_player_stats(player_query: Query<(Player, Health, Relationship<Holding, Sword>)>)
+{
+ for (_, health, sword_relationship) in &player_query {
+ println!("Player health: {}", health.health);
+
+ if let Some(sword) = sword_relationship.get() {
+ println!("Player sword attack strength: {}", sword.attack_strength);
+ }
+ }
+}
+
+#[derive(Debug)]
+struct Start;
+
+impl Event for Start {}
+
+fn main()
+{
+ let mut world = World::new();
+
+ world.register_system(Start, print_player_stats);
+
+ let sword_uid = world.create_entity((Sword { attack_strength: 17 },));
+
+ world.create_entity((
+ Player,
+ Health { health: 180 },
+ Relationship::<Holding, Sword>::new(&world, sword_uid),
+ ));
+
+ world.prepare();
+
+ world.emit(Start);
+}