diff options
| author | HampusM <hampus@hampusmat.com> | 2026-05-21 17:55:20 +0200 |
|---|---|---|
| committer | HampusM <hampus@hampusmat.com> | 2026-05-21 17:55:20 +0200 |
| commit | 8022e8998290b067b8aa0cb9cba8ba410826bdab (patch) | |
| tree | 7171e79ce530e03079046ee8fd12167160c45480 /engine-ecs/examples/relationship.rs | |
| parent | 412cee02c252f91bcf0b70a3f5cc5fca6d2b4c62 (diff) | |
Diffstat (limited to 'engine-ecs/examples/relationship.rs')
| -rw-r--r-- | engine-ecs/examples/relationship.rs | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/engine-ecs/examples/relationship.rs b/engine-ecs/examples/relationship.rs new file mode 100644 index 0000000..749c202 --- /dev/null +++ b/engine-ecs/examples/relationship.rs @@ -0,0 +1,56 @@ +use engine_ecs::pair::{Pair, Wildcard}; +use engine_ecs::phase::START as START_PHASE; +use engine_ecs::{Component, Query, World}; + +#[derive(Component)] +struct Sword +{ + attack_strength: u32, +} + +#[derive(Component)] +struct Player; + +#[derive(Component)] +struct Health +{ + health: u32, +} + +#[derive(Component)] +struct Holding; + +fn print_player_stats(player_query: Query<(&Player, &Health, Pair<Holding, Wildcard>)>) +{ + for (_, health, target_sword) in &player_query { + println!("Player health: {}", health.health); + + if let Some(sword_ent) = target_sword.get_target_ent() { + let sword = sword_ent + .get::<Sword>() + .expect("Sword entity is missing sword component"); + + println!("Player sword attack strength: {}", sword.attack_strength); + } + } +} + +fn main() +{ + let mut world = World::new(); + + world.register_system(*START_PHASE, print_player_stats); + + let sword_uid = world.create_entity((Sword { attack_strength: 17 },)); + + world.create_entity(( + Player, + Health { health: 180 }, + Pair::builder() + .relation::<Holding>() + .target_id(sword_uid) + .build(), + )); + + world.step(); +} |
