From fd42ca5a25f8bab3ea66252f8bc0db02604f70dd Mon Sep 17 00:00:00 2001 From: HampusM Date: Thu, 1 Aug 2024 16:11:15 +0200 Subject: feat(ecs): add relationships --- ecs/examples/relationship.rs | 55 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 ecs/examples/relationship.rs (limited to 'ecs/examples/relationship.rs') 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)>) +{ + 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::::new(&world, sword_uid), + )); + + world.prepare(); + + world.emit(Start); +} -- cgit v1.2.3-18-g5258