diff options
author | HampusM <hampus@hampusmat.com> | 2024-08-01 16:11:15 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2024-08-02 15:34:54 +0200 |
commit | fd42ca5a25f8bab3ea66252f8bc0db02604f70dd (patch) | |
tree | 6d384b5f462e2699342372f6b56791fc4607e9c6 /ecs/examples/relationship.rs | |
parent | 70c7d745f918dd23343599963a619539f4f880cb (diff) |
feat(ecs): add relationships
Diffstat (limited to 'ecs/examples/relationship.rs')
-rw-r--r-- | ecs/examples/relationship.rs | 55 |
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); +} |